Imagick 缩放图片和实现模糊

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);


 

转载于:https://www.cnblogs.com/lein317/p/5067516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值