Home > Article > Backend Development > How to use PHP to achieve lossless compression and optimization of images
How to use PHP to achieve lossless compression and optimization of images
Introduction:
In web development, images are an indispensable part. However, image files that are too large will cause the web page to load slowly and affect the user experience. In order to solve this problem, we can use PHP to achieve lossless compression and optimization of images. This article will introduce how to use PHP to implement this function and provide corresponding code examples.
Installing and configuring the GD library
The GD library is a library for processing graphics images in PHP. Before starting, we need to make sure that the GD library is installed on the server. You can check whether the GD library is installed by running the following command:
<?php if(function_exists('gd_info')) { echo 'GD库已安装'; } else { echo 'GD库未安装'; }
If the output result is "GD library has been installed", it means that the GD library has been installed. If the output result is "GD library not installed", you need to install the GD library.
Compress images
The following code example shows how to use the GD library to compress images. We can limit the size of the image by specifying the maximum width and height, and set the compression quality to reduce the image file size.
<?php function compressImage($source, $destination, $maxWidth, $maxHeight, $quality) { $info = getimagesize($source); $width = $info[0]; $height = $info[1]; // 计算缩放比例 $scale = min($maxWidth/$width, $maxHeight/$height); // 计算缩放后的宽度和高度 $newWidth = $width * $scale; $newHeight = $height * $scale; // 创建缩略图 $thumb = imagecreatetruecolor($newWidth, $newHeight); // 根据原图类型进行相应的处理 if ($info['mime'] == 'image/jpeg') { $sourceImage = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/png') { $sourceImage = imagecreatefrompng($source); } elseif ($info['mime'] == 'image/gif') { $sourceImage = imagecreatefromgif($source); } else { return false; } // 将原图复制到缩略图中 imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // 保存缩略图 if ($info['mime'] == 'image/jpeg') { imagejpeg($thumb, $destination, $quality); } elseif ($info['mime'] == 'image/png') { imagepng($thumb, $destination, 9); } elseif ($info['mime'] == 'image/gif') { imagegif($thumb, $destination); } return true; } // 使用示例 $sourceImage = 'source.jpg'; $destinationImage = 'compressed.jpg'; $maxWidth = 800; $maxHeight = 600; $quality = 75; compressImage($sourceImage, $destinationImage, $maxWidth, $maxHeight, $quality);
In the above code example, the compressImage
function accepts the source file path, target file path, maximum width, maximum height and compression quality of the image as parameters. It first obtains the size information of the original image, then calculates the scaling ratio based on the maximum width and height, and calculates the scaled width and height. After that, the function creates a thumbnail image and performs corresponding processing according to the type of the original image. Finally, the thumbnail is saved to the target file with the specified compression quality.
Use transparent images: If there are transparent parts in the picture, you can save it in transparent PNG format instead of JPEG format.
<?php $sourceImage = 'source.png'; $destinationImage = 'transparent.png'; compressImage($sourceImage, $destinationImage, $maxWidth, $maxHeight, $quality);
Delete picture metadata: Some picture files contain a lot of metadata, such as shooting time, device information, etc. We can use the following code to remove metadata from the image:
<?php function deleteMetadata($source, $destination) { $info = getimagesize($source); // 根据原图类型进行相应的处理 if ($info['mime'] == 'image/jpeg') { $sourceImage = imagecreatefromjpeg($source); imagejpeg($sourceImage, $destination); } elseif ($info['mime'] == 'image/png') { $sourceImage = imagecreatefrompng($source); imagepng($sourceImage, $destination, 9); } elseif ($info['mime'] == 'image/gif') { $sourceImage = imagecreatefromgif($source); imagegif($sourceImage, $destination); } return true; } // 使用示例 $sourceImage = 'source.jpg'; $destinationImage = 'optimized.jpg'; deleteMetadata($sourceImage, $destinationImage);
Use WebP format: WebP is an image format that supports lossless and lossy compression, usually better than JPEG and PNG formats Picture files are smaller. You can use the following code to save images in WebP format:
<?php function saveWebp($source, $destination) { $info = getimagesize($source); // 根据原图类型进行相应的处理 if ($info['mime'] == 'image/jpeg') { $sourceImage = imagecreatefromjpeg($source); imagewebp($sourceImage, $destination); } elseif ($info['mime'] == 'image/png') { $sourceImage = imagecreatefrompng($source); imagewebp($sourceImage, $destination); } elseif ($info['mime'] == 'image/gif') { $sourceImage = imagecreatefromgif($source); imagewebp($sourceImage, $destination); } return true; } // 使用示例 $sourceImage = 'source.jpg'; $destinationImage = 'optimized.webp'; saveWebp($sourceImage, $destinationImage);
Summary:
By using PHP and GD libraries, we can easily achieve lossless compression and optimization of images. In addition to compressing images, you can also use other optimization methods to reduce the size of image files and improve web page loading speed, thereby improving user experience. The above code examples show you how to implement these functions. Please choose the appropriate method and code to use according to your needs.
The above is the detailed content of How to use PHP to achieve lossless compression and optimization of images. For more information, please follow other related articles on the PHP Chinese website!