001: /*
002: * Created on Sep 20, 2005
003: */
004: package org.openedit.store.images;
005:
006: import java.io.File;
007: import java.util.ArrayList;
008: import java.util.List;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import com.openedit.OpenEditException;
014: import com.openedit.modules.image.ConvertInstructions;
015: import com.openedit.modules.image.ImageResizer;
016: import com.openedit.util.Exec;
017: import com.openedit.util.PathUtilities;
018:
019: public class ImageMagickResizer extends ImageResizer {
020: private static final Log log = LogFactory
021: .getLog(ImageMagickResizer.class);
022: protected String fieldCommandName;
023:
024: public boolean canConvert(String inPath) {
025: String ext = PathUtilities.extractPageType(inPath);
026: String mime = getMimeTypeMap().getMimeType(ext);
027: if (mime.startsWith("image") ||
028:
029: mime.equals("application/photoshop")) {
030: return true;
031: }
032:
033: if (!System.getProperty("os.name").toLowerCase().contains(
034: "windows")) {
035: if (mime.equals("application/postscript")
036: || mime.equals("application/pdf")
037: || mime.equals("application/illustrator")
038: || mime.equals("image/x-eps")) {
039: return true;
040: }
041: }
042:
043: return false;
044: }
045:
046: public boolean resizeImage(File inFile, File inOutFile,
047: ConvertInstructions inStructions) throws Exception {
048: List com = createCommand(inFile, inStructions);
049:
050: com.add("-resize");
051: com.add((int) inStructions.getMaxScaledSize().getWidth() + "x"
052: + (int) inStructions.getMaxScaledSize().getHeight()
053: + ">");
054: com.add("-colorspace");
055: com.add("rgb");
056: com.add(inOutFile.getAbsolutePath());
057:
058: inOutFile.getParentFile().mkdirs();
059: long start = System.currentTimeMillis();
060: if (runExec(com)) {
061: log.info("Resize complete in:"
062: + (System.currentTimeMillis() - start) + " "
063: + inOutFile.getName());
064: return true;
065: }
066: return false;
067: }
068:
069: protected List createCommand(File inFile,
070: ConvertInstructions inStructions) {
071: List com = new ArrayList();
072: //New version of Image Magik are 0 based
073: int page = inStructions.getPageNumber();
074: page--;
075: page = Math.min(0, page);
076:
077: com.add(getCommandName());
078: com.add(inFile.getAbsolutePath() + "[" + page + "]");
079: return com;
080: }
081:
082: public void resizeThumbnailImage(File inFile, File inOutFile,
083: ConvertInstructions inStructions) throws Exception {
084:
085: List com = new ArrayList();
086: com.add(getCommandName());
087: com.add(inFile.getAbsolutePath() + "["
088: + inStructions.getPageNumber() + "]");
089: com.add("-thumbnail");
090: com.add((int) inStructions.getMaxScaledSize().getWidth() + "x"
091: + (int) inStructions.getMaxScaledSize().getHeight()
092: + ">");
093: com.add(inOutFile.getAbsolutePath());
094: inOutFile.getParentFile().mkdirs();
095: long start = System.currentTimeMillis();
096: if (runExec(com)) {
097: log.info("Resize complete in:"
098: + (System.currentTimeMillis() - start) + " "
099: + inOutFile.getName());
100: }
101: }
102:
103: protected boolean runExec(List inCom) throws OpenEditException {
104: Exec exec = new Exec();
105: exec.setTrackOutput(false);
106: boolean works = exec.runExec(inCom);
107: if (!works) {
108: log
109: .info("Resize failed running again with output tracking");
110: exec.setTrackOutput(true);
111: works = exec.runExec(inCom);
112: log.info(exec.getStandardOutput() + " error output:"
113: + exec.getErrorOutput());
114: }
115: return works;
116: }
117:
118: public String getCommandName() {
119: if (fieldCommandName == null) {
120: String env = System.getProperty("IMAGE_MAGICK_COMMAND");
121: if (env == null) {
122: env = System.getenv("IMAGE_MAGICK_COMMAND");
123: }
124: if (env == null) {
125: if (System.getProperty("os.name").toUpperCase()
126: .contains("WINDOWS")) {
127: env = "imconvert.exe";
128: } else {
129: env = "convert";
130: }
131: }
132: fieldCommandName = env;
133: }
134:
135: return fieldCommandName;
136: }
137:
138: public void setCommandName(String inCommandName) {
139: fieldCommandName = inCommandName;
140: }
141:
142: public List getConvertCommand(File inIn, File inOut,
143: int inPageNumber) {
144: List com = new ArrayList();
145: com.add(getCommandName());
146: com.add(inIn.getAbsolutePath() + "[" + inPageNumber + "]");
147: com.add(inOut.getAbsolutePath());
148: return com;
149: }
150:
151: public boolean convert(File inIn, File inOut,
152: ConvertInstructions inStructions) throws OpenEditException {
153: //if this is an eps output from an ai input then use ghostscript
154: //try to call convert with a 0-based page index (Image Magick 6.3 series)
155: List com = getConvertCommand(inIn, inOut, inStructions
156: .getPageNumber() - 1);
157: inOut.getParentFile().mkdirs();
158: long start = System.currentTimeMillis();
159: if (runExec(com)) {
160: log.info("Resize complete in:"
161: + (System.currentTimeMillis() - start) + " output "
162: + inOut.getAbsolutePath());
163: return true;
164: } else {
165: //TODO: Drop support for old Image Magick versions to clean up code
166: log
167: .debug("0-based page index failed. Trying 1-based page index");
168: //try to call convert with a 0-based page index (older Image Magick)
169:
170: com = getConvertCommand(inIn, inOut, inStructions
171: .getPageNumber());
172: if (runExec(com)) {
173: log.info("Resize complete in:"
174: + (System.currentTimeMillis() - start)
175: + " output " + inOut.getAbsolutePath());
176: return true;
177: }
178: return false;
179: }
180:
181: }
182: }
|