Howdy:
I have a page where a user can upload an image and the script will take that image and create an icon that is 100x100. The problem I'm having is that unfortunately, not all images being uploaded are the same. I went ahead and coded the right ratio so the images are resampled at the correct ratio, but this leaves me with the dilema that the icons are no longer 100x100.
What I want to do is, take the resampled image and crop from either the top or the bottom if the image is taller than wider, or crop from left and right if the image is wider than taller. I want it to end up being 100x100 (even if the image looses a bit on the sides).
I don't really understand ImageCopyResampled, and the manual is leaving me a bit confused when I want to crop the image. Is this done by where the script currently has 0,0,0,0? Can anyone lend me a hand?
This is my original code:
// Get image just moved after upload
$originalimage_path = "$icon_folder/$icon_name_form"; // where the image to be resized is located
$destination_path = "icons/icon_".$_SESSION['login_un'].".jpg";
// Set maximum image width and height
$max_width = 100;
$max_height = 100;
// Get the submitted image information
$img = imagecreatefromjpeg($originalimage_path);
$originalwidth= imagesx($img);
$originalheight= imagesy($img);
// If the picture is higher than wide, we set the height of the thumb to 100 pixels.
if ($originalwidth < $originalheight) {
$icon_w = (($max_width)*($originalheight/$originalwidth));
$icon_h = $max_height;
}
if ($originalwidth > $originalheight) {
$icon_w = $max_width;
$icon_h = $max_height($originalwidth/$originalheight);
}
if ($originalwidth == $originalheight) {
$icon_h = $max_height;
$icon_w = $max_width;
}
$actual_icon = ImageCreateTrueColor($icon_w,$icon_h);
ImageCopyResampled($actual_icon, $img, 0,0,0,0,$icon_w,$icon_h,$originalwidth,$originalheight);
// Now copy the image to destiny
imagejpeg($actual_icon,$destination_path,90);
imagedestroy($img);
// Once done, insert the filename of the icon into the database
$icon_filename = "icon".$SESSION['login_un'].".jpg";