Home > Backend Development > PHP Tutorial > How to batch compress image file size using PHP

How to batch compress image file size using PHP

WBOY
Release: 2023-08-25 14:28:02
Original
1826 people have browsed it

How to batch compress image file size using PHP

How to use PHP to batch compress image file sizes

Introduction:
With the development of the Internet, pictures are becoming more and more common in our daily lives. However, large and small picture files also bring storage and transmission problems. In order to reduce the size of image files and improve the loading speed of the website, we can use PHP to batch compress image file sizes. This article will introduce how to use PHP to batch compress image file sizes and provide relevant code examples.

Steps:

  1. Get the list of image files:
    First, we need to get the list of image files to be compressed. This can be achieved by traversing the image files in the folder, or reading the image path stored in the database. The following is a sample code snippet for obtaining all image files in a folder:
$directory = 'images/';
$fileList = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
Copy after login
  1. Define the target size and compression quality of compressed images:
    Before performing image compression , we need to define the size and compression quality of the target image. Usually, we can choose to reduce the size of the picture to half of the original size and set the compression quality to 80-90. The following is a sample code snippet for defining the target size and compression quality:
$targetWidth = 200; // 目标宽度
$targetQuality = 80; // 目标质量
Copy after login
  1. Perform image compression:
    Next, we can use PHP's GD library for image compression . The GD library provides a wealth of image processing functions that can help us process images. The following is a sample code snippet for compressing a single image:
foreach($fileList as $file) {
    $imageInfo = getimagesize($file);
    
    $sourceWidth = $imageInfo[0]; // 原始宽度
    $sourceHeight = $imageInfo[1]; // 原始高度
    
    $sourceImage = imagecreatefromjpeg($file); // 根据文件类型选择对应的函数
    
    $targetHeight = round($sourceHeight * $targetWidth / $sourceWidth); // 计算目标高度
    
    $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); // 创建目标图像
    
    imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight); // 图像复制和缩放
    
    imagejpeg($targetImage, $file, $targetQuality); // 保存压缩后的图片
    
    imagedestroy($sourceImage); // 释放资源
    imagedestroy($targetImage);
}
Copy after login
  1. Full code reference:
    The following is a complete code sample for compressing image file sizes in batches:
$directory = 'images/';
$fileList = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$targetWidth = 200; // 目标宽度
$targetQuality = 80; // 目标质量

foreach($fileList as $file) {
    $imageInfo = getimagesize($file);
    
    $sourceWidth = $imageInfo[0]; // 原始宽度
    $sourceHeight = $imageInfo[1]; // 原始高度
    
    $sourceImage = imagecreatefromjpeg($file); // 根据文件类型选择对应的函数
    
    $targetHeight = round($sourceHeight * $targetWidth / $sourceWidth); // 计算目标高度
    
    $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); // 创建目标图像
    
    imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight); // 图像复制和缩放
    
    imagejpeg($targetImage, $file, $targetQuality); // 保存压缩后的图片
    
    imagedestroy($sourceImage); // 释放资源
    imagedestroy($targetImage);
}
Copy after login

Summary:
By using PHP to batch compress image file sizes, we can effectively reduce the size of image files and improve the loading speed of the website. This article provides a simple method and corresponding code example, but it needs to be adapted and extended according to the actual situation. I hope this article helps you better process and optimize image files.

The above is the detailed content of How to batch compress image file size 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