Home  >  Article  >  Backend Development  >  Examples of how to use PHP to change the size of website images

Examples of how to use PHP to change the size of website images

PHPz
PHPzOriginal
2023-04-04 14:29:22875browse

In website development, images often need to be processed. One of the common requirements is to modify the size of the image. In PHP, multiple ways are provided to modify the image size. This article will introduce in detail how to use PHP to change the size of website images.

1. GD library

The GD library is a function library of PHP. It provides a set of functions for processing images and functions for generating images. It can be used to create thumbnails, watermarks and other image processing operations. The following is how to use the GD library to change the size of the image:

1.1 First, you need to enable PHP's GD library support:

if (!function_exists('gd_info')) {
    echo 'The GD library is not installed.';
    exit;
}

1.2 Next, you need to open the image whose size you want to modify, and then obtain its Corresponding width and height:

$src_image = imagecreatefrompng('source.png') or die('Cannot locate image source.');
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);

1.3 Determine the dimensions that need to be changed and create a new target image:

$dst_width = 200;
$dst_height = 200;
$dst_image = imagecreatetruecolor($dst_width, $dst_height);

1.4 Resize the image:

imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);

1.5 Finally, Save the new image:

imagepng($dst_image, 'output.png', 9);

In the above code, the imagecopyresampled() function is used to scale the source image to the specified new size and store the result in the target image. Note that this function supports multiple image formats and you can choose the calling method according to your needs.

2. Imagick extension

Imagick is an excellent cross-platform image processing toolkit that can support a variety of commonly used image formats, including gif, jpeg, png, etc. In PHP, you can modify the image size by calling its related functions through the Imagick extension.

2.1 First, you need to create a new Imagick object and read the source image information:

$imagick = new Imagick('source.png');
$src_width = $imagick->getImageWidth();
$src_height = $imagick->getImageHeight();

2.2 Set the target image size, and then call the resizeImage() function to perform the scaling operation:

$dst_width = 200;
$dst_height = 200;
$imagick->resizeImage($dst_width, $dst_height, Imagick::FILTER_LANCZOS, 1);

2.3 Finally, save the new image:

$imagick->writeImage('output.png');

It should be noted that the Imagick extension has better performance than the GD library and supports more image formats. Therefore, it is recommended to use the Imagick extension when you need to handle large or complex image operations.

3. Summary

This article introduces how to use PHP’s GD library and Imagick extension to change the size of website images. The appropriate method can be selected according to specific needs. In actual development, there are many other image processing needs, such as cropping pictures, adding watermarks, etc., which can be achieved using these two powerful toolkits of PHP.

The above is the detailed content of Examples of how to use PHP to change the size of website images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn