Skip to content

Instantly share code, notes, and snippets.

@styledev
Created February 26, 2013 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save styledev/5042132 to your computer and use it in GitHub Desktop.
Save styledev/5042132 to your computer and use it in GitHub Desktop.
Positional Cropping for Wordpress Timthumb Alternative
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;
}
}
@MatthewRuddy
Copy link

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

@doitmax
Copy link

doitmax commented Mar 29, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment