1 4 package com.openedit.store.images; 5 6 import java.io.File ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import com.openedit.OpenEditException; 14 import com.openedit.modules.image.ImageResizer; 15 import com.openedit.util.Exec; 16 17 public class ImageMagickResizer extends ImageResizer 18 { 19 protected String fieldCommandName; 20 protected String fieldGhostScriptCommand; 21 22 private static final Log log = LogFactory.getLog(ImageMagickResizer.class); 23 public void resizeImage(File inFile, File inOutFile) throws Exception 24 { 25 if ( inFile.getName().toLowerCase().endsWith(".avi")) 26 { 27 return; 30 } 31 List com = new ArrayList (); 32 com.add(getCommandName()); 33 com.add(inFile.getAbsolutePath()); 34 com.add("-resize"); 35 com.add( (int)getMaxScaledSize().getWidth() + "x" + (int)getMaxScaledSize().getHeight() + ">" ); 36 com.add("-colorspace"); 37 com.add("rgb"); 38 com.add(inOutFile.getAbsolutePath()); 39 inOutFile.getParentFile().mkdirs(); 40 long start = System.currentTimeMillis(); 41 if ( runExec(com) ) 42 { 43 log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " " + inOutFile.getName()); 44 } 45 } 46 47 public void resizeThumbnailImage(File inFile, File inOutFile) throws Exception 48 { 49 50 if ( inFile.getName().toLowerCase().endsWith(".avi")) 51 { 52 return; 55 } 56 57 58 List com = new ArrayList (); 59 com.add(getCommandName()); 60 com.add(inFile.getAbsolutePath()); 61 com.add("-thumbnail"); 62 com.add( (int)getMaxScaledSize().getWidth() + "x" + (int)getMaxScaledSize().getHeight() + ">" ); 63 com.add(inOutFile.getAbsolutePath()); 64 inOutFile.getParentFile().mkdirs(); 65 long start = System.currentTimeMillis(); 66 if ( runExec(com) ) 67 { 68 log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " " + inOutFile.getName()); 69 } 70 } 71 protected boolean runExec(List inCom) throws OpenEditException 72 { 73 Exec exec = new Exec(); 74 exec.setTrackOutput(false); 75 boolean works = exec.runExec(inCom); 76 if ( !works ) 77 { 78 log.info("Resize failed running again with output tracking"); 79 exec.setTrackOutput(true); 80 works = exec.runExec(inCom); 81 log.info(exec.getStandardOutput() + " error output:" + exec.getErrorOutput() ); 82 } 83 return works; 84 } 85 86 public String getCommandName() 87 { 88 if (fieldCommandName == null) 89 { 90 String env = System.getProperty("IMAGE_MAGICK_COMMAND"); 91 if ( env == null) 92 { 93 env = System.getenv("IMAGE_MAGICK_COMMAND"); 94 } 95 if ( env == null) 96 { 97 env = "convert"; 98 } 99 fieldCommandName = env; 100 } 101 102 return fieldCommandName; 103 } 104 public void setGhostScriptCommand( String inName) 105 { 106 fieldGhostScriptCommand = inName; 107 } 108 public String getGhostScriptCommand() 109 { 110 if (fieldGhostScriptCommand == null) 111 { 112 String env = System.getProperty("GS_COMMAND"); 113 if ( env == null) 114 { 115 env = System.getenv("GS_COMMAND"); 116 } 117 if ( env == null) 118 { 119 if ( System.getProperty("os.name").indexOf("Windows") > -1) 120 { 121 env = "gswin32c.exe"; 122 } 123 else 124 { 125 env = "gs"; 126 } 127 } 128 fieldGhostScriptCommand = env; 129 } 130 131 return fieldGhostScriptCommand; 132 133 } 134 public void setCommandName(String inCommandName) 135 { 136 fieldCommandName = inCommandName; 137 } 138 139 public boolean convert(File inIn, File inOut) throws OpenEditException 140 { 141 boolean iseps = inOut.getPath().toLowerCase().endsWith("eps") 143 && inIn.getPath().toLowerCase().endsWith("ai"); 144 145 List com = new ArrayList (); 146 if ( iseps ) 147 { 148 com.add(getGhostScriptCommand()); 149 150 com.add("-q" ); 151 com.add("-dBATCH" ); 152 com.add("-dSAFER" ); 153 com.add("-dNOPAUSE" ); 154 com.add("-sDEVICE=epswrite" ); 155 com.add("-sOutputFile=\"" + inOut.getAbsolutePath() + "\""); 156 com.add("\"" + inIn.getAbsolutePath() + "\""); 157 158 160 } 161 else 162 { 163 com.add(getCommandName()); 164 com.add(inIn.getAbsolutePath()); 165 com.add(inOut.getAbsolutePath()); 166 } 167 inOut.getParentFile().mkdirs(); 168 long start = System.currentTimeMillis(); 169 if ( runExec(com) ) 170 { 171 log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " output " + inOut.getAbsolutePath() ); 172 return true; 173 } 174 else 175 { 176 return false; 177 } 178 179 } 180 } 181 | Popular Tags |