PHP resize and crop centered-top image
I'm trying to resize (keeping the aspect ratio) and crop the excess of
image (outside the thumbnail limits), but doing that while crop x = center
and y = top.
I'm missing something here, but my final image fits inside the thumbnail
area, and not fill it and crop the excess. Hope someone can help me on
this.
This is my code, so far:
$image_width = 725;
$image_height = 409;
$width = 140;
$height = 160;
$thumbnail = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($thumbnail, 255, 255, 255);
imagefill($thumbnail, 0, 0, $white);
$width_ratio = $image_width/$width;
$height_ratio = $image_height/$height;
if ($width_ratio>$height_ratio) {
$dest_width=$width;
$dest_height=$image_height/$width_ratio;
}
else{
$dest_width=$image_width/$height_ratio;
$dest_height=$height;
}
$int_width = ($width - $dest_width)/2;
$int_height = ($height - $dest_height)/2;
imagecopyresampled($thumbnail, $original_image, $int_width, $int_height,
0, 0, $dest_width, $dest_height, $image_width, $image_height);
Thanks!
No comments:
Post a Comment