Skip to content

Commit

Permalink
Implemented resize before upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Luc Paour committed Sep 28, 2002
1 parent 2eb4daf commit 2c9b2e6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion com/gallery/GalleryRemote/GalleryComm.java
Expand Up @@ -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);
Expand Down
86 changes: 80 additions & 6 deletions com/gallery/GalleryRemote/ImageUtils.java
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");

Expand All @@ -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);
Expand Down
18 changes: 17 additions & 1 deletion com/gallery/GalleryRemote/model/Picture.java
Expand Up @@ -26,6 +26,7 @@
import java.awt.datatransfer.*;
import java.util.*;

import com.gallery.GalleryRemote.*;

/**
* Picture model
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 2c9b2e6

Please sign in to comment.