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