Wordpress-Timthumb-alternative
Wordpress-Timthumb-alternative copied to clipboard
Setting crop to false squashes/stretches images instead of the desired resize result
If I pass an image through this script with crop set to false, instead of simply resizing the image, it nastily stretches/squashes the image into the specified width/height.
Example: original image is 100x500.
I want the script to take the values width:50, height:50, crop:false and return me an image that's 10x50. What is actually does is returns that portrait image squashed into a 50x50 space.
Right, I managed to sort this by hacking the code. If anyone finds this thread with the same issue, this should help:
Replace this:
if ( $crop ) {
$cmp_x = $orig_width / $dest_width;
$cmp_y = $orig_height / $dest_height;
// Calculate x or y coordinate, and width or height of source
if ( $cmp_x > $cmp_y ) {
$src_w = round( $orig_width / $cmp_x * $cmp_y );
$src_x = round( ( $orig_width - ( $orig_width / $cmp_x * $cmp_y ) ) / 2 );
}
else if ( $cmp_y > $cmp_x ) {
$src_h = round( $orig_height / $cmp_y * $cmp_x );
$src_y = round( ( $orig_height - ( $orig_height / $cmp_y * $cmp_x ) ) / 2 );
}
}
// Time to crop the image!
$editor->crop( $src_x, $src_y, $src_w, $src_h, $dest_width, $dest_height );
With this:
if ( $crop ) {
$cmp_x = $orig_width / $dest_width;
$cmp_y = $orig_height / $dest_height;
// Calculate x or y coordinate, and width or height of source
if ( $cmp_x > $cmp_y ) {
$src_w = round( $orig_width / $cmp_x * $cmp_y );
$src_x = round( ( $orig_width - ( $orig_width / $cmp_x * $cmp_y ) ) / 2 );
}
else if ( $cmp_y > $cmp_x ) {
$src_h = round( $orig_height / $cmp_y * $cmp_x );
$src_y = round( ( $orig_height - ( $orig_height / $cmp_y * $cmp_x ) ) / 2 );
}
// Time to crop the image!
$editor->crop( $src_x, $src_y, $src_w, $src_h, $dest_width, $dest_height );
} else {
// Time to resize the image!
$editor->resize( $dest_width, $dest_height, false );
}