Image sizes
Design and Skin Customization
This seems to work for all images I've tried:
// Check for an aspect ratio crop
if (strpos($_post['crop'], ':')) {
// // Our aspect ratio crop will come in like 4x3 or 16x9, etc.
// list($w, $h) = explode(':', $_post['crop']);
// $w = (int) $w;
// $h = (int) $h;
// // What is our existing ratio?
// if ($w > $h) {
// $chunk = round($src_height / $w);
// $src_y_offset = round(($src_height - ($h * $chunk)) / 2);
// $new_width = $_post['width'];
// $new_height = round(($_post['width'] / $w) * $h);
// $_post['height'] = $new_height;
// $src_height -= round($src_y_offset * 2);
// }
// else {
// $chunk = round($src_width / $h);
// $src_x_offset = round(($src_width - ($w * $chunk)) / 2);
// $new_height = $_post['width'];
// $new_width = round(($_post['width'] / $h) * $w);
// $_post['width'] = $new_width;
// $_post['height'] = $new_height;
// $src_width -= round($src_x_offset * 2);
// }
// Our aspect ratio crop will come in like 4:3 or 16:9, etc.
$crop = array_filter(explode(':', $_post['crop']));
$ratio = array(0 => $src_width / $src_height, 1 => $crop[0] / $crop[1]);
$w = $crop[0];
$h = $crop[1];
// What is our existing ratio?
if ($w > $h) {
if ($ratio[0] > $ratio[1]) {
$width = $src_height * ($w / $h);
$src_x_offset = ($src_width - $width) / 2;
$src_width = $width;
}
else if ($ratio[0] < $ratio[1]) {
$height = $src_width / ($w / $h);
$src_y_offset = ($src_height - $height) / 2;
$src_height = $height;
}
$new_width = $_post['width'];
$new_height = round(($_post['width'] / $w) * $h);
$_post['height'] = $new_height;
}
else {
if ($ratio[0] > $ratio[1]) {
$width = $src_height * ($w / $h);
$src_x_offset = ($src_width - $width) / 2;
$src_width = $width;
}
else if ($ratio[0] < $ratio[1]) {
$height = $src_width / ($w / $h);
$src_y_offset = ($src_height - $height) / 2;
$src_height = $height;
}
$new_height = $_post['width'];
$new_width = round(($_post['width'] / $h) * $w);
$_post['width'] = $new_width;
$_post['height'] = $new_height;
}
}
else {
// Default auto height/width