Home  >  Article  >  Backend Development  >  How to modify image binary file size in php

How to modify image binary file size in php

PHPz
PHPzOriginal
2023-04-03 11:14:59639browse

In network development and applications, we often need to modify images to achieve different needs and purposes. The binary file size (file volume) of the image is an important factor that we cannot ignore, because an image that is too large will reduce the performance and loading speed of the website or application, and may even cause user loss. Therefore, the size of the image file Optimization is very necessary. PHP is a powerful programming language and one of the commonly used languages ​​in network development. Below I will introduce to you how to use PHP to modify the binary file size of images.

1. Why do you need to modify the binary file size of images?

In web development, there are many ways to display images to users, and the two most common ways are to load images directly into the page or provide them as attachments for users to download. When using images to load directly into the page, the webpage will load the image based on the content of the page. If the image is too large, the webpage loading speed will slow down and the user experience will be reduced; in the worst case, it may cause the webpage to crash. When provided as an attachment to users for download, because the binary file size (file volume) of an image is too large, the download speed will be too slow, resulting in poor user experience.

Therefore, for the pictures in the project, we must control its binary file size (file volume). A common method is to compress and optimize images. For PHP development, you can use the GD library to modify the binary file size of images.

2. Introduction to GD library

The GD library is an open source library written in C language and used to dynamically create images. This library can process images in PNG, JPEG, GIF and other formats, and generate images in various formats through operations such as scaling, cropping, rotating, adding text and watermarks. The GD library provides a series of functions that can be supported through the GD extension of PHP.

3. How to modify the size of image binary files with PHP

The method of using PHP to modify the size of image binary files is mainly achieved through compression and optimization. The following are two methods of using PHP's GD library to achieve compression and optimization:

1. Adjust the image size by proportion

This method can keep the aspect ratio of the image unchanged, but only adjust the image size. The image is scaled proportionally.

Code example:

<?php
$src_file = &#39;test.jpg&#39;;  // 图片的路径和名称
$dst_file = &#39;thumbnail.jpg&#39;; // 缩略图的路径和名称
$quality = 50; // 压缩后的图片质量,0~100,0 地表示压缩比最高
$max_width = 200;  // 图片最大的宽度

// 获取原始图片的宽和高
$size = getimagesize($src_file);
$src_width = $size[0];
$src_height = $size[1];

// 计算缩小比例
if($src_width > $max_width){
    $shrink = $max_width / $src_width;
} else{
    $shrink = 1;
}

// 计算缩略图的宽度和高度
$des_width = intval($src_width * $shrink);
$des_height = intval($src_height * $shrink);

// 创建图片资源
$src_image = imagecreatefromjpeg($src_file);
$des_image = imagecreatetruecolor($des_width, $des_height);

// 拷贝并缩放图片
imagecopyresampled($des_image, $src_image, 0, 0, 0, 0, $des_width, $des_height, $src_width, $src_height);

// 保存图片
imagejpeg($des_image, $dst_file, $quality);

// 释放图片资源
imagedestroy($src_image);
imagedestroy($des_image);
?>

Here, the test.jpg image is scaled proportionally. The maximum width after scaling is 200 pixels. The saved image name is thumbnail.jpg, and the compression quality is 50.

Note: Using this method, you can also choose to crop the image when zooming. You should consider using the PNG format, which preserves transparency when the image is enlarged or reduced.

2. Directly adjust the quality of the picture

This method can compare and measure the quality of the picture and directly output it as a modified picture.

Code example:

<?php
$src_file = &#39;test.jpg&#39;;  // 原始图片
$dst_file = &#39;optimize.jpg&#39;; // 优化后的图片
$quality = 50; // 压缩后的图片质量,0~100,0 地表示压缩比最高

// 创建图片资源
$src_image = imagecreatefromjpeg($src_file);

// 优化图片
imagejpeg($src_image, $dst_file,$quality);

// 释放图片资源
imagedestroy($src_image);
?>

In this code, the compression quality of the test.jpg image is reduced to 50, and the saved image name is optimize.jpg.

Note: Please note that this method cannot change the image size.

4. Summary

In the development of PHP, it is very necessary to use the GD library to modify the binary file size of images. By compressing and optimizing images, the size of images can be effectively reduced, thereby optimizing web page loading speed and improving user experience. This article introduces an introduction to the GD library and two methods of using the GD library to modify the binary file size of images in PHP. I hope it will be helpful to PHP engineers doing web development.

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