public
Created

Positional Cropping for Wordpress Timthumb Alternative

  • Download Gist
Positional Cropping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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 );
}
// positional cropping!
if( strpos($crop,',') > 0 ) {
$crop = explode(',',$crop);
$src_x = $crop[0];
$src_y = $crop[1];
}elseif( $crop !== true ) {
if (strpos ($crop, 't') !== false) $src_y = 0;
if (strpos ($crop, 'b') !== false) $src_y = $orig_height - $src_h;
if (strpos ($crop, 'l') !== false) $src_x = 0;
if (strpos ($crop, 'r') !== false) $src_x = $orig_width - $src_w;
}
}

Awesome! Going to give this a try tomorrow. Sorry I haven't been merging requests, been crazy busy!

You should consider to add the cropping direction to the file name too, to ensure that it is updated if the cropping direction has changed on the image.

-> image-300x200.jpg

to

-> image-300x200-tl.jpg

Otherwise the image is not recreated.

Cheers

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.