Try using GraphicsMagick's thumbnail feature under PHP

  • 2020-03-31 21:21:48
  • OfStack

Commonly used image processing tool (link: http://www.boutell.com/gd/), (link: http://www.imagemagick.org/), (link: http://www.imagemagick.org/) and so on. GD is a bully, skip it; ImageMagick is the most popular image processing tool, it is very rich in functions; GraphicsMagick is slightly less powerful than ImageMagick, but it's more efficient, like Apache or Nginx, where one is more powerful and the other more efficient.

Now efficiency is more important, so this article takes GraphicsMagick as an example:

For PHPer, there are two ways to use GraphicsMagick:

1: using the extension (link: http://pecl.php.net/package/gmagick/).
2: use the GraphicsMagick command line.

I do not like the way of PECL extension, on the one hand, PECL code is full of bugs, and on the other hand, the implementation of PECL extension, the code is very verbose to write: for example, zoom a GIF animation picture, if you use the command line, a sentence to solve, but with PECL extension, also have to calculate the animation has a few frames, reclooping processing, very troublesome. So I prefer the command line approach, and while command line operations may sound "heavy," they can be quite flexible if you set up several picture servers and connect them through Gearman.

Let's take GraphicsMagick as an example to see how to use the thumbnail functionality from the command line:

The original image (input.jpg: 160x120) will be used in each of the following examples:

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225956950.jpg" border = 0 small = "0" >

BTW: the janitor can swallow now.

The thumbnail 1

Gm convert input.jpg -thumbnail '100x100' output_1.jpg

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225956736.jpg" border = 0 small = "0" >

The actual size of the generated image is: 100x75, that is, according to this command, will keep the image scale unchanged to generate thumbnails. It is very good, but there is a potential problem: we can't simply know the final size of images, the result is the front-end display, unable to set width and height properties of the img tags, if I remember correctly, is generally recommended to set width and height attributes, there may be a bit browsers render up slowly.

Thumbnails 2

Gm convert input.jpg -thumbnail '100x100! 'output_2. JPG

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225957460.jpg" border = 0 small = "0" >

This time the actual size of the generated image is defined, but the image is distorted, which is sometimes unacceptable.

The thumbnail 3


Gm convert input.jpg -thumbnail '100x100^' \
-gravity center - to an extent of 100x100 output_3.jpg

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225957140.jpg" border = 0 small = "0" >

This time it not only guarantees the size, but also the proportion. But the picture has been cropped.

The thumbnail 4

Gm convert input.jpg -thumbnail '100x100' \
-background gray-gravity center - to an extent of 100x100 output_4.jpg

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225957678.jpg" border = 0 small = "0" >

This time, not only the size is guaranteed, but also the proportion is guaranteed. At the same time, there is no cutting on the picture, and the extra parts are filled in according to the specified color.

The thumbnail 5

Gm convert input.jpg -thumbnail '10000@' \
-background gray-gravity center - to an extent of 100x100 output_5.jpg

The < img border = 0 class = blogimg SRC = "http://files.jb51.net/upload/201101/20110101225957265.jpg" border = 0 small = "0" >

This time the size and proportion are guaranteed, where 10,000 is the product of 100x100, and there is a balance between filling and clipping.


With these examples in mind, thumbnails are pretty much catered to, and there's definitely one that will suit your needs. GraphicsMagick has very little data, but the good news is that the usage of GraphicsMagick and ImageMagick is basically compatible, so you can use ImageMagick's data to apply it.

Supplement: if you want to let the user manual cutting head piece, (link: http://odyniec.net/projects/imgareaselect/) is a good choice.

Related articles: