Search code examples
phpimage-uploadingresize-image

resize image while uploading in PHP


I have a file upload form where I upload only jpg, png and gif images. I resize the image if its width is more then 225 and height automatically fix with that width.

if($width > 225) {
    $newwidth = 225;
} 
else {
    $newwidth = $width; 
}
$newheight = ($height/$width)*$newwidth;

The above code fixes the width for me in case if image > 225. Now the problem is that the new height is according to the image width. I don't want the height to be more then 150. How can I fix it with out stretching the image?


Solution

  • Try adjusting the width in case of $newheight being greater than 150, calculating the proportion. Add this at the bottom:

    if ($newheight > 150) {
        $proportion = $newwidth/$newheight;
        $newheight = 150;
        $newwidth = 150*$proportion;
    }