How to resize image in PHP

In this article, we will discuss how to resize an image in PHP. Image resizing is useful when you have a bigger or smaller size window as compared to the image.

Image resizing requires information about the old height and width of an image to generate an image of new width and height.

The below image is considered as source file throughout the article.

Before resizing image in PHP

Resize image with predefined function in PHP

  • In PHP, the imagecopyresized() method resize an image.

Syntax:-

imagecopyresized(destination,source,x1,y1,x2,y2,new_width,new_height,old_width,old_height);

The below script shows the method for resizing,

<?php
// File and new size
$fname = 'extreme-snowboarding-1920x1080-winter-jump-snow-10925.jpg';


//set $percent value <1 for shrinking and $percent value >1 for expanding
$per = 0.3;

//Check Content type
header('Content-Type: image/jpeg');

//Generate new size parameters
list($width, $height) = getimagesize($fname);
$new_w = $width * $per;
$new_h = $height * $per;

// Load image
$output = imagecreatetruecolor($new_w, $new_h);
$source = imagecreatefromjpeg($fname);

// Resize the source image to new size
imagecopyresized($output, $source, 0, 0, 0, 0, $new_w, $new_h, $width, $height);

//Display Output 
imagejpeg($output);
?>

Output:-

How to Resize image in PHP

  • In PHP, there is one more method i.e imagecopyresampled() is used to resize an image.

Syntax:-

imagecopyresampled(destination,source,x1,y1,x2,y2,new_width,new_height,old_width,old_height);

The below script shows the method for resizing,

<html>
<body>
<form name="ImgResize" action="phpresize.php" method="post" enctype="multipart/form-data">
<input type="file" name="img" /> 
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Output:-

Upload button to resize an image in PHP

phpresize.php

<?php
if(isset($_POST["submit"])) 
{
  if(is_array($_FILES)) 
  {
  $file = $_FILES['img']['tmp_name']; 
  $image_prop = getimagesize($file);
  $image_type = $image_prop[2]; 
  if( $image_type == IMAGETYPE_JPEG ) 
    {   
    $image_resource_id = imagecreatefromjpeg($file);  
    $layer = resize($image_id,$image_prop[0],$image_prop[1]);
    imagejpeg($layer,$_FILES['img']['name'] . "_thump.jpg");
    }
  elseif( $image_type == IMAGETYPE_GIF )  
    {  
    $image_id = imagecreatefromgif($file);
    $layer = resize($image_id,$image_prop[0],$image_prop[1]);
    imagegif($layer,$_FILES['img']['name'] . "_thump.gif");
    }
  elseif( $image_type == IMAGETYPE_PNG )
    {
    $image_id = imagecreatefrompng($file); 
    $layer = resize($image_id,$image_prop[0],$image_prop[1]);
    imagepng($layer,$_FILES['img']['name'] . "_thump.png");
    }
  }
}
function resize($image_id,$width,$height) 
{
$new_width =$width * 0.5;
$new_height =$height * 0.5;
$layer=imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($layer,$image_id,0,0,0,0,$new_width,$new_height, $width,$height);
return $layer;
}
?>

Output:-

Final image

In this way, we can resize an image. If you have any doubts about this article, leave a comment below.

See also,

2 responses to “How to resize image in PHP”

  1. Bartosz Czaplewski says:

    Where is imagecreatetruecolor method and imagecopyresampled method? GG rly. helpful article!

  2. Mark says:

    On line 11:

    $image_resource_id

    should be:

    $image_id

Leave a Reply

Your email address will not be published. Required fields are marked *