PHP resize and crop centered-top image -
i'm trying resize (keeping aspect ratio) , crop excess of image (outside thumbnail limits), doing while crop x = center , y = top.
i'm missing here, final image fits inside thumbnail area, , not fill , crop excess. hope can me on this.
this code, far:
$image_width = 725; // not static, example $image_height = 409; // not static, example // image can wide or portrait $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!
your $image_width, $image_height, $width , $height static, means $width_ratio , $height_ratio same aswell (respectively: 5.1785714285714 , 2.55625, width ratio higher height ratio).
in case, block of code:
if ($width_ratio>$height_ratio) { $dest_width=$width; $dest_height=$image_height/$width_ratio; } else{ $dest_width=$image_width/$height_ratio; $dest_height=$height; } will run if , never run else - remove , leave just:
$dest_width=$image_width/$height_ratio; $dest_height=$height; and image cropped based on higher value - in case height resized accordingly new height , excess of width cut off.
hope looking for!
edit:
right script if cutting off edges equally. if want them cut top or left (depends on ratio), then:
remove part of code totally:
$int_width = ($width - $dest_width)/2; $int_height = ($height - $dest_height)/2; change if else condition mentioned before to:
if($width_ratio < $height_ratio) { $dest_width=$width; $dest_height=$image_height/$width_ratio; $int_width = ($width - $dest_width)/2; $int_height = 0; } else { $dest_width=$image_width/$height_ratio; $dest_height=$height; $int_width = 0; $int_height = ($height - $dest_height)/2; } edit 2
horizontal cut off equally, vertical top - wanted:
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 = 0;
Comments
Post a Comment