Home  >  Article  >  Backend Development  >  How to do image processing in PHP?

How to do image processing in PHP?

PHPz
PHPzOriginal
2023-05-12 08:04:401663browse

In websites and applications, image processing is a very important technology. Through image processing technology, images can be resized, cropped, rotated, filter effects and other operations can be performed to meet personalized needs. In PHP, there are some powerful image processing libraries, such as GD, ImageMagick, etc., that can help us complete these operations. This article will introduce you to how to perform image processing in PHP.

1. GD library

GD is a commonly used image processing library in PHP. Through this library, PHP can perform image processing, image generation and other operations. The GD library supports processing a variety of common image formats, such as JPEG, PNG, GIF, WBMP, BMP, etc., and provides some commonly used operation functions.

When using the GD library for image processing, you need to ensure that PHP has the GD extension. First, you need to open the php.ini file and find the following line in the file to ensure that the GD extension is enabled:

extension=php_gd2.dll

If it is not enabled, you need to remove the comment symbol, and then Restart the server or PHP service.

1. Open a picture

Use the imagecreatefromjpeg() function to open a picture in JPEG format. The code is as follows:

$im = imagecreatefromjpeg("picture.jpg" );

Similarly, if you want to open a picture in PNG or GIF format, you can use the imagecreatefrompng() function and imagecreatefromgif() function.

2. Create a new image

Use the imagecreatetruecolor() function to create a blank image. The code is as follows:

$im = imagecreatetruecolor(400, 400);

The first parameter of this function is the width of the new image, and the second parameter is the height of the new image.

3. Modify the image size

Use the imagecopyresized() function to modify the size of the image. The code is as follows:

$width = imagesx($im);
$height = imagesy($im);
$new_width = $width * 0.5;
$new_height = $height * 0.5;
$new_im = imagecreatetruecolor($new_width, $new_height);
imagecopyresized ($new_im, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

In the code, we first get the width and height of the original image, and then calculate Out the new width and height, and use imagecreatetruecolor() to create a new image. Then we use imagecopyresized() to reduce the original image by the specified ratio and save the result in the new image.

4. Image rotation

Use the imagerotate() function to rotate the image. The code is as follows:

$degree = 45;
$new_im = imagerotate( $im, $degree, 0);

The first parameter of this function is the picture to be rotated, the second parameter is the angle of rotation, and the third parameter is the specified color (during the rotation process, Missing pixels are filled with that color).

5. Add watermark

Use the imagestring() function to add text watermark to the image. The code is as follows:

imagestring($im, 5, 5, 5, " Hello world", $black);

The first parameter of this function is the image to be added with watermark, the second parameter is the font size, the third parameter is the x coordinate, and the fourth parameter is y Coordinates, the fifth parameter is the text content to be added, and the sixth parameter is the text color.

2. ImageMagick

ImageMagick is a powerful image processing library through which various image processing operations can be performed, such as image resizing, rotation, cropping, etc.

To use the ImageMagick library, you need to first ensure that ImageMagick is installed on the server and the PHP ImageMagick extension is enabled.

1. Open a picture

Use the readImage() function of the Imagick() class to open a picture. The code is as follows:

$im = new Imagick() ;
$im->readImage("picture.jpg");

2. Create a new picture

Use the newImage() function to create a new picture. The code is as follows:

$im = new Imagick();
$im->newImage(400, 400, "white");

The first parameter of this function is The width of the new image. The second parameter is the height of the new image. The third parameter is the background color of the new image.

3. Modify the image size

Use the resizeImage() function to modify the size of the image. The code is as follows:

$im->resizeImage($width 0.5, $height 0.5, Imagick::FILTER_LANCZOS, 1);

The first parameter of this function is the new width, the second parameter is the new height, and the third parameter is the interpolation Filter, the fourth parameter is the filter ambiguity.

4. Image rotation

Use the rotateImage() function to rotate the image. The code is as follows:

$im->rotateImage(new ImagickPixel(), 45 );

The first parameter of this function is the angle of rotation, and the second parameter is the specified color (during the rotation process, missing pixels will be filled with this color).

5. Add watermark

Use the annotateImage() function to add a text watermark to the image. The code is as follows:

$draw = new ImagickDraw();
$ draw->setFont("Arial.ttf");
$draw->setFontSize(24);
$draw->setFillColor("black");
$draw->setGravity (Imagick::GRAVITY_SOUTHWEST);
$draw->annotation(5, 5, "Hello world");
$im->drawImage($draw);

The first parameter of this function is the created ImagicDraw object, the second parameter is the x coordinate, the third parameter is the y coordinate, the fourth parameter is the text content to be added, and the fifth parameter is the text color. .

3. Summary

In this article, we introduced how to use the GD library and ImageMagick in PHP for image processing operations. Through these libraries, we can easily resize, rotate, add watermarks, etc. to images to meet different needs. Using these libraries, we can make our websites and applications more beautiful and attract more users.

The above is the detailed content of How to do image processing 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