本文提及Linux系統(tǒng)版本 CentOS7
MAC 系統(tǒng)重新安裝一下imagick 就好了,似乎等于更新了我之前的版本就有效了。
brew reinstall imagemagick
convert -list format | grep webp
Centos 安裝了Imagick但是不含webp, 參考前一篇安裝文章php 安裝imagick圖形處理庫
1. 卸載以前的版本
yum remove ImageMagick
2. 手工下載新版本安裝
wget https://www.imagemagick.org/download/ImageMagick.tar.gztar zxvf ImageMagick.tar.gz cd ImageMagick-7.0.9-26/ ./configure --with-webp
./libtool: line 1765: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
這個問題,客戶端導致的,別找服務器方案
MAC下設置下就好了
3. 安裝webp
yum -y install libwebp-devel libwebp-tools
4. 重新安裝php的插件imagick
#php -m| grep imagick PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/imagick.so' - /usr/lib64/php/modules/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
報錯了
卸載之前安裝的
pecl uninstall imagick
再安裝一遍
pecl install imagick
php -m| grep imagick
好了
php --ri imagick imagick imagick module => enabledimagick module version => 3.4.4imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernelImagick compiled with ImageMagick version => ImageMagick 7.0.9-26 Q16 x86_64 2020-02-26 https://imagemagick.orgImagick using ImageMagick library version => ImageMagick 7.0.9-26 Q16 x86_64 2020-02-26 https://imagemagick.orgImageMagick copyright => ? 1999-2020 ImageMagick Studio LLCImageMagick release date => 2020-02-26ImageMagick number of supported formats: => 239ImageMagick supported formats => 3FR, 3G2, 3GP, A, AAI, AI, ART, ARW, AVI, AVS, B, BGR, BGRA, BGRO, BMP, BMP2, BMP3, BRF, C, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CR3, CRW, CUBE, CUR, CUT, DATA, DCM, DCR, DCRAW, DCX, DDS, DFONT, DNG, DPX, DXT1, DXT5, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, FAX, FILE, FITS, FLV, FRACTAL, FTP, FTS, G, G3, G4, GIF, GIF87, GRADIENT, GRAY, GRAYA, GROUP4, HALD, HDR, HISTOGRAM, HRZ, HTM, HTML, HTTP, HTTPS, ICB, ICO, ICON, IIQ, INFO, INLINE, IPL, ISOBRL, ISOBRL6, JNG, JNX, JPE, JPEG, JPG, JPS, JSON, K, K25, KDC, LABEL, M, M2V, M4V, MAC, MAP, MASK, MAT, MATTE, MEF, MIFF, MKV, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NRW, NULL, O, ORF, OTB, OTF, PAL, PALM, PAM, PANGO, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PGX, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG00, PNG24, PNG32, PNG48, PNG64, PNG8, PNM, POCKETMOD, PPM, PS, PS2, PS3, PSB, PSD, PTIF, PWP, R, RADIAL-GRADIENT, RAF, RAS, RAW, RGB, RGB565, RGBA, RGBO, RGF, RLA, RLE, RMF, RW2, SCR, SCREENSHOT, SCT, SFW, SGI, SHTML, SIX, SIXEL, SPARSE-COLOR, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TM2, TTC, TTF, TXT, UBRL, UBRL6, UIL, UYVY, VDA, VICAR, VID, VIFF, VIPS, VST, WBMP, WMV, WPG, X, X3F, XBM, XC, XCF, XPM, XPS, XV, XWD, Y, YCbCr, YCbCrA, YUV Directive => Local Value => Master Valueimagick.locale_fix => 0 => 0imagick.skip_version_check => 0 => 0imagick.progress_monitor => 0 => 0
5. php 測試效果
5.1 把圖片a.png 縮小為寬度100的圖片
header('Content-type: image/jpeg');date_default_timezone_set("Asia/Shanghai");$image = new Imagick('a.png');$image->thumbnailImage(100, 0);echo $image;
5.2 轉換,把png 改成jpg
<?php date_default_timezone_set("Asia/Shanghai");$thumb = new Imagick();$thumb->readImage('a.png');$thumb->writeImage('b.jpg');$thumb->clear();$thumb->destroy();?>
5.3 轉換,把png 改成webp
<?php date_default_timezone_set("Asia/Shanghai");$thumb = new Imagick();$thumb->readImage('a.png');$thumb->writeImage('b.webp');$thumb->clear();$thumb->destroy();?>
# ls -l -rw-r--r-- 1 root root 33761 Feb 26 16:59 a.png-rwxrwxrwx 1 root root 10158 Feb 26 16:50 a.webp-rw-r--r-- 1 apache apache 46142 Feb 26 17:37 b.jpg-rw-r--r-- 1 apache apache 10158 Feb 26 17:38 b.webp-rwxrwxrwx 1 root root 172 Feb 26 17:38 webp.php
可以看到 b.webp的文件比b.jpg的文件小很多,說明成功的轉換了webp文件。
5.4 用一下setImageFormat 設置webp
當使用了setImageFormat才能看出來webp其實沒有安裝成功。
date_default_timezone_set("Asia/Shanghai");$thumb = new Imagick();$thumb->readImage('a.png');$thumb->setImageFormat("webp"); $thumb->writeImage('b.webp');$thumb->clear();$thumb->destroy();
Fatal error: Uncaught exception 'ImagickException' with message 'Unable to set image format' in /www/webp.php:6 Stack trace: #0 /www/javaer.com/webp.php(6): Imagick->setimageformat('webp') #1 {main} thrown in /www/javaer.com/webp.php on line 6
6. 重新編譯服務器imagick
6.1 重裝imagick
./configure --with-webp=yesmake make install
6.2 查看,更新webp
convert -list format | grep WebP
還是沒有
更新一下lib的綁定【很關鍵,我折騰了好幾遍,感覺就是差了這一步】
ldconfig /usr/local/lib
convert -list format | grep WebP
好了。
特別關鍵,在重裝以后要執(zhí)行這句話。
ldconfig /usr/local/lib
6.2 看看php的插件是否支持webp
服務器上執(zhí)行
php --ri imagick | grep WEBP
沒有
那么重新安裝php的imagick
pecl uninstall imagickpecl install imagick
php --ri imagick | grep WEBP
有了
6.3 寫個test.php 看看phpinfo里有沒有
但是phpinfo()里沒有
重啟動fpm, 重啟動nginx
phpinfo也有了
當php使用了setImageFormat 設置webp的格式時候,就不會報錯了。徹底安裝成功。
webp官方
https://developers.google.com/speed/webp/download
聯(lián)系客服