esedic
6/13/2012 - 10:09 PM

Using JImage to create square profile pictures in two sizes, regardless of the original's orientation

Using JImage to create square profile pictures in two sizes, regardless of the original's orientation

<?php

$image = new JImage('/path/to/original.jpg');

$height = $image->getHeight();
$width = $image->getWidth();

if ($height > $width) {
	$crop_square = $width;
	$crop_top = ($height - $width) / 2;
	$crop_left = 0;
} else {
	$crop_square = $height;
	$crop_top = 0;
	$crop_left = ($width - $height) / 2;
}

$image = $image->crop($crop_square, $crop_square, $crop_left, $crop_top, false);

$small = $image->resize(48, 48);
$large = $image->resize(160, 160);

$small->toFile('/path/to/small.jpg');
$large->toFile('/path/to/large.jpg');