PHP to try to use the GraphicsMagick thumbnail function _php tips

Source: Internet
Author: User
Tags imagemagick
Commonly used image processing tools are gd,imagemagick,graphicsmagick and so on. GD is a fools, skip to mention; ImageMagick is currently the most popular image processing tool, its function is very rich; GraphicsMagick's function is slightly less than ImageMagick, but it's more efficient, like Apache and Nginx, A more powerful, more efficient one.

It's more about efficiency now, so this article takes GraphicsMagick as an example:

For Phper, there are two ways to use GraphicsMagick:

1: Use pecl gmagick extension.
2: Use the GraphicsMagick command line.

I don't like the way the pecl expands. One pecl code bugs a lot, and then pecl extension of the implementation of the code to write very verbose: such as scaling an animated GIF picture, if you use the way of command line, and a word is done, and with pecl expansion, you have to calculate the animation has a few frames, and then cycle processing, Very troublesome. So I am more inclined to use the command line, although the command line operation sounds very "heavy", but if set up a number of image servers, through the Gearman connected, in fact, very flexible.

Let's take GraphicsMagick as an example, using the command line to see how to use the thumbnail feature:

First the original picture (input.jpg:160x120), which will be used in future examples:



BTW: You reader can swallow your saliva now.

thumbnail 1

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



The actual picture size is: 100x75, which means that by pressing this command, the picture is kept unchanged to generate thumbnails. This is good, but there is a potential problem: we can not simply know the final size of the picture, the result is the front of the display, can not set the IMG tag width and Height properties, if I remember correctly, is generally recommended to set the width and Height properties, Otherwise the browser may be slightly slower to render.

thumbnail 2

GM convert input.jpg-thumbnail ' 100x100! ' output_2.jpg



This time the actual size of the picture produced is defined, but the picture is distorted and sometimes unacceptable.

Thumbnail 3


GM convert input.jpg-thumbnail ' 100x100^ ' \
-gravity center-extent 100x100 Output_3.jpg



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

thumbnail 4

GM convert input.jpg-thumbnail ' 100x100 ' \
-background gray-gravity Center- Extent 100x100 output_4.jpg



This time not only guarantees the size, but also guarantees the proportions, without any cropping of the picture, and the extra parts are filled in the specified color.

thumbnail 5

GM convert input.jpg-thumbnail ' 10000@ ' \
-background gray-gravity Center- Extent 100x100 output_5.jpg



This time, the size and proportions are guaranteed, 10000 of which is the product of the 100x100, and a balance is made between padding and cropping.


Understand the above examples, thumbnails are basically all in all, there must be one that will suit your needs. GraphicsMagick's data is very small, but the good news is that GraphicsMagick and ImageMagick are basically compatible, so you can use the ImageMagick data to apply.

Add: Imgareaselect is a good choice if you want users to manually crop the headers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.