Imagick功能至關的多,只是還不穩定,我下面的程序可以運行,可是會出現內存錯誤,但咱們要的圖片仍是可以獲得。php
弄這個的緣由是,一個客戶要求在一個appcan的應用裏面實現一個頁面的背景圖的縮放、調整位置和模糊效果。css
這些js和css都能實現,在電腦上表現得很好,可是到了手機上,模糊就出了問題,而她又要求背景不動,調整了位置就設置了背景圖的x y,但不動的話又要求背景位置fixed,因此我加了div,位置fixed,再給它放上背景圖,在電腦上好好的,到手機上fixed就失效了。服務器
我只好採起在服務器端裁切和模糊好的辦法了。上代碼(會奔潰,但圖片會出來,因此用system去調用php了來實現吧):app
<?php ini_set('display_errors', true); error_reporting(E_ALL); $filepath = '1.jpg';//原始圖片 $scale = 1.6;//縮放比例 $newfilepath = '_'.$filepath;//新位置 $blur = 25;//模糊度,越大越模糊 $W = 640;//最終寬度 $H = 960; $X = 100;//裁切的左上角x $Y = 100; list($width, $height, $type, $attr) = getimagesize($filepath); $_width = $width*$scale ; $_height = $height*$scale ; $image = new Imagick($filepath); // Resizes to whichever is larger, width or height if($image->getImageHeight() <= $image->getImageWidth()) { $image->resizeImage($_width,0,Imagick::FILTER_LANCZOS, $blur); } else { $image->resizeImage(0,$_height,Imagick::FILTER_LANCZOS, $blur); } $image->setImageCompression(Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality(75); $image->stripImage(); if($H+$Y>$image->getImageHeight()){ if($H>$image->getImageHeight()){ $H = $image->getImageHeight(); $Y = 0; }else{ $Y = $image->getImageHeight()-$H; } } if($W+$X>$image->getImageHeight()){ if($W>$image->getImageWidth()){ $W = $image->getImageWidth(); $X = 0; }else{ $X = $image->getImageWidth()-$W; } } $image->cropImage($W,$H,$X,$Y); $image->writeImage($newfilepath); $image->destroy(); echo __FILE__.'->'.__LINE__.':<br>'.chr(10);