Friday, July 5, 2024
HomeLanguagesPhpHow to Resize JPEG Image in PHP ?

How to Resize JPEG Image in PHP ?

Why do we need to resize images?
In a website, often, we need to scale an image to fit a particular section. Sometimes, it becomes necessary to scale down any image of random size to fit a cover photo section or profile picture section. Also, we need to show a thumbnail of a bigger image. In those cases, it’s not feasible to manually resize the image always.

One way to solve the above problem is to use the following method where we need to just set the width and height attributes on the image tag in our HTML.

<img src="check.jpg" height="100" width="100" alt="Image resize">

The problem with this is that the entire image is downloaded from the server and it is shown in the decreased size within the browser. This means the same amount of bandwidth is required to show the image in the smaller size that would have been required in case of the original size.

Using PHP to resize the image on server end: We will be henceforth using PHP to decrease the image dimension and render it so that a smaller size is only downloaded on the client end and not the original image. To achieve this we will be using the imagecopyresampled() function in PHP.

The imagecopyresampled() function is used to copy and resize an image or part of the image with resampling.

Syntax:

bool imagecopyresampled( resource $dst_image, resource $src_image,
                  int $dst_x, int $dst_y, int $src_x, int $src_y,
                  int $dst_w, int $dst_h, int $src_w, int $src_h )

Parameters: This function accepts a rectangular area from $src_image of width $src_w and height $src_h at position ($src_x, $src_y) and place it in a rectangular area of $dst_image of width $dst_w and height $dst_h at position ($dst_x, $dst_y).

Example: This example uses imagecopyresampled() function to resize the image.




<?php
  
// The file concerned
$filename = 'check.jpg';
  
// Maximum width and height
$width = 100;
$height = 100;
  
// File type
header('Content-Type: image/jpg');
  
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
  
$ratio_orig = $width_orig/$height_orig;
  
if ($width/$height > $ratio_orig) {
    $width = $height*$ratio_orig;
} else {
    $height = $width/$ratio_orig;
}
  
// Resampling the image 
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
  
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
        $width, $height, $width_orig, $height_orig);
  
// Display of output image
imagejpeg($image_p, null, 100);
  
?>


Output:

  • Original Image:
    Original image
  • Output Image:
    Final image

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments