Created
February 26, 2013 20:58
-
-
Save styledev/5042132 to your computer and use it in GitHub Desktop.
Positional Cropping for Wordpress Timthumb Alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
Awesome! Going to give this a try tomorrow. Sorry I haven't been merging requests, been crazy busy!