In the field of PHP image processing, the most famous GD library is good because it does not need any other image processing tools to be installed, and it is released with the PHP source code. You only need to add compilation parameters when installing PHP.
Although the GD library can help us deal with many things, such as simple drawing, watermarking and scaling of pictures, the business requirements are often more diverse and rich. For example, we need functions such as turning and blurring pictures like PS today. Relying on the GD library alone is very troublesome.
Of course, many students may have used ImageMagick, but today we will not introduce ImageMagick, but its branch software graphicsmagick. Graphicsmagick is a fork from ImageMagick 5.5.2. Compared with ImageMagick, it has no new features, but focuses more on stability and performance.
Therefore, the focus of our study will be on the later ImageMagick. For graphicsmagick, let’s simply understand it through this article!
First, we need to install graphicsmagick in the system, and then install the gmagick extension in PHP. For the specific installation process, please refer to the following link:
Loading pictures and viewing picture information
The first step is to simply take a look at how to load and initialize the gmagick object and some of its basic information.
$image = new Gmagick('./img/2.jpg'); echo 'Copyright：', $image->getcopyright(), PHP_EOL; // Copyright：Copyright (C) 2002-2020 GraphicsMagick Group. // Additional copyrights and licenses apply to this software. // See https://imgs.developpaper.com/imgs/2.jpg echo 'Image Format：', $image->getimageformat(), PHP_EOL; // Image Format：JPEG echo 'Image Width and Height：', $image->getimagewidth(), ' * ', $image->getimageheight(), PHP_EOL; // Image Width and Height：300 * 244 echo 'Image type：', $image->getimagetype(), PHP_EOL; // Image type：6
Directly use new gmagick (path) to instantiate a gmagick object. Then, we can use a series of getxxxx related methods to obtain some information of the picture, such as the version information of the current graphicsmagick, picture path, size, format and so on.
Similarly, to perform other operations on the image, you can directly use the methods provided in various extension libraries under this object.
//Border $image = new Gmagick('./img/2.jpg'); $image->borderimage("green", 2, 2)->oilpaintimage(0.3); $image->write('./img/2-border.jpg');
Using the borderimage () method, you can simply add a green border with a width and height of 2 pixels to the picture. Oilpainimage () is to add an oil painting effect to the picture. Do you see that the methods in the object instantiated by gmagick can be called in a chain. As long as the method you are currently using also returns the gmagick object.
Crop pictures and thumbnails
$image = new Gmagick('./img/2.jpg'); $image->resizeimage(150, 150, 10, 1); $image->write('./img/2-resize.jpg'); $image = new Gmagick('./img/2.jpg'); $image->scaleimage(150, 150); $image->write('./img/2-scale.jpg');
These two methods, resizeimage() and scaleimage(), can be used to change the size of the image. Resizeimage() will operate in the way of channel, but the actual use is not much different.
//Thumbnail $image = new Gmagick('./img/2.jpg'); $image->thumbnailimage(100, 0); $image->write('./img/2-thumbnail.jpg'); //Crop thumbnail $image = new Gmagick('./img/2.jpg'); $image->cropthumbnailimage(100,90); $image->write('./img/2-cropthumbnaili.jpg'); //Scale it in half $image = new Gmagick('./img/2.jpg'); $image->minifyimage(); $image->write('./img/2-minify.jpg');
Thumbnailimage () directly generates thumbnails. Its goal is to make small and low-cost thumbnail images suitable for online display. We can only fill in one width or one height, and the images will be automatically split into the specified size in equal proportion. Cropthumbnailimage () creates a fixed size thumbnail by reducing the image and then cropping the specified area from the center. In fact, from the simple test performance, the difference is not very big.
Minifyimage () directly reduces the image by half, so this method does not require any parameters.
Picture rotation and offset
//Flip vertically $image = new Gmagick('./img/2.jpg'); $image->flipimage(); $image->write('./img/2-flip.jpg'); //Flip horizontally $image = new Gmagick('./img/2.jpg'); $image->flopimage(); $image->write('./img/2-flop.jpg'); //Rotate image $image = new Gmagick('./img/2.jpg'); $image->rotateimage('#ffffff', 60); $image->write('./img/2-rotate.jpg'); //Offset image $image = new Gmagick('./img/2.jpg'); $image->rollimage(150, 150); $image->write('./img/2-roll.jpg');
Flipimage () and flopimage () flip the picture vertically and horizontally, while rotateimage () rotates the picture according to the specified angle. The first parameter is the background color we want to leave after the rotation. The effect of rollimage() is to offset the picture, that is, to offset the position of the picture in the original size. This effect may be a little difficult to understand. You can try it yourself. It’s cool.
Picture color effect adjustment
//Adjust brightness, saturation and hue $image = new Gmagick('./img/2.jpg'); $image->modulateimage(80, 80, 80); $image->write('./img/2-modulate.jpg'); //Color contrast $image = new Gmagick('./img/2.jpg'); $image->normalizeimage(30); $image->write('./img/2-normalize.jpg');
Moduleimage() adjusts the brightness, saturation and hue of the picture directly through three parameters. Their values are integers between – 100 and 100. In fact, this is similar to the relevant picture adjustment tools in PS. in PS or various drawing repair software, there will be a sliding rod pulled up and down to adjust. Similarly, normalizeimage() is used to adjust the contrast of the image, which is similar to the parameter value of the image modification software.
Various special effects
//Blur effect $image = new Gmagick('./img/2.jpg'); $image->blurimage(30, 10); $image->write('./img/2-blur.jpg'); //Motion blur effect $image = new Gmagick('./img/2.jpg'); $image->motionblurimage(30, 50, 10); $image->write('./img/2-motionblur.jpg'); //Radial blur effect //$image = new Gmagick('./img/2.jpg'); //$image->radialblurimage(12.5); //$image->write('./img/2-radialblur.jpg');
There are three blur tools in the blur effect. The first blur () method is a common blur function. Its two parameters are blur radius and standard deviation. Different blur effects can be obtained by adjusting these two parameters. Motionblur () motion blur is actually the blur effect of left-right drag feeling with a little slope. The final radial blur effect has always reported an error in the test, and the reason has not been found. It seems that this function is not supported in graphicsmagick. Useful students who understand the situation can leave a message to explain ha!
//Simulated oil painting effect $image = new Gmagick('./img/2.jpg'); $image->oilpaintimage(5); $image->write('./img/2-oilpaint.jpg'); //Create simulated 3D button $image = new Gmagick('./img/2.jpg'); $image->raiseimage(50, 50, 150, 150, true); $image->write('./img/2-raise.jpg'); //Charcoal effect $image = new Gmagick('./img/2.jpg'); $image->charcoalimage(10, 3); $image->write('./img/2-charcoal.jpg'); //Apply daylight effects to images $image = new Gmagick('./img/2.jpg'); $image->solarizeimage(60); $image->write('./img/2-solarize.jpg'); //Randomly moving pixels in a graph $image = new Gmagick('./img/2.jpg'); $image->spreadimage(10); $image->write('./img/2-spread.jpg'); //Rotate pixels around center $image = new Gmagick('./img/2.jpg'); $image->swirlimage(100); $image->write('./img/2-swirl.jpg');
I won’t say much about this pile of effects. You can try it yourself.
See, in fact, compared with Gd, graphicsmagick is very simple to realize the effects of these modifications. And these are the functions of common drawing repair software. In other words, we can make a similar online drawing repair tool with PHP! Of course, the ImageMagick we will learn later also has these functions, and even the method name is basically the same. Therefore, when learning ImageMagick, we will not describe the function of each method function in detail.
In addition, graphicsmagick also has two objects, gmagickdraw and gmagickpixel, which are used to draw graphics and define colors. These two objects also have corresponding implementations in ImageMagick, and we mainly focus on the learning there.
Official account: hard core project manager
Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials
Tiktok, official account, voice, headline search, hard core project manager.
Station B ID: 482780532