I want the script to crop from bottom and top if the image is taller than wider and if the image is wider than taller, to crop from left and right. In essence, the image should end up being 100x100. The whole script works well except for that part.
For example, when I upload an image that is 500x375, it resamples it to 133x100. This works great. However, when it actually crops it down to 100x100 it chops the image by the right-hand side, not equally from both sides. Can anyone help?
EDIT: By the way, I looked through the manual for ImageCopyResampled, but I was confused as to how the cropping aspect worked.
// 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 taller than wider, we set the height of the thumb to 100 pixels.
if ($originalwidth < $originalheight) {
$icon_w = $max_width;
$icon_h = round(($max_height * $originalheight) / $originalwidth);
$tocrop_w = 0;
$tocrop_h = round(($icon_h-$max_height)/2);
}
if ($originalwidth > $originalheight) {
$icon_w = round(($max_width * $originalwidth) / $originalheight);
$icon_h = $max_height;
$tocrop_w = round(($icon_w-$max_width)/2);
$tocrop_h = 0;
}
if ($originalwidth == $originalheight) {
$icon_w = $max_width;
$icon_h = $max_height;
$tocrop_w = 0;
$tocrop_h = 0;
}
$actual_icon = ImageCreateTrueColor($max_width,$max_height);
ImageCopyResampled($actual_icon, $img, 0,0,$tocrop_w,$tocrop_h,$icon_w,$icon_h,$originalwidth,$originalheight);
// Now copy the image to destiny
imagejpeg($actual_icon,$destination_path,90);