Home  >  Article  >  Backend Development  >  How to compress images when saving remote images using PHP?

How to compress images when saving remote images using PHP?

WBOY
WBOYOriginal
2023-07-14 14:45:10907browse

How to compress images when saving remote images using PHP?

In web development, it is often necessary to save remote images to the local server. However, in the process of saving pictures, sometimes it is necessary to compress the pictures to reduce the size of the picture files, save storage space and improve the loading speed of the website. This article will introduce how to use PHP to save remote images and compress them.

Before we start, we need to ensure that the server has enabled the php-gd extension. The php-gd extension is the basic extension for PHP to process images. If your server has not installed the php-gd extension, you need to install it first.

First, we need to get the content of the remote image and save it locally. You can use the file_get_contents() function to obtain the binary data of the remote image. The following is a sample code:

// 获取远程图片的二进制数据
$data = file_get_contents('http://example.com/image.jpg');

// 保存图片到本地
file_put_contents('local_image.jpg', $data);

The above code will obtain the binary data of the image from the remote address through the file_get_contents() function, and use the file_put_contents() function to save the binary data as a local image file.

Next, we need to use PHP's GD library to compress the image. The GD library provides a series of functions to process images, such as scaling, cropping, rotating, etc. We can use the imagecreatefromjpeg() function to load the image as a GD image object, and use the imagejpeg() function to save the image in JPEG format.

// 压缩图片
function compressImage($source, $destination, $quality) {
    // 打开源图像
    $image = imagecreatefromjpeg($source);
    
    // 保存为JPEG格式,同时设定质量
    imagejpeg($image, $destination, $quality);
    
    // 释放图像资源
    imagedestroy($image);
}

// 原图路径
$source = 'local_image.jpg';
// 压缩后的图路径
$destination = 'compressed_image.jpg';
// 压缩质量,0-100,值越大质量越好,文件越大
$quality = 80;

// 压缩图片
compressImage($source, $destination, $quality);

The above code defines a compressImage() function, which accepts three parameters: source image path, target image path and compression quality. Inside the function, the imagecreatefromjpeg() function is used to load the source image as a GD image object, and the imagejpeg() function is used to save the image as a JPEG format image with the specified compression quality. Finally, use the imagedestroy() function to release the image resources.

In the above code, you can modify the compression quality to control the degree of image compression. Compression quality ranges from 0-100, with higher values ​​providing better image quality but larger file sizes.

Finally, we can save and compress the remote image by calling the compressImage() function. The following is a complete example:

// 获取远程图片的二进制数据
$data = file_get_contents('http://example.com/image.jpg');
// 保存图片到本地
file_put_contents('local_image.jpg', $data);

// 压缩图片
function compressImage($source, $destination, $quality) {
    // 打开源图像
    $image = imagecreatefromjpeg($source);
    
    // 保存为JPEG格式,同时设定质量
    imagejpeg($image, $destination, $quality);
    
    // 释放图像资源
    imagedestroy($image);
}

// 原图路径
$source = 'local_image.jpg';
// 压缩后的图路径
$destination = 'compressed_image.jpg';
// 压缩质量,0-100,值越大质量越好,文件越大
$quality = 80;

// 压缩图片
compressImage($source, $destination, $quality);

The above example code saves the remote image to the local and compresses it by calling the compressImage() function. The final compressed image will be saved as compressed_image.jpg file.

Through PHP's GD library, we can easily compress the saved remote images to improve website performance and user experience.

The above is the detailed content of How to compress images when saving remote images using PHP?. 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