Scale Image Using PHP and Maintaining Aspect Ratio
I had written a peice of code like this for another project I've done. I've copied it below, might need a bit of tinkering! (It does required the GD library)
These are the parameters it needs:
$image_name - Name of the image which is uploaded
$new_width - Width of the resized photo (maximum)
$new_height - Height of the resized photo (maximum)
$uploadDir - Directory of the original image
$moveToDir - Directory to save the resized image
It will scale down or up an image to the maximum width or height
function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $image_name;
$mime = getimagesize($path);
if($mime['mime']=='image/png') {
$src_img = imagecreatefrompng($path);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$src_img = imagecreatefromjpeg($path);
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $new_width;
$thumb_h = $old_y*($new_height/$old_x);
}
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
if($old_x == $old_y)
{
$thumb_w = $new_width;
$thumb_h = $new_height;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $image_name;
if($mime['mime']=='image/png') {
$result = imagepng($dst_img,$new_thumb_loc,8);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$result = imagejpeg($dst_img,$new_thumb_loc,80);
}
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
I was thinking about how to achieve this and i came with a pretty nice solution that works in any case... Lets say you want to resize heavy images that users upload to your site but you need it to maintain the ratio. So i came up with this :
<?php
// File
$filename = 'test.jpg';
// Get sizes
list($width, $height) = getimagesize($filename);
//obtain ratio
$imageratio = $width/$height;
if($imageratio >= 1){
$newwidth = 600;
$newheight = 600 / $imageratio;
}
else{
$newidth = 400;
$newheight = 400 / $imageratio;
};
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width,
$height);
// Output
imagejpeg($thumb, "img/test.jpg");
imagedestroy();
?>
In this case if width is bigger than height, i wanted the width to be 600px and if the height was bigger than the width, i wanted the width to be 400px
Formule is wrong for keeping aspect ratio. It should be: original height / original width x new width = new height
function createThumbnail($imageName,$newWidth,$newHeight,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $imageName;
$mime = getimagesize($path);
if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $old_y/$old_x*$newWidth;
}
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
if($old_x == $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $newHeight;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $imageName;
if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); }
if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
Actually the accepted solution it is not the correct solution. The reason is simple: there will be cases when the ratio of the source image and the ratio of the destination image will be different. Any calculation should reflect this difference.
Please note the relevant lines from the example given on PHP.net website:
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
The full example may be found here: http://php.net/manual/en/function.imagecopyresampled.php
There are other answers (with examples) on stackoverflow to similar questions (the same question formulated in a different manner) that suffer of the same problem.
Example:
Let's say we have an image of 1630 x 2400 pixels that we want to be auto resized keeping the aspect ratio to 160 x 240. Let's do some math taking the accepted solution:
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
height = 240 width = 1630 * ( 160/2400 ) = 1630 * 0.0666666666666667 = 108.6666666666667 108.6 x 240 it's not the correct solution.
The next solution proposed is the following:
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
height = 240; width = 1630 / 2400 * 240 = 163 It is better (as it maintain the aspect ratio), but it exceeded the maximum accepted width.
Both fail.
We do the math according to the solution proposed by PHP.net: width = 160 height = 160/(1630 / 2400) = 160/0.6791666666666667 = 235.5828220858896 (the else clause). 160 x 236 (rounded) is the correct answer.