您的位置:首页 > 其它

Imagick 缩放图片和实现模糊

2015-08-12 14:43 351 查看
Imagick功能相当的多,只是还不稳定,我下面的程序能够运行,但是会出现内存错误,但我们要的图片还是能够得到。

弄这个的原因是,一个客户要求在一个appcan的应用里面实现一个页面的背景图的缩放、调整位置和模糊效果。

这些js和css都能实现,在电脑上表现得很好,但是到了手机上,模糊就出了问题,而她又要求背景不动,调整了位置就设置了背景图的x y,但不动的话又要求背景位置fixed,所以我加了div,位置fixed,再给它放上背景图,在电脑上好好的,到手机上fixed就失效了。

我只好采取在服务器端裁切和模糊好的办法了。上代码(会奔溃,但图片会出来,所以用system去调用php了来实现吧):

<?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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: