Home >Backend Development >PHP7 >How to perform image processing in PHP7.0?

How to perform image processing in PHP7.0?

王林
王林Original
2023-05-27 08:51:391599browse

PHP is a programming language widely used in Web development. It is highly readable and easy to learn. It also has high application value in the field of image processing. From PHP5.5 to PHP7.0 upgrade, PHP has made a series of optimizations and improvements in image processing, including more efficient memory management, faster execution speed, richer image processing functions, etc. This article will introduce in detail how to perform image processing in PHP7.0.

1. GD Library

Image processing is an indispensable part of Web development, so PHP provides many libraries to help web developers perform image processing, of which GD library is the most common one. The GD library is an extension library for PHP that provides many simple and easy-to-use functions to process images, such as scaling, rotating, shearing, etc. In PHP7.0, the GD library has also undergone some optimizations. To use the GD library, it can be enabled through the extension directive in the php.ini file and must be enabled during PHP installation.

2. Install the GD library

Before using the GD library, you need to check whether PHP has enabled this library. You can view PHP configuration information through the phpinfo() function, including information about the GD library. You can check whether the GD library is installed by the following method:

<?php
phpinfo();
?>

If you do not see the gd extension module table, it means that the GD library is not installed. To install the GD library, you can use the following steps:

  1. Download GD library

Visit the official website of the GD library (https://libgd.github.io/), And download the corresponding installation package.

  1. Installing dependencies

To install the GD library, you must first install the gd, libpng, libjpeg and libfreetype dependencies, which depend on the operating system you are using. Certainly.

For example, in Ubuntu, these dependencies can be installed using the following commands:

 sudo apt-get install libpng-dev libjpeg-dev libfreetype6-dev
  1. Compile and install the GD library

Compile and install using the following commands Install the GD library:

 ./configure
 make
 make install
  1. Enable GD library

Enter the php.ini file (php-fpm.ini if ​​it is PHP-FPM) and find the following Instructions:

 ;extension=gd.so

Remove the preceding semicolon, uncomment it, save and restart the server.

3. Image processing

  1. Creating an image

First you need to create a canvas. You can use the imagecreatetruecolor() function of the GD library to create a canvas of a specified size. and color canvas. For example, the following code will create a canvas with dimensions of 400x400 pixels and a background color of white:

 $image = imagecreatetruecolor(400, 400);
 $background = imagecolorallocate($image, 255, 255, 255);
 imagefill($image, 0, 0, $background);
  1. Reading the image

To manipulate the image, you need to read the image file Get it into memory. Images can be read using the imagecreatefromxxx() function of the GD library. xxx represents different image file formats, such as jpeg, png, gif, etc. For example, the following code will read a JPEG image named "test.jpg":

 $image = imagecreatefromjpeg('test.jpg');
  1. Scale the image

Scale the image is a common image processing operation . You can use the imagescale() function of the GD library to scale the image and specify the scaled size. For example, the following code will scale an 800x600 pixel image to a 400x300 pixel size:

 $image = imagecreatefromjpeg('test.jpg');
 $resized_image = imagescale($image, 400, 300);
  1. Rotate the image

Another common image processing operation is to rotate the image. You can use the imagerotate() function of the GD library to rotate an image and specify the angle of rotation. For example, the following code will rotate an image 90 degrees:

 $image = imagecreatefromjpeg('test.jpg');
 $rotated_image = imagerotate($image, 90, 0);
  1. MERGE IMAGES

Merge images is a method of combining two or more images into a new image operation. Images can be merged using the GD library's imagecopy() function. For example, the following code will merge two images:

 $image1 = imagecreatefrompng('image1.png');
 $image2 = imagecreatefromjpeg('image2.jpg');
 imagecopy($image1, $image2, 0, 0, 0, 0, 200, 200);
  1. Cut image

Cut image is a common operation, you can use the GD library's imagecrop() function to implement. For example, the following code will cut out a 200x200 pixel square from an image with dimensions of 800x600 pixels:

 $image = imagecreatefromjpeg('test.jpg');
 $cropped_image = imagecrop($image, ['x' => 300, 'y' => 200, 'width' => 200, 'height' => 200]);
  1. Output image

Finally, the processed The image is output to the response. You can use the GD library's imagepng(), imagejpeg(), imagegif() and other functions to output images into files in PNG, JPEG, GIF and other formats. For example, the following code outputs a JPEG image named "output.jpg" to the response:

 header('Content-Type: image/jpeg');
 imagejpeg($image, null, 100);

4. Summary

Image processing in PHP7.0 is very easy, Mainly use various functions provided by the GD library to create, manipulate and output images. To use the GD library, you need to check and install the dependencies and enable the corresponding extensions in the php.ini file. You can then use various GD library functions to implement common image processing operations such as scaling, rotation, shearing, and merging. For image processing in web development, PHP7.0 is a very effective and efficient choice.

The above is the detailed content of How to perform image processing in PHP7.0?. 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