search
HomeBackend DevelopmentPHP ProblemHow to compress images in php without distortion

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

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);
?>

Next, we need to specify the compressed image size:

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

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

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);
?>

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);
?>

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');
?>

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function