Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Auto Resize image With PHP

Most of time, It makes me angry. Open image, resize and then upload again. If you believe you fixed
the size issue, but after updating web site, you can see, product image is overflow. Download all image
again, and resize it to true size and upload it again.

Then I wrote this function for along time ago. It is loading image and resizing what you want with
picture quality. So, you can reduce image size and you can fix your image overflow issue.

Here is PHP function code;
<?php/**
* @author PCoder
* @copyright 2009
* Auto Resized Image / Thumb
*/ function resize($file, $height, $width, $quality)
{
# Get image width/height to resize properly
$size = GetImageSize($file);
$img_width = $size[0];
$img_height = $size[1];

# In this part, we are calculating proper size for new image
if ($img_width > $img_height) {
$new_y = ceil(($width * $img_height) / $img_width);
$new_x = $height;
} else {
$new_y = ceil(($height * $img_width) / $img_height);
$new_x = $width;
}

# Create image with properly size for new sized image
$image_p = imagecreatetruecolor($new_x, $new_y);
$image = imagecreatefromjpeg($file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_x, $new_y,
$img_width, $img_height);

# finally, outout image file with correct header mime-type
imagejpeg($image_p, null, $quality);
Header("Content-type: image/jpeg");
}?>


Just use this function with your var;
<?php resize("logo.jpg",150,150,90);?>

You might also like