In the field of PHP image processing, why the most famous GD library is good is that it does not require additional installation of other image processing tools, and it is released with the PHP source code. You only need to add it when you install PHP Compile the parameters on it.
Although the GD library can help us deal with many things, such as simple drawing of pictures, watermarking, scaling, etc., business needs are often more diverse and rich. For example, we need to be able to flip and blur pictures like PS. Function, only relying on GD library is very troublesome.
Of course, there may be many students who should have used ImageMagick, but today we will not introduce ImageMagick first, but a branch of its software GraphicsMagick. GraphicsMagick is a fork from ImageMagick 5.5.2. Compared to ImageMagick, it has no new features, but focuses more on stability and performance.
So, the focus of our study will be on the following ImageMagick. For GraphicsMagick, let’s have a brief understanding through this article!
Install
First, we need to install GraphicsMagick in the system, and then install the Gmagick extension in PHP. The specific installation process can refer to the following link:
https://www.jianshu.com/p/7c4e02a84641
Load pictures and view picture information
The first step is to briefly look at how to load and initialize the Gmagick object and some basic information about it.
$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 http://www.GraphicsMagick.org/www/Copyright.html for details.
echo 'Filename:', $image->getimagefilename(), PHP_EOL; // Filename:./img/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
You can instantiate a Gmagick object directly by using new Gmagick(path). Then, we can obtain some information of the picture through a series of getxxx-related methods, such as the current GraphicsMagick version information, picture path, size, format, and so on.
Similarly, if you want to perform other operations on the image, you can use the methods provided in the various extension libraries directly under this object.
Picture border
// 加边框
$image = new Gmagick('./img/2.jpg');
$image->borderimage("green", 2, 2)->oilpaintimage(0.3);
$image->write('./img/2-border.jpg');
The borderimage() method can be used to add a green border with a width of 2 pixels to the image very simply. oilpaintimage() adds an oil painting effect to the picture. Can you see it? 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 a 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 picture, resizeimage() will operate in a channel manner, but the actual usage is actually not too obvious.
// 缩略图
$image = new Gmagick('./img/2.jpg');
$image->thumbnailimage(100, 0);
$image->write('./img/2-thumbnail.jpg');
// 裁剪缩略图
$image = new Gmagick('./img/2.jpg');
$image->cropthumbnailimage(100,90);
$image->write('./img/2-cropthumbnaili.jpg');
// 按比例缩小一半
$image = new Gmagick('./img/2.jpg');
$image->minifyimage();
$image->write('./img/2-minify.jpg');
Thumbnailimage() is to directly generate thumbnails. Its goal is to make small low-cost thumbnail images suitable for display on the Internet. We can fill in only one width or only one height, and the image will automatically bloom to the specified proportions. the size of. cropthumbnailimage() first reduces the image and then crops the specified area from the center to create a fixed-size thumbnail. In fact, from the simple test performance, the difference is not very big.
minifyimage() directly reduces the image by half proportionally, so this method does not require any parameters.
Picture rotation, offset
// 垂直翻转
$image = new Gmagick('./img/2.jpg');
$image->flipimage();
$image->write('./img/2-flip.jpg');
// 水平翻转
$image = new Gmagick('./img/2.jpg');
$image->flopimage();
$image->write('./img/2-flop.jpg');
// 旋转图像
$image = new Gmagick('./img/2.jpg');
$image->rotateimage('#ffffff', 60);
$image->write('./img/2-rotate.jpg');
// 偏移图像
$image = new Gmagick('./img/2.jpg');
$image->rollimage(150, 150);
$image->write('./img/2-roll.jpg');
flipimage() and flopimage() directly flip the image vertically and horizontally, rotateimage() rotates the image according to the specified angle, the first parameter is the background color we want to leave the place where the rotation passes after the rotation . The effect of rollimage() is to offset the picture, that is, to offset the position of the picture from the original size. This effect may not be easy to understand. You can try it yourself, it's cool.
Picture color effect adjustment
// 调亮度、饱和度、色调
$image = new Gmagick('./img/2.jpg');
$image->modulateimage(80, 80, 80);
$image->write('./img/2-modulate.jpg');
// 颜色对比度
$image = new Gmagick('./img/2.jpg');
$image->normalizeimage(30);
$image->write('./img/2-normalize.jpg');
modulateimage() directly adjusts the brightness, saturation and hue of the picture through three parameters, and their value is an integer between -100 and 100. This is actually similar to the related image adjustment tools in PS. In PS or various retouching software, there will be a slide bar that moves up and down to adjust. In the same way, normalizeimage() adjusts the contrast of the picture, which is also similar to the parameter value of the retouching software.
Various special effects
// 模糊效果
$image = new Gmagick('./img/2.jpg');
$image->blurimage(30, 10);
$image->write('./img/2-blur.jpg');
// 运动模糊效果
$image = new Gmagick('./img/2.jpg');
$image->motionblurimage(30, 50, 10);
$image->write('./img/2-motionblur.jpg');
// 径向模糊效果
//$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 blurimage() method is a normal blur function. Its two parameters are blur radius and standard deviation. By adjusting these two parameters, different blur degree effects can be obtained. motionblurimage() Motion blur is actually a blurring effect with a slightly sloping left and right drag-like feeling. The final radial blur effect has been reported in the test, and the reason has not been found. It seems that this function is not supported in GraphicsMagick itself. Anyone who has used it and understands the situation can leave a message to explain!
// 模拟油画效果
$image = new Gmagick('./img/2.jpg');
$image->oilpaintimage(5);
$image->write('./img/2-oilpaint.jpg');
// 创建模拟3D按扭
$image = new Gmagick('./img/2.jpg');
$image->raiseimage(50, 50, 150, 150, true);
$image->write('./img/2-raise.jpg');
// 木炭效果
$image = new Gmagick('./img/2.jpg');
$image->charcoalimage(10, 3);
$image->write('./img/2-charcoal.jpg');
// 图像应用日光效果
$image = new Gmagick('./img/2.jpg');
$image->solarizeimage(60);
$image->write('./img/2-solarize.jpg');
// 随机移动图中的像素
$image = new Gmagick('./img/2.jpg');
$image->spreadimage(10);
$image->write('./img/2-spread.jpg');
// 围绕中心旋转像素
$image = new Gmagick('./img/2.jpg');
$image->swirlimage(100);
$image->write('./img/2-swirl.jpg');
I won't say much about these effects, you can try it yourself.
Summarize
See it, in fact, compared with GD, GraphicsMagick will be very simple to achieve these retouching effects. And these are the functions that common photo editing software will have, that is to say, we can use PHP to make a similar online photo editing tool! Of course, these functions are also available in the ImageMagick we will learn later, and even the method names are basically the same. Therefore, we will not explain the function of each method function in detail when learning ImageMagick.
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 learning there.
Test code:
Reference documents:
https://www.php.net/manual/zh/book.gmagick.php
Searchable on their respective media platforms [Hardcore Project Manager]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。