PHP extension test tool Series-use gmagick to crop images to generate thumbnails

Source: Internet
Author: User
After crawlers crawl to an article, the first image in the article is often used as a thumbnail of the article to be displayed on the list, home page, or other recommended positions. in this case, the image is generally processed simply, generate thumbnails of several sizes. for example, I used the thumbnails in the 170x170 and 370x370 formats. due to the poor quality of gd images, another extension library of PHP image processing & mdash; g

After crawlers crawl to an article, the first image in the article is often used as a thumbnail of the article to be displayed on the list, home page, or other recommended positions. in this case, the image is generally processed simply, generate thumbnails of several sizes. for example, I used the thumbnails in the 170x170 and 370x370 formats. due to the poor quality of gd images, here we use another extension Library named gmagick for PHP image processing. before using it, we need to install this extension (for installation methods, refer to: compiling and installing GraphicsMagick and PHP extension gmagick in Linux ).
After the test and installation are successful, you can use the gmagick tool for image processing. the code is as follows:

$ Imagepath = '/path/to/test.jpg'; list ($ width, $ height) = getimagesize ($ imagepath); $ image1 = new Gmagick ($ imagepath ); if ($ width/170> = $ height/170) {$ iScale = 170/$ height; $ iScaleHeight = $ height; // After proportional reduction, the image height $ iScaleWidth = 170/$ iScale;} else {$ iScale = 170/$ width; $ iScaleWidth = $ width; // scaled down Image height $ iScaleHeight = 170/$ iScale;} $ iHeight = 170 * $ height/$ width; $ image1-> cropthumbnailimage ($ iScaleWidth, $ iScaleHeight) -> setcompressionquality (100); $ image1-> scaleimage (170,170, true)-> setcompressionquality (100); $ image1-> write(registrimagepath.'_170x170.jpg '); $ image2 = new Gmagick ($ imagepath); if ($ width/370 >=$ height/370) {$ iScale = 370/$ height; $ iScaleHeight = $ height; // After proportional reduction, the image height $ iScaleWidth = 370/$ iScale;} else {$ iScale = 370/$ width; $ iScaleWidth = $ width; // After proportional reduction, the image height $ iScaleHeight = 370/$ iScale;} $ image2-> cropthumbnailimage ($ iScaleWidth, $ iScaleHeight)-> setcompressionquality (100 ); $ image2-> scaleimage (370,370, true)-> setcompressionquality (100); $ image2-> write(imagepath.'_370x370.jpg '); $ image1-> destroy (); $ image2-> destroy ();

SOURCE image (scale to 60% of the original proportion, click to view the original proportion ):

Thumbnail of 170x170 size:

370x370 thumbnail:

For more usage of gmagick related classes and functions, refer to this article :? PHP extension-image processing 2-Gmagick Library examples and related classes and functions

Related Article

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.