Reputation: 1024
Well, In my site I am trying to crop image with following php function :
function image_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
$backgroundColor = imagecolorallocate($tci, 255, 255, 255);
imagefill($tci, 0, 0, $backgroundColor);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
But it's not working what I exactly want.
For example : I have a image which size is : 720 x 478
. When I resize it with this function (I can set the width and height in function call) it's cropping the image based on width. For e.g. I am calling it like that:
image_resize($file_destination, "../users_avator/ppic_$file_new_name", 200, 200, $file_ext);
It's saved the image with 200 x 132 size. but which I don't want
I want to crop the image exactly 200 x 200 size but without cutting any portion of the image. Like :
I see a website called cozymeal.com When I upload the same image in the profile section which size is 720 x 478
it's magically crop the image size to 200 x 200 without cutting any portion of the image.
Original Image :
My cropping result :
Cozymeal cropping result :
My question is How can I fix my function / crop any images like this cozymeal.com cropping system ? I mean custom size without loosing any image portion.
Upvotes: 1
Views: 68
Reputation: 121
Funny, I'm actually working at Cozymeal. We get this feature with our CDN cloudinary but you can use this library if you want to crop images as you want: https://github.com/avalanche123/Imagine
All the doc is inside the plugin
If you don't feel comfortable with this library you can use one of the following: https://github.com/ziadoz/awesome-php#imagery
Good luck :)
Upvotes: 1