@fmw42: yes, I want the second one in that image, from any image size to produce 120x90 image but without simply displaying the center of the image, but as you said (calculate and then crop).
It helps to know that there is no direct way to do that, I made it by calculation and this code works nicely after testing around 6 different image sizes/proportions, etc:
Code: Select all
if(!function_exists("crop_image")){
function crop_image($original_file, $destination_file, $new_width = 120, $new_height = 90){
list($original_width,$original_height) = getimagesize($original_file);
// first test if difference between old/new width are larger than diff betwern old/new height
$width_difference = $original_width/$new_width;
$height_difference = $original_height/$new_height;
// resize width first if width difference < height difference or if differences are the same (old/new image have same proportions);
// resize width first if width difference is smaller than height difference
if($width_difference<=$height_difference){
$resize_first = "width";
}
// resize height first if height difference is smaller than width difference
if($height_difference<$width_difference){
$resize_first = "height";
}
if($resize_first == "width"){
exec("convert $original_file -resize ".$new_width."x -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 $destination_file");
} else {
exec("convert $original_file -resize x".$new_height." -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 $destination_file");
}
}
}
// crop_image("ferrari.jpg","ferrari_new.jpg",120,90);
BTW: imagemagick has a much better image quality than GD, I really like it
