zoukankan      html  css  js  c++  java
  • 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);


     

  • 相关阅读:
    9、搜索 :ion-searchbar
    8、列表:ion-list
    uwp 的work project 的 取消闹钟
    long ? 的使用和理解
    uwp 中的音频开发
    uwp 之语音朗读
    uwp 语音指令
    C# 泛型(Generic)
    C# 排序列表(SortedList)
    C# 反射(Reflection)
  • 原文地址:https://www.cnblogs.com/lein317/p/5067516.html
Copyright © 2011-2022 走看看