Home > Backend Development > PHP Problem > How to compress images in php without distortion

How to compress images in php without distortion

王林
Release: 2023-05-06 18:26:07
Original
1978 people have browsed it

With the development of Internet technology, pictures have become an indispensable part of web design, but with it comes the problem of loading speed. In order to make web pages faster, images must be compressed, but the compression process can easily cause image distortion. In this case, we need to use more advanced technology. In this article, we will introduce how to use PHP to compress images without distortion.

1. Using PHP’s GD library

PHP’s GD library is a commonly used graphics library that can be used to handle various graphics operations, such as scaling, cropping, rotation, and watermarking. wait. By leveraging this library, we can compress images to a specified size without distortion. Let's take a look at how to use PHP's GD library for image compression.

First, make sure your PHP has the GD library installed. If it is not installed, you can install it through the following command:

sudo apt-get install php-gd
Copy after login

After installation, we can write the code to compress the image. First, we need to load the image:

<?php
// 加载原图片
$src_file = 'source.jpg';
$image = imagecreatefromjpeg($src_file);
?>
Copy after login

Next, we need to specify the compressed image size:

<?php
// 加载原图片
$src_file = 'source.jpg';
$image = imagecreatefromjpeg($src_file);

// 指定压缩后的图片大小
$max_width = 600;
$max_height = 400;
?>
Copy after login

Then, we need to calculate the actual size and compression ratio of the image:

<?php
// 加载原图片
$src_file = 'source.jpg';
$image = imagecreatefromjpeg($src_file);

// 指定压缩后的图片大小
$max_width = 600;
$max_height = 400;

// 计算出图片的实际大小及压缩比例
$width = imagesx($image);
$height = imagesy($image);
$scale = min($max_width/$width, $max_height/$height, 1);

// 计算出压缩后的大小
$new_width = round($scale*$width);
$new_height = round($scale*$height);
?>
Copy after login

Now, we already have the actual size and compression ratio of the image. The next step is to compress the image to the specified size:

<?php
// 加载原图片
$src_file = 'source.jpg';
$image = imagecreatefromjpeg($src_file);

// 指定压缩后的图片大小
$max_width = 600;
$max_height = 400;

// 计算出图片的实际大小及压缩比例
$width = imagesx($image);
$height = imagesy($image);
$scale = min($max_width/$width, $max_height/$height, 1);

// 计算出压缩后的大小
$new_width = round($scale*$width);
$new_height = round($scale*$height);

// 创建新图片
$new_image = imagecreatetruecolor($new_width, $new_height);

// 复制原图片到新图片并压缩
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// 保存压缩后的图片
imagejpeg($new_image, 'compressed.jpg', 90);
?>
Copy after login

The last step is to save the compressed image. Here we use imagejpeg() The function is saved in JPEG format, and the third parameter is the compression quality. The larger the value, the higher the quality and the larger the file.

2. Use third-party libraries: TinyPNG

In addition to using PHP’s GD library for compression, we can also use some third-party libraries for operations. Among them, a very good compressed image library is TinyPNG. It can compress JPEG, PNG and other images to the smallest size without distortion at all.

First, we need to register on TinyPNG’s official website and generate an API Key. Then, you can compress the image directly through the API. The following is the code for image compression using TinyPNG:

<?php
require_once 'vendor/autoload.php';

// 指定图片路径
$file = 'source.jpg';

// 压缩图片并保存
\Tinify\setKey("YOUR_API_KEY");
\Tinify\fromFile($file)->toFile('compressed.jpg');
?>
Copy after login

Here, we use the officially provided PHP SDK and specify the API Key in the code. Then we can use the fromFile() method to load the image for compression. , the toFile() method saves the compressed image to the specified path.

Summary

Whether you use PHP's GD library or the third-party library TinyPNG for image compression, you can reduce the image file size as much as possible and improve the website loading speed while ensuring the image quality. . In actual projects, we need to choose according to specific situations and use them flexibly. Doing a good job of image compression can not only improve web page performance, but also provide users with a better access experience.

The above is the detailed content of How to compress images in php without distortion. 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