Resizing images in PHP
In this tutorial article, we will discuss about resizing images in PHP.
Load the image before resizing
Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents()
different from using a function like to get the contents of an image file. To load a file, we need to use functions like imagecreatefromjpeg()
, imagecreatefrompng()
and imagecreatefromgif()
. Depending on the type of image we'll be resizing, we'll use different functions accordingly.
PHPgetimagesize()
After loading the image, we use getimagesize()
to calculate the width, height, and type of the input image. This function returns a list of items where the width and height of the image are stored at index 0 and 1 respectively, and IMAGETYPE_XXX
the constant is stored at index 2. We will use the value of this constant returned to find out what type of image to use and what function to use.
<?php
function load_image($filename, $type) {
$new = 'new34.jpeg';
if( $type == IMAGETYPE_JPEG ) {
$image = imagecreatefromjpeg($filename);
echo "here is jpeg output:";
imagejpeg($image, $new);
}
elseif( $type == IMAGETYPE_PNG ) {
$image = imagecreatefrompng($filename);
echo "here is png output:";
imagepng($image,$new);
}
elseif( $type == IMAGETYPE_GIF ) {
$image = imagecreatefromgif($filename);
echo "here is gif output:";
imagejpeg($image, $new);
}
return $new;
}
$filename = "panda.jpeg";
list($width, $height,$type) = getimagesize($filename);
echo "Width:" , $width,"\n";
echo "Height:", $height,"\n";
echo "Type:", $type, "\n";
$old_image = load_image($filename, $type);
?>
Input image:
Output:
Width:225
Height:225
Type:2
here is jpeg output:
Output image:
Both are the same image because we only load the image and calculate the size of the original image in this section.
PHPimagecopyresized()
imagecopyresized()
Gets a rectangular area of width and height from (src_x,src_y)
at position and places it at position . It is an inbuilt function in PHP.src_image
src_w
src_h
(dst_x,dst_y) 处的目标图像的矩形区域中
<?php
// File and new size
$filename = 'panda.jpeg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height, $type) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$type = $list[2];
echo $type;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb,'new.jpeg');
?>
Input image:
Output image:
As mentioned above, imagecopyresize()
the function accepts ten parameters, which are listed below.
parameter | explain |
---|---|
$dst_image |
Target Image Resource |
$src_image |
Source Image Resources |
$dst_x |
The x coordinate of the target point |
$dst_y |
The y coordinate of the target point |
$src_x |
The x coordinate of the source point |
$src_y |
The y coordinate of the source point |
$dst_w |
Destination Width |
$dst_h |
Destination altitude |
$src_w |
Source Width |
$src_h |
Source Height |
It returns a Boolean value of TRUE on success and FALSE on failure.
PHPimagecopyresampled()
imagecopyresampled()
Copies a rectangular portion of one image to another with different, seamlessly interpolated pixel values to reduce the image size while maintaining a high degree of clarity.
It functions similarly imagecopyresized()
to the resize function, with the added benefit of upsampling the image in addition to resizing it.
<?php
// The file
$filename = 'panda.jpeg';
$percent = 2;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, 'resampled1.jpeg', 100);
?>
Input image:
Output image:
The function imagecopyresampled()
accepts ten different parameters.
parameter | explain |
---|---|
$image_p |
It determines the target image. |
$image |
It determines the source image. |
$x_dest |
It determines the x coordinate of the destination image. |
$y_dest |
It determines the y coordinate of the destination image. |
$x_src |
It determines the x coordinate of the source image. |
$y_src |
It determines the y coordinate of the source image. |
$wid_dest |
It determines the width of the new image. |
$hei_dest |
It determines the height of the new image. |
$wid_src |
It determines the width of the old image. |
$wid_src |
It determines the height of the old image. |
PHPimagescale()
You specify the size instead of defining the width or height of the latest image. If you want the new image to be half the size of the first image, set the size to 0.8. Here is sample code that scales an image by a given factor while preserving its proportions.
We scale image()
multiply the original width and height of the image by the ratio specified in the function.
Here is an example of it:
<?php
function load_image($filename, $type) {
if( $type == IMAGETYPE_JPEG ) {
$image = imagecreatefromjpeg($filename);
}
elseif( $type == IMAGETYPE_PNG ) {
$image = imagecreatefrompng($filename);
}
elseif( $type == IMAGETYPE_GIF ) {
$image = imagecreatefromgif($filename);
}
return $image;
}
function scale_image($scale, $image, $width, $height) {
$new_width = $width * $scale;
$new_height = $height * $scale;
return resize_image($new_width, $new_height, $image, $width, $height);
}
function resize_image($new_width, $new_height, $image, $width, $height) {
$image12 = 'hello.jpeg';
$new_imag = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_imag, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return imagejpeg($new_imag, $image12);
}
$filename = "panda.jpeg";
list($width, $height, $type) = getimagesize($filename);
$old_image = load_image($filename, $type);
$image_scaled = scale_image(0.8, $old_image, $width, $height);
?>
Input image:
Output image:
Apart from this, PHP has a built-in function that allows you to scale images to a specified width and height.
See the sample code.
<?php
$out_image = "sca.jpeg";
$image_name ="panda.jpeg";
// Load image file
$image = imagecreatefromjpeg($image_name);
// Use imagescale() function to scale the image
$img = imagescale( $image, 500, 400 );
header("Content-type: image/jpeg");
imagejpeg($img,$out_image);
?>
Input image:
Output_Image:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of
Displays PHP configuration information on localhost
Publish Date:2025/04/13 Views:107 Category:PHP
-
phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404