Java has excellent capability for images, sound, multimedia, etc. I developed this utility to, yes you guessed it, save some cycles and pain and aggravation for yours truly. This utility takes an image and resizes it based on input parameters. It requires three parameters: 1) url to the image, 2) width you want the resized image, 3) height you want the resized image. Now, as everybody know, it is impossible to resize objects exactly as asked for and keep a decent aspect. If I try, for example to take a large photo and resize it to 100×100, well… it’s not going to look good. The utility resizes while keeping the image “viewable”, so if I ask to resize to 100×100, the utility will select the best ratio. It still may look nasty, but as least it is consistently nasty!
The utility works for all images, but it works best for jpg. URLs to images behind ssl will not work. I suppose I could set up ssl connection, but… nah. Why make this hard! If you are faced with this issue, I suggest copying ssl image to your hard drive and point the utility there.
If you are using a Mac, trying this out will be easy. All you have to do is copy the code (see below) to a file, save it, compile it and run it. That’s it sports fans. Have I mentioned that I love my iMac? All the jar files were already setup and I did not even have to include a class path! Insane. For those out there who have run java programs before, you know that finding the necessary jar files can be a PIA. Well, if you are not using a Mac, all bets are off. You are on your own, although I run this at work on Windows XP.
At work, I made a slight modification to the utility to accept - in addition to url - a directory. Being able to ingest everything in a folder and resize is sweet. All I do is copy all the images I want to a folder and point the utility to it. Double sweet.
Note to reader: Adobe Photoshop does a better job of resizing images.
Run from my Mac: javac ResizeImage java ResizeImage http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/800/orca-killer-whale.jpg 300 300 Image before: http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/800/orca-killer-whale.jpg Image after: http://www.csatechconsulting.com/blog/wp-content/uploads/2009/02/staticfilesngssharedstaticfilesanimalsimages800orcakillerwhale.jpg
ResizeImage.java:
import java.io.*; import java.net.URL; import java.net.MalformedURLException; import com.sun.image.codec.jpeg.*; import java.awt.*; import java.awt.image.*; public class ResizeImage{ public static void createThumbnail(String url, String width, String height){ try{ // get url handle URL my_url = new URL(url); // get image object so we can get width, height of image Image image = Toolkit.getDefaultToolkit().getImage(my_url); // let's track the object MediaTracker mt = new MediaTracker(new Container()); mt.addImage(image, 0); // wait for image to load mt.waitForID(0); // convert input width/height to integers int thumbWidth = Integer.parseInt(width); int thumbHeight = Integer.parseInt(height); double thumbRatio = (double)thumbWidth / (double)thumbHeight; // get width/height of image int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double)imageWidth / (double)imageHeight; if (thumbRatio < imageRatio) thumbHeight = (int)(thumbWidth / imageRatio); else thumbWidth = (int)(thumbHeight * imageRatio); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); // resize image Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // extract the file name from the url and replace "evil" characters String fName = my_url.getFile().replaceAll("/","").replaceAll(":","").replaceAll("-",""); // create output file BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fName)); // write resized image to output file JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); int quality = Integer.parseInt("100"); // we want good quality quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float)quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); // close output file out.close(); } catch(MalformedURLException e){ e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ // must have 3 parameters: 1) url of image, 2) width you want the new image, 3) height you want the new image String url = args[0]; String w = args[1]; String h = args[2]; createThumbnail(url, w, h); } }