Almost every website uses thumbnail or resized images with content in form of featured image. Every website showing thumbnails in product gallery, portfolio, featured section, etc. So I have created a function to resize image in php. With this you can resize images easily using PHP and the GD library. If you’re looking for the same to resize uploaded images you can try it.
In this article I will show you how to use this and how the resize function works. It allows you to take care of image quality, image ratio and it works well with png and gif images. So resizeImage() function is as below:
function resizeImage($file, $destination, $size = null, $imageNoRatio = false, $quality = NULL){ if (is_null($quality)) $quality = 90; $resizedImage = $destination; // $maxW = null; $maxH = null; $maxScale = null; // -- format variables $output = strtolower(pathinfo( $file, PATHINFO_EXTENSION)); if (is_array($size)) { $maxW = intval($size[0]); $maxH = intval($size[1]); } else { $maxScale = intval($size); } if (is_numeric($quality)) { $quality = intval($quality); if ($quality > 100 || $quality < 1) { $quality = 90; } } else { $quality = 90; } // -- get some information about the file $uploadSize = getimagesize($file); $uploadWidth = $uploadSize[0]; $uploadHeight = $uploadSize[1]; $uploadType = $uploadSize[2]; switch ($output) { case 'gif': $srcImg = imagecreatefromgif($file); break; case 'jpg': case 'jpeg': $srcImg = imagecreatefromjpeg($file); break; case 'png': $srcImg = imagecreatefrompng($file); break; } // -- determine new size // According to the image size and ratio it will keep the proportion of image if(!$imageNoRatio){ if($maxScale != null){ if ($uploadWidth > $maxScale || $uploadHeight > $maxScale) { if ($uploadWidth > $uploadHeight) { $newX = $maxScale; $newY = ($uploadHeight*$newX)/$uploadWidth; } else if ($uploadWidth < $uploadHeight) { $newY = $maxScale; $newX = ($newY*$uploadWidth)/$uploadHeight; } else if ($uploadWidth == $uploadHeight) { $newX = $newY = $maxScale; } } } else if($maxW != null && $maxH != null){ if($uploadHeight < $maxW && $uploadWidth < $maxW) { $tnWidth = $uploadWidth; $tnHeight = $uploadHeight; } else if($uploadWidth > $maxW){ $tnWidth = $maxW; $tnHeight = $maxW*($uploadHeight/$uploadWidth); } else if($uploadHeight > $maxH){ $tnHeight = $maxH; $tnWidth = $maxH*($uploadWidth/$uploadHeight); } if($tnHeight > $maxH){ $tnHeight = $maxH; $tnWidth = $maxH*($uploadWidth/$uploadHeight); if($tnWidth > $maxW){ $tnHeight = $maxW*($tnHeight/$tnWidth); $tnWidth = $maxW; } } if($tnWidth > $maxW){ $tnWidth = $maxW; $tnHeight = $maxW*($uploadHeight/$uploadWidth); if($tnHeight > $maxH){ $tnWidth = $maxH*($tnWidth/$tnHeight); $tnHeight = $maxH; } } $newX = $tnWidth; $newY = $tnHeight; }else{ $newX = $uploadWidth; $newY = $uploadHeight; } }else{ // Force to resize according to given size $newX = $maxW; $newY = $maxH; } $dstImg = @imagecreatetruecolor($newX, $newY); // for png and gif images if(($output == 'gif') || ($output == 'png')){ $trnprt_indx = imagecolortransparent($srcImg); // If we have a specific transparent color if($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($srcImg, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($dstImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($dstImg, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($dstImg, $trnprt_indx); } // Always make a transparent background color for PNGs that don't have one allocated already elseif ($output == 'png') { // Turn off transparency blending (temporarily) imagealphablending($dstImg, false); // Create a new transparent color for image $color = imagecolorallocatealpha($dstImg, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($dstImg, 0, 0, $color); // Restore transparency blending imagesavealpha($dstImg, true); } } imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newX, $newY, $uploadWidth, $uploadHeight); // -- save the resized image to destination switch ($output) { case 'jpg': case 'jpeg': $write = imagejpeg($dstImg, $resizedImage, $quality); break; case 'png': $write = imagepng($dstImg, $resizedImage, round(($quality / 10) - 1)); break; case 'gif': $write = imagegif($dstImg, $resizedImage, $quality); break; } // -- clean up imagedestroy($dstImg); return pathinfo ( $resizedImage , PATHINFO_BASENAME ); }
Now let’s take a simple image upload program to understand the usage of the function.
<form name="imageresize" method="post" enctype="multipart/form-data"> <input type="file" name="imgfile" /> <input type="submit" value="Resize" name="submit" /> </form>
Now save the uploaded image to the destination as below
if(!$_FILES['imgfile']['error']){ $ext = pathinfo ( $_FILES['imgfile']['name'], PATHINFO_EXTENSION); $filename = md5(time()).'.'.$ext; $destination = $_SERVER['DOCUMENT_ROOT'].'/test/uploads/'.$filename; if (move_uploaded_file($_FILES['imgfile']['tmp_name'], $destination)) { // resize image with imageResize() function } }
Usage:
To Resize image you need to pass source path and destination path of the image and desired size for the thumbnail. You can provide both width and height as an array or only height or width as variable and it will resize image and will keep the ratio of the image.
$width = 100; $height = 100; resizeImage($source, $destination, array($width, $height)); resizeImage($source, $destination, $width ); resizeImage($source, $destination, $height);
If you don’t want the image ratio, just pass $imageNoRatio = true as below
resizeImage($source, $destination, array($width, $height), true); resizeImage($source, $destination, $width, true );
You can also provide desired quality of the image. I have taken default 90 that is sufficient. So you can leave it to the function to determine the quality. As a result the function provides you name of resized image and you can use it as you want.