本帖最后由 chlinux 于 2012-04-12 15:55 编辑
- function compressImage($srcFile, $destFile) {
- $compression_type = Imagick::COMPRESSION_JPEG;
- $im = new Imagick ( $srcFile );
- $thumbnail = $im->clone ();
- $w = $thumbnail->getImageWidth ();
- $h = $thumbnail->getImageHeight ();
- $thumbnail->setImageCompression ( $compression_type );
- $thumbnail->setImageCompressionQuality ( 80 );
- $thumbnail->stripImage ();
- $thumbnail->thumbnailImage ( $w, $h, false );
- $thumbnail->writeImage ( $destFile );
- $im->clear ();
- $im->destroy ();
- $thumbnail->clear ();
- $thumbnail->destroy ();
- }
- $srcFile = '28390_s.jpg';
- $destFile = '28390_s_c.jpg';
- compressImage ( $srcFile, $destFile );
复制代码 一个12847字节的图片,经过此程序处理去掉一些没用的信息,并降低质量,最后处理到3899字节。
此例子没有改变尺寸,在现有基础上可稍加改动就可以改质量同时改尺寸了。
补充说明一下:
这个例子用到了imagick模块,需要另外安装。
- imagick module version 3.1.0b1
- ImageMagick supported formats A, ART, AVI, AVS, B, BMP, BMP2, BMP3, C, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DFONT, DNG, DOT, DPS, DPX, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, EXR, FAX, FITS, FRACTAL, FTS, G, G3, GIF, GIF87, GRADIENT, GRAY, HISTOGRAM, HTM, HTML, ICB, ICO, ICON, INFO, IPL, JNG, JPEG, JPG, K, LABEL, M, M2V, MAP, MAT, MATTE, MIFF, MNG, MONO, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NULL, O, ORF, OTB, OTF, PAL, PALM, PAM, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PEF, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG24, PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSD, PTIF, PWP, R, RAF, RAS, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UIL, UYVY, VDA, VICAR, VID, VIFF, VST, WBMP, WMF, WMZ, WPG, X, X3F, XBM, XC, XCF, XPM, XV, XWD, Y, YCbCr, YCbCrA, YUV
复制代码 |