1 24 package org.riotfamily.common.image; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 30 import org.riotfamily.common.io.CommandUtils; 31 32 37 public class ImageMagickThumbnailer implements Thumbnailer { 38 39 private String convertCommand = "convert"; 40 41 private boolean crop; 42 43 private String backgroundColor; 44 45 public void setConvertCommand(String convertCommand) { 46 this.convertCommand = convertCommand; 47 } 48 49 public void setCrop(boolean crop) { 50 this.crop = crop; 51 } 52 53 78 public void setBackgroundColor(String backgroundColor) { 79 this.backgroundColor = backgroundColor; 80 } 81 82 public void renderThumbnail(File source, File dest, int width, int height) 83 throws IOException { 84 85 ArrayList cmd = new ArrayList (); 86 cmd.add(convertCommand); 87 cmd.add(source.getAbsolutePath()); 88 cmd.add("-resize"); 89 if (crop) { 90 cmd.add("x" + height * 2); 91 cmd.add("-resize"); 92 cmd.add(width * 2 + "x<"); 93 cmd.add("-resize"); 94 cmd.add("50%"); 95 cmd.add("-crop"); 96 cmd.add(width + "x" + height + "+0+0"); 97 cmd.add("+repage"); 98 } 99 else { 100 cmd.add(width + "x" + height + ">"); 101 if (backgroundColor != null) { 102 cmd.add("-size"); 103 cmd.add(width + "x" + height); 104 cmd.add("xc:" + backgroundColor); 105 cmd.add("+swap"); 106 cmd.add("-gravity"); 107 cmd.add("center"); 108 cmd.add("-composite"); 109 } 110 } 111 cmd.add("-colorspace"); 112 cmd.add("RGB"); 113 cmd.add(dest.getAbsolutePath()); 114 CommandUtils.exec(cmd); 115 } 116 117 } 118 | Popular Tags |