0% found this document useful (0 votes)
137 views

Auto Resize Image With PHP: Here Is PHP Function Code

This document discusses a PHP function to automatically resize images. The function takes in an image file, desired height and width, and quality level as parameters. It gets the original image size, calculates the proper scaling, and resizes the image using imagecopyresampled. The resized image is then outputted as a JPEG with the correct header. Using this function avoids manually resizing images each time they need to be updated on a website.

Uploaded by

Alet Qondom
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Auto Resize Image With PHP: Here Is PHP Function Code

This document discusses a PHP function to automatically resize images. The function takes in an image file, desired height and width, and quality level as parameters. It gets the original image size, calculates the proper scaling, and resizes the image using imagecopyresampled. The resized image is then outputted as a JPEG with the correct header. Using this function avoids manually resizing images each time they need to be updated on a website.

Uploaded by

Alet Qondom
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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