How To Resize Image In PHP

PHP
By bhagwatchouhan
How To Resize Image In PHP

This tutorial provides the steps required to get the actual width and height of an image and resize it using the new dimensions in PHP.

 

Image Width and Height

This section provides the code required to get the image dimensions. We can obtain the actual width and height of the given image as shown below.

// New Dimensions
$newWidth	= 500;
$newHeight	= 350;
// Min Dimensions - Keep the minimum of the new dimensions $minDimensions = true;
// Exact Dimensions - Keep the exact new dimensions, cropping is required $exactDimensions = true;
// Image Quality $quality = 100;
// Image Path $imagePath = __DIR__ . "/banner.jpg";
$newImagePath = __DIR__ . "/banner-new.jpg";
// Obtain Image
$image = null;
$extension = strtolower( strrchr( $imagePath, '.' ) );
switch( $extension ) {
// JPEG
case '.jpg':
case '.jpeg': {
$image = @imagecreatefromjpeg( $imagePath );
break; }
// GIF case '.gif': {
$image = @imagecreatefromgif( $imagePath );
break; }
// PNG case '.png': {
$image = @imagecreatefrompng( $imagePath );
break; } }
if( empty( $image ) ) {
die( "Provide a valid image." ); }
// Get the Image dimensions $width = imagesx( $image ); $height = imagesy( $image );

This is the easiest way to get the real width and height of the given image.

 

Optimal Width and Height

In this section, we will calculate the optimal width and height using the aspect ratio of the image dimensions. This will help us to maintain the aspect ratio without any distortion.

// Image Aspect Ratio
$aspectRatio = $width / $height;
// Get the optimal Image dimensions $optimalWidth = $aspectRatio >= 1 ? $newWidth : ($aspectRatio * $newHeight); $optimalHeight = $aspectRatio <= 1 ? $newHeight : ($newWidth / $aspectRatio);
// Keep the minimum of the new dimensions if( $minDimensions ) {
$heightRatio = $height / $newHeight;
$widthRatio = $width / $newWidth;
if( $heightRatio < $widthRatio ) {
$optimalRatio = $heightRatio; } else {
$optimalRatio = $widthRatio; }
$optimalWidth = $width / $optimalRatio;
$optimalHeight = $height / $optimalRatio; }

Now we can use the optimal width and height to maintain the same aspect ratio.

 

Resize Image

Now we will resize the given image using the new and optimal dimensions.

We can use the optimal dimensions to maintain the same aspect ratio, but it will consider the new width and height as the maximum or minimum values instead of creating the new image with exact and newer dimensions as shown below.

// Optimal Dimensions using New Dimensions
$tempImage = imagecreatetruecolor( $optimalWidth, $optimalHeight );
if( $extension == '.png' ) {
imagealphablending( $tempImage, false );
imagesavealpha( $tempImage, true );
$transparent = imagecolorallocatealpha( $tempImage, 255, 255, 255, 127 );
imagefilledrectangle( $tempImage, 0, 0, $optimalWidth, $optimalHeight, $transparent ); }
imagecopyresampled( $tempImage, $image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $width, $height );
// Save New Image
switch( $extension ) {
case '.jpg':
case '.jpeg': {
if( imagetypes() & IMG_JPG ) {
imagejpeg( $tempImage, $newImagePath, $quality ); }
break; } case '.gif': {
if( imagetypes() & IMG_GIF ) {
imagegif( $tempImage, $newImagePath ); }
break; } case '.png': {
$scale = round( ( $quality / 100 ) * 9 );
$invert = 9 - $scale;
if( imagetypes() & IMG_PNG ) {
imagepng( $tempImage, $newImagePath, $invert ); }
break; } } imagedestroy( $tempImage );

We might be required to crop the image to maintain the original aspect ratio, but create the new image having exact newer dimensions. We just need to resample the image twice by adding another call to the resample function just below the first call of the imagecopyresampled and before saving the image.

/// Crop to get exact dimensions
if( $exactDimensions ) {
// Crop Position
$cropStartX = ( $optimalWidth / 2) - ( $newWidth / 2 );
$cropStartY = ( $optimalHeight / 2) - ( $newHeight / 2 );
// Crop the temporary image
$crop = $tempImage;
// Crop from center
$tempImage = imagecreatetruecolor( $newWidth, $newHeight );
if( $extension == '.png' ) {
imagealphablending( $tempImage, false );
imagesavealpha( $tempImage, true );
$transparent = imagecolorallocatealpha( $tempImage, 255, 255, 255, 127 );
imagefilledrectangle( $tempImage, 0, 0, $newWidth, $newWidth, $transparent ); }
imagecopyresampled( $tempImage, $crop, 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight, $newWidth, $newHeight ); }

 

Summary

This tutorial provided the steps required to obtain the actual width and height of an image and resize it using the new dimensions maintaining the image quality. The code used in this tutorial is also available at GitHub.

You are most welcome to submit your comments either directly to Tutorials24x7 or Disqus. You may also be interested in How To Install XAMPP On Windows, Typed Properties In PHP, How To Autoload PHP Classes Using Composer and How To Send Email Using PHP.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan