How to batch process the size of images using PHP

王林
Release: 2023-08-18 12:26:01
Original
1438 people have browsed it

How to batch process the size of images using PHP

How to use PHP to batch process image sizes

Size adjustment is one of the common needs for processing images, especially when processing images in batches, it can be easily achieved through PHP this function. This article will introduce how to use PHP to batch process the size of images and provide corresponding code examples.

  1. Installation and configuration environment

Before you start, make sure your server has PHP installed and the corresponding extension library, such as the GD library, enabled. In the php.ini file, find and enable the extension=gd extension library configuration item.

  1. Write a function to handle image size

First, we need to write a function to handle image size. The following is a sample code:

function resizeImage($srcImagePath, $newImagePath, $newWidth, $newHeight) {
    list($srcWidth, $srcHeight) = getimagesize($srcImagePath);
    $srcImage = imagecreatefromjpeg($srcImagePath);
    $newImage = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImage, $srcImage, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);
    imagejpeg($newImage, $newImagePath);
    imagedestroy($srcImage);
    imagedestroy($newImage);
}
Copy after login

In the above code, we first obtain the width and height of the original image, and then use the imagecreatefromjpeg function to create a source image that can be manipulated. Next, we use the imagecreatetruecolor function to create a new image with the specified $newWidth and $newHeight dimensions. Finally, the source image is copied to the new image through the imagecopyresampled function, and the new image is saved to the specified $newImagePath using the imagejpeg function.

  1. Batch processing of image sizes

Next, we can use the above function to batch process the size of images. The following is a sample code:

$sourceFolder = '/path/to/source/folder/';
$destinationFolder = '/path/to/destination/folder/';
$newWidth = 800;
$newHeight = 600;

$files = glob($sourceFolder . '*.jpg');

foreach ($files as $file) {
    $newFileName = $destinationFolder . basename($file);
    resizeImage($file, $newFileName, $newWidth, $newHeight);
}

echo '图片尺寸处理完成!';
Copy after login

In the above code, we first specify the paths of the source and destination folders, and the desired new size. Next, we use the glob function to obtain all .jpg format image files in the source folder. Then, iterate through each image file through a foreach loop, call the resizeImage function to process the size, and save it to the target folder.

Finally, we can run the above code in the browser to complete the batch processing of image sizes. Please note that the source and destination folders must exist and have appropriate read and write permissions.

Summary

This article introduces how to use PHP to batch process the size of images and provides corresponding code examples. With these simple codes, you can easily handle a large number of image sizes to suit different needs. Hope this article helps you!

The above is the detailed content of How to batch process the size of images using PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template