Skip to content

Instantly share code, notes, and snippets.

@sobytes
Forked from FokkeZB/CROP.md
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobytes/81ca17298b1e239ccc06 to your computer and use it in GitHub Desktop.
Save sobytes/81ca17298b1e239ccc06 to your computer and use it in GitHub Desktop.
Image (cropping) CommonJS lib for Titanium

Often I need to display a user-provided picture in an ImageView in such a way that the whole ImageView is filled with as much of the picture possible.

This is how I do it:

var image = require('image');

Ti.Media.showCamera({
        mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
        success: function (e) {
                myImageView.image = image.crop(e.media, myImageView.rect);
        }
});

Take a look at the next image.js file to learn how it works.

Params

  • blob: The Ti.Blob containing the image.
  • options: Either an object (or Dimension) containing the target width and height and the other options listed below, or a Number representing only the width.
  • height: The target height.

Options

  • width: See above.
  • height: See above.
  • pixels: Set to FALSE to disable converting target dimensions to actual pixels needed for either Retina or Android variable DPI.
  • fix: Set to FALSE to disable the workaround for bug TIMOB-4865.
var _factor;
function crop(blob, options, height) {
if (typeof options !== 'object') {
options = {
width: options,
height: height
}
}
if (!blob.width || !blob.width) {
return blob;
}
// https://jira.appcelerator.org/browse/TIMOB-4865
if (options.fix !== false) {
blob = blob.imageAsResized(blob.width, blob.height);
}
if (options.pixels !== false) {
options = pixels(options);
}
if (options.width && options.height) {
var blob_ratio = blob.width / blob.height;
var ratio = options.width / options.height;
if (blob_ratio !== ratio) {
// Cut left and right
if (blob_ratio > ratio) {
blob = blob.imageAsCropped({
width: Math.round(blob.height * ratio),
});
}
// Cut top and bottom
else {
blob = blob.imageAsCropped({
height: Math.round(blob.width / ratio)
});
}
}
if (blob.width !== options.width || blob.height !== options.height) {
blob = blob.imageAsResized(options.width, options.height);
}
return blob
} else {
return blob.imageAsCropped(options);
}
}
function pixels(dimension) {
if (typeof dimension === 'number') {
return dimension * pixelFactor();
}
if (dimension.width) {
dimension.width = dimension.width * pixelFactor();
}
if (dimension.height) {
dimension.height = dimension.height * pixelFactor();
}
return dimension;
}
function pixelFactor() {
if (!_factor) {
_factor = 1;
if (Ti.Platform.name === 'iPhone OS') {
if (Ti.Platform.displayCaps.density === 'high') {
_factor = 2;
}
} else if (Ti.Platform.name === 'android') {
_factor = (Ti.Platform.displayCaps.dpi / 160);
}
}
return _factor;
}
exports.crop = crop;
exports.pixels = pixels;
exports.pixelFactor = pixelFactor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment