A little side project I have here at work has taken a life on of its own. Most recently with the launch of the new Aurora storefront I once again enhanced an internal tool to automatically size product images to adhere to the size requirements for the store. I ended up using Eclipses SWT (The Standard Widget Toolkit) that comes with Eclipse to do this. What I wanted to share today was how easy this was using the SWT API’s.
In the code below I actually base the new scale off of the width passed into the method. Once I get that ratio I can apply it to the height and width of the original image to get the new sizes.
//Retain the original width and height double oWidth = data.width; double oHeight = data.height; //Use the width as the scale so the end image is proportional // "width" is the newly desired width of the resulting image double yScale = width/oWidth; int newWidth = (int)(oWidth * yScale); int newHeight = (int)(oHeight * yScale); //Call the magic API to scale the image data ImageData nData = data.scaledTo(newWidth, newHeight); //Create a new ImageLoader to save the data, base it on the new Data ImageLoader imageLoader = new ImageLoader(); imageLoader.data = new ImageData[] {nData}; int option = SWT.IMAGE_JPEG; //Figure out the format of the image based on the filename //This is probably stored in the ImageData somewhere... if (newFileName.toLowerCase().endsWith("jpg")) option = SWT.IMAGE_JPEG; else if(newFileName.toLowerCase().endsWith("png")) option = SWT.IMAGE_PNG; else if(newFileName.toLowerCase().endsWith("bmp")) option = SWT.IMAGE_BMP; else if(newFileName.toLowerCase().endsWith("gif")) option = SWT.IMAGE_GIF; else if(newFileName.toLowerCase().endsWith("ico")) option = SWT.IMAGE_ICO; else if(newFileName.toLowerCase().endsWith("tiff")) option = SWT.IMAGE_TIFF; //Save the new image to the new file name! imageLoader.save(newFileName,option);
Advertisements