From 2c9b2e6dd739775c75b5e4adfd2448e3c991e19b Mon Sep 17 00:00:00 2001 From: Pierre-Luc Paour Date: Sat, 28 Sep 2002 18:39:26 +0000 Subject: [PATCH] Implemented resize before upload. --- com/gallery/GalleryRemote/GalleryComm.java | 2 +- com/gallery/GalleryRemote/ImageUtils.java | 86 ++++++++++++++++++-- com/gallery/GalleryRemote/model/Picture.java | 18 +++- 3 files changed, 98 insertions(+), 8 deletions(-) diff --git a/com/gallery/GalleryRemote/GalleryComm.java b/com/gallery/GalleryRemote/GalleryComm.java index e554190..fa72616 100644 --- a/com/gallery/GalleryRemote/GalleryComm.java +++ b/com/gallery/GalleryRemote/GalleryComm.java @@ -207,7 +207,7 @@ boolean uploadPicture(Picture p) { }; Log.log(Log.TRACE, MODULE, "add-item parameters: " + Arrays.asList(opts)); - NVPair[] afile = { new NVPair("userfile", p.getSource().getAbsolutePath()) }; + NVPair[] afile = { new NVPair("userfile", p.getUploadSource().getAbsolutePath()) }; NVPair[] hdrs = new NVPair[1]; byte[] data = Codecs.mpFormDataEncode(opts, afile, hdrs); HTTPConnection mConnection = new HTTPConnection(url); diff --git a/com/gallery/GalleryRemote/ImageUtils.java b/com/gallery/GalleryRemote/ImageUtils.java index 47d8ab0..be75fc0 100644 --- a/com/gallery/GalleryRemote/ImageUtils.java +++ b/com/gallery/GalleryRemote/ImageUtils.java @@ -40,12 +40,14 @@ public class ImageUtils { static int totalIter = 0; static boolean useIM = false; static String imPath = null; + static File tmpDir = null; public static final int THUMB = 0; public static final int PREVIEW = 1; + public static final int UPLOAD = 2; - static String[] filterName = new String[2]; - static String[] format = new String[2]; + static String[] filterName = new String[3]; + static String[] format = new String[3]; /** * Perform the actual icon loading @@ -80,7 +82,7 @@ public static ImageIcon load( String filename, Dimension d, int usage ) { cmdline.append(d.height); cmdline.append(" +profile \"*\" "); - File temp = File.createTempFile("thumb", "." + format[usage], new File("thumbs")); + File temp = File.createTempFile("thumb", "." + format[usage], tmpDir); toDelete.add(temp); cmdline.append(temp.getPath()); @@ -125,13 +127,80 @@ public static ImageIcon load( String filename, Dimension d, int usage ) { return r; } + public static File resize( String filename, Dimension d ) { + File r = null; + long start = System.currentTimeMillis(); + + if (useIM) { + try { + StringBuffer cmdline = new StringBuffer(imPath); + cmdline.append(" -size "); + + cmdline.append(d.width); + cmdline.append("x"); + cmdline.append(d.height); + + if (filterName[UPLOAD] != null && filterName[UPLOAD].length() > 0) { + cmdline.append(" -filter "); + cmdline.append(filterName[UPLOAD]); + } + + cmdline.append(" \""); + cmdline.append(filename); + + cmdline.append("\" -resize \""); + cmdline.append(d.width); + cmdline.append("x"); + cmdline.append(d.height); + cmdline.append(">\" "); + + //cmdline.append("-gravity SouthEast -draw \"image Over 200,200 0,0 G:\\Projects\\Dev\\gallery_remote10\\2ni.png\" "); + + r = File.createTempFile("res" + , "." + GalleryFileFilter.getExtension(filename), tmpDir); + toDelete.add(r); + + cmdline.append(r.getPath()); + + Log.log(Log.TRACE, MODULE, "Executing " + cmdline.toString()); + + Process p = Runtime.getRuntime().exec(cmdline.toString()); + p.waitFor(); + Log.log(Log.TRACE, MODULE, "Returned with value " + p.exitValue()); + + if (p.exitValue() != 0) { + Log.log(Log.CRITICAL, MODULE, "ImageMagick doesn't seem to be working. Disabling"); + useIM = false; + r = null; + } + } catch (IOException e1) { + Log.logException(Log.ERROR, MODULE, e1); + } catch (InterruptedException e2) { + Log.logException(Log.ERROR, MODULE, e2); + } + } + + if ( ! useIM && r == null ) { + throw new UnsupportedOperationException("IM must be installed for this operation"); + } + + long time = System.currentTimeMillis() - start; + totalTime += time; + totalIter++; + Log.log(Log.TRACE, MODULE, "Time: " + time + " - Avg: " + (totalTime/totalIter) ); + + return r; + } + static { - File f = new File("thumbs"); + tmpDir = new File(System.getProperty("java.io.tmpdir") + "thumbs"); - if (!f.exists()) { - f.mkdir(); + if (!tmpDir.exists()) { + tmpDir.mkdirs(); } + Log.log(Log.INFO, MODULE, "tmpDir: " + tmpDir.getPath()); + try { PropertiesFile p = new PropertiesFile("imagemagick/im"); @@ -150,9 +219,14 @@ public static ImageIcon load( String filename, Dimension d, int usage ) { if (useIM) { filterName[THUMB] = p.getProperty("imThumbnailResizeFilter"); filterName[PREVIEW] = p.getProperty("imPreviewResizeFilter"); + filterName[UPLOAD] = p.getProperty("imUploadResizeFilter"); + if ( filterName[UPLOAD] == null ) { + filterName[UPLOAD] = filterName[PREVIEW]; + } format[THUMB] = p.getProperty("imThumbnailResizeFormat", "gif"); format[PREVIEW] = p.getProperty("imPreviewResizeFormat", "jpg"); + format[UPLOAD] = null; } } catch (Exception e) { Log.logException(Log.CRITICAL, MODULE, e); diff --git a/com/gallery/GalleryRemote/model/Picture.java b/com/gallery/GalleryRemote/model/Picture.java index 267b6d6..2e6f9c3 100644 --- a/com/gallery/GalleryRemote/model/Picture.java +++ b/com/gallery/GalleryRemote/model/Picture.java @@ -26,6 +26,7 @@ import java.awt.datatransfer.*; import java.util.*; +import com.gallery.GalleryRemote.*; /** * Picture model @@ -97,7 +98,22 @@ public File getSource() { return source; } - + /** + * Gets the fource file of the picture, prepared for upload. + * Called by GalleryComm to upload the picture. + * + *@return The source value + */ + public File getUploadSource() { + if ( GalleryRemote.getInstance().properties.getBooleanProperty("resizeBeforeUpload") ) { + Dimension d = GalleryRemote.getInstance().properties.getDimensionProperty("resizeTo"); + + return ImageUtils.resize( getSource().getPath(), d ); + } else { + return getSource(); + } + } + /** * Gets the caption attribute of the Picture object *