安装imagick支持webp php处理

2020-02-27  本文已影响0人  月小升_582e

https://java-er.com/blog/php-webp-imagick/
本文提及Linux系统版本 CentOS7

MAC 系统重新安装一下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.gz

tar 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 => enabled

imagick module version => 3.4.4

imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel

Imagick compiled with ImageMagick version => ImageMagick 7.0.9-26 Q16 x86_64 2020-02-26 https://imagemagick.org

Imagick using ImageMagick library version => ImageMagick 7.0.9-26 Q16 x86_64 2020-02-26 https://imagemagick.org

ImageMagick copyright => © 1999-2020 ImageMagick Studio LLC

ImageMagick release date => 2020-02-26

ImageMagick number of supported formats:  => 239

ImageMagick 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 Value

imagick.locale_fix => 0 => 0

imagick.skip_version_check => 0 => 0

imagick.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=yes

make

make install

6.2 查看,更新webp

convert -list format | grep WebP

还是没有

更新一下lib的绑定【很关键,我折腾了好几遍,感觉就是差了这一步】

ldconfig /usr/local/lib

convert -list format | grep WebP

好了。

特别关键,在重装以后要执行这句话。

ldconfig /usr/local/lib

6.2 看看php的插件是否支持webp

服务器上执行

php --ri imagick | grep WEBP

没有

那么重新安装php的imagick

pecl uninstall imagick

pecl 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

https://java-er.com/blog/php-webp-imagick/

上一篇下一篇

猜你喜欢

热点阅读