Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Created January 4, 2013 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenfeather/4452230 to your computer and use it in GitHub Desktop.
Save stephenfeather/4452230 to your computer and use it in GitHub Desktop.
Module that uses the ti.imagefactory module to resize images.
/*jslint vars: true, sloppy: true, nomen: true, maxerr: 1000 */
function ImageProcessor(imagePath){
//if (Ti.App.Properties.getInt('CompressImages') === 1){
var ImageFactory = require('ti.imagefactory');
Ti.API.info('Compressing and Resizing Image');
var tempFile = Ti.Filesystem.getFile(imagePath);
var imageBlob = tempFile.read();
//Ti.API.info('Blob Length: '+imageBlob.length);
//Ti.API.info('Blob Width: '+imageBlob.width);
//Ti.API.info('Blob Height: '+imageBlob.height);
var heightOfImage = imageBlob.height;
var widthOfImage = imageBlob.width;
var aspectRatio = heightOfImage / widthOfImage;
var compression = (Ti.App.Properties.getInt('ImageQuality')) || 90;
if (Ti.Android){
compression = Math.round(compression/100);
}
if (widthOfImage > 960) {
var newWidth = 960;
var newHeight = Math.round(newWidth*aspectRatio);
var smallImage = ImageFactory.imageAsResized(imageBlob, {
width: newWidth,
height: newHeight,
quality: compression
});
//}
//Ti.API.info('New Blob Length: '+smallImage.length);
//Ti.API.info('Blob Width: '+smallImage.width);
//Ti.API.info('Blob Height: '+smallImage.height);
tempFile.write(smallImage);
//Ti.API.info('Resized image from ' + widthOfImage + ', ' + heightOfImage + ' to ' + newWidth + ', ' + newHeight +' | Ratio of '+aspectRatio+' | Quality of '+compression);
T//i.API.info('Reduce size from '+imageBlob.length+' to '+smallImage.length+' for a savings of '+(imageBlob.length-smallImage.length));
}
}
module.exports = ImageProcessor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment