1 7 package com.openedit.store.edit; 8 9 import java.awt.Dimension ; 10 import java.awt.image.BufferedImage ; 11 import java.io.File ; 12 import java.util.Iterator ; 13 14 import javax.imageio.ImageIO ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.apache.lucene.document.Document; 19 20 import com.openedit.modules.search.LuceneHitTracker; 21 import com.openedit.store.Category; 22 import com.openedit.store.Product; 23 import com.openedit.store.Store; 24 import com.openedit.store.images.ImageMagickResizer; 25 26 32 public class AutoImageCreator { 33 34 private static final Log log = LogFactory.getLog(AutoImageCreator.class); 35 36 protected Store fieldStore; 37 protected boolean fieldUse100s; 38 39 public void run() throws Exception 40 { 41 ImageMagickResizer resizer = new ImageMagickResizer(); 43 resizer.setMaxScaledSize(new Dimension (350,400)); 44 resizer.setCommandName(getStore().getConfigValue("convertpath") ); 45 resizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") ); 46 47 ImageMagickResizer tresizer = new ImageMagickResizer(); 48 tresizer.setMaxScaledSize(new Dimension (150,200)); 49 tresizer.setCommandName(getStore().getConfigValue("convertpath") ); 50 tresizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") ); 51 log.info("Checking for Medium sized images"); 53 Category catalog = getStore().getCatalog("index"); 56 String catalogs = "index"; 57 for ( Iterator iter = catalog.getChildren().iterator(); iter.hasNext(); ) 58 { 59 Category childCatalog = (Category) iter.next(); 60 catalogs = catalogs + " OR " + childCatalog.getId(); 61 } 62 String query = "category:( " + catalogs + ")"; 63 64 LuceneHitTracker hits = getStore().search(query); 65 log.info("Checking " + hits.getTotal() + " images"); 66 for (int i = 0; i < hits.getTotal(); i++) 67 { 68 Document hit = (Document)hits.get(i); 69 String productid = hit.get("id"); 71 String path = getStore().getProductPathFinder().idToPath(productid); 72 File med = new File ( getStore().getStoreDirectory(),"/products/images/medium/" + path + ext() ); 73 if ( !med.exists() ) 74 { 75 log.info("Creating medium image:" + med.getName()); 76 Product prod = getStore().getProduct(productid); 77 log.info("Loading original"); 78 File orig = getOriginalDocument(prod); 79 if ( orig != null && orig.exists() ) 80 { 81 log.info("Start resize " + med.getName()); 82 resizer.resizeImage(orig,med); 83 log.info("Resize complete " + med.getName()); 84 } 85 else 86 { 87 log.error("No such original: " + orig + " =>" + productid ); 88 } 89 } 90 File thumb = new File ( getStore().getStoreDirectory(),"/products/images/thumb/" + path + ext() ); 91 if( !thumb.exists() && med.exists() ) 92 { 93 tresizer.resizeImage(med,thumb); 94 } 95 else if (med.exists() && thumb.exists() && thumb.length() > 6000) 96 { 97 BufferedImage image = ImageIO.read(thumb); 98 log.info("large image " + productid + " " + image.getHeight()); 99 if ( image.getHeight() > 200 || image.getWidth() > 150 ) 100 { 101 tresizer.resizeImage(med,thumb); 102 } 103 } 104 105 } 106 log.info("Completed image processing"); 107 } 108 protected String ext() 109 { 110 if ( isUse100s()) 111 { 112 return "-100.jpg"; 113 } 114 return ".jpg"; 115 } 116 protected File getOriginalDocument(Product inProd) 117 { 118 File image = new File ( getStore().getStoreDirectory(),"/products/images/original/" + 119 getStore().getProductPathFinder().idToPath(inProd.getId()) + ext() ); 120 return image; 121 } 122 public Store getStore() 123 { 124 return fieldStore; 125 } 126 public void setStore(Store inStore) 127 { 128 fieldStore = inStore; 129 } 130 public boolean isUse100s() 131 { 132 return fieldUse100s; 133 } 134 public void setUse100s(boolean inUse100s) 135 { 136 fieldUse100s = inUse100s; 137 } 138 139 } 140 141 | Popular Tags |