【bf88必发唯一官网】php使用imagick模块达成图片缩放,文字图片水印

by admin on 2019年7月18日

正文实例陈述了php_imagick达成图片剪切、旋转、锐化、减色或追加特效的点子。分享给大家供大家参考。具体解析如下:

PHP建图平日都用GD库,因为是置于的无需在服务器上额外设置插件,所以用起来相比较简便,不过只要你的先后首要的意义正是管理图像,那麼就不建议用GD了,因为GD不但低效用何况才具也相比较弱,佔用的系统财富也颇多,其他GD的creatfrom也可能有bug,而imagick却是一个很好的替代品,为此目前把自家的二个种类由GD改成了imagick,不过改完之后出现了部分光景在此共享给大家.

PHP 使用Imagick模块 缩放,裁剪,压缩图片 包罗gif图片

一:添Gavin字水印 使用办法

贰个得以供PHP调用ImageMagick作用的PHP扩展。使用这么些扩大能够使PHP具有和ImageMagick同样的功能。

第一说一下小编那边出现的意况:

缩放 裁剪

require ‘image.class.php’
$src=”001.jpg”;
$content=”hello”;
$font_url=”my.ttf”;
$size=20;
$image=new Image($src);
$color=array(
0=>255,
1=>255,
2=>255,
2=>20
);
$local=array(
‘x’=>20,
‘y’=>30
);
$angle=10;
$image->fontMark($content,$font_url,$size,$color,$local,$angle);
$image->show();

ImageMagick是一套作用强大、稳固并且无偿的工具集和开荒包,能够用来读、写和处理超越185种为主格式的图纸文件,包蕴流行的TIFF,
JPEG, GIF, PNG,
PDF以及PhotoCD等格式。利用ImageMagick,你可以依据web应用程序的内需动态生成图片,
还是能对一个(或一组)图片展开改造大小、旋转、锐化、减色或充实特效等操作,并将操作的结果以同样格式或别的格式保存。

景况一:须要重写图像操作class

复制代码 代码如下:

二:图片缩略图
使用格局:

php_imagick是PHP对图纸管理的一个恢宏包,可以产生对图片退换加大小、旋转、锐化、减色或充实特效等操作。

现象二:imagick多线程时会导致cpu使用率暴增到百分百

/**
  * 图片裁剪
  * 裁剪法规:
  *   1. 冲天为空或为零   按上升的幅度缩放 高度自适应
  *   2. 上升的幅度为空或为零  按中度缩放 宽度自适应
  *      3. 开间,中度到不为空或为零  按宽高比例等比例缩放裁剪 
暗中认可从尾部居中裁剪
  * @param number $width
  * @param number $height
  */
 public function resize($width=0, $height=0){
  if($width==0 && $height==0){
   return;
  }

require ‘image.class.php’
$src=”001.jpg”;
$image=new Image($src);
$image->thumb(300,200);
$image->show();

一、windows下安装Imagick扩展:

在此顺便提一下imagick在centos6.4的安装方式:

  $color = ”;// ‘rgba(255,255,255,1)’;
  $size = $this->image->getImagePage ();
  //原始宽高
  $src_width = $size [‘width’];
  $src_height = $size [‘height’];

三:image.class.php

1、下载 ImageMagick并安装

1、安装ImageMagick

  //按上涨的幅度缩放 中度自适应
  if($width!=0 && $height==0){
   if($src_width>$width){
    $height = intval($width*$src_height/$src_width);

class image{
private $info;
private $image;
public function __contruct($src){
$info= getimagesize($src);
$this->info=array(
‘width’=> $info[0]【bf88必发唯一官网】php使用imagick模块达成图片缩放,文字图片水印。,
‘height’=>$info[1],
‘type’=>image_type_to_extension($info[2],false),
‘mime’=>$info[‘mime’],
 
);
$fun=”imagecreatefrom{$this->info[‘type’]}”;
 
$this->image= $fun($src);
 
}
 
//缩略图
public function thumd($width,$height){
$image_thumb= imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info[‘width’],$this->info[‘height’]);
imagedestroy($this->image);
$this->image=$image_thumb;
 
}
//文字水印
public function
fontMark($content,$font_url,$size,$color,$local,$angle){
 
$col=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
$text=imagettftext($this->image,$size,$angle,$local[‘x’],$local[‘y’],$col,$font_url,$content);
}
//输出图片
public function show()
{
header(“Content-type:”,$this->info[‘mime’]);
 
$func=”image{$this->info[‘type’]}”;
$func($this->image);
 
}
public function save($nwename){
$func=”image{$this->info[‘type’]}”;
//从内部存款和储蓄器中抽取图片展现
$func($this->image);
//保存图片
$func($this->image,$nwename.$this->info[‘type’]);
 
}
public function _destruct(){
 
imagedestroy($this->image);
}
 
}

复制代码 代码如下:

    if ($this->type == ‘gif’) {
     $this->_resizeGif($width, $height);
    }else{
     $this->image->thumbnailImage ( $width, $height, true );
    }
   }
   return;
  }
  //按中度缩放 宽度自适应
  if($width==0 && $height!=0){
   if($src_height>$height){
    $width = intval($src_width*$height/$src_height);

2、下载php_imagick.dll

wget
tar zxvf ImageMagick-6.7.1-2.tar.gz
cd ImageMagick-6.7.1-2/
./configure –prefix=/usr/local/imagemagick –disable-openmp
make && make install
ldconfig

    if ($this->type == ‘gif’) {
     $this->_resizeGif($width, $height);
    }else{
     $this->image->thumbnailImage ( $width, $height, true );
    }
   }
   return;
  }

测量检验ImageMagick是或不是可以不奇怪运维:

  //缩放的后的尺码
  $crop_w = $width;
  $crop_h = $height;

若是你用的是线程安全的php,请下载

复制代码 代码如下:

  //缩放后裁剪的地方
  $crop_x = 0;
  $crop_y = 0;

windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll

/usr/local/imagemagick/bin/convert -version

  if(($src_width/$src_height) < ($width/$height)){
   //宽高比例小于指标宽高比例  宽度等比例放大     
按目标高度从底部截取
   $crop_h = intval($src_height*$width/$src_width);
   //从最上端裁剪  不用总计 $crop_y
  }else{
   //宽高比例高于指标宽高比例   中度等比例放大      按对象宽度居中裁剪
   $crop_w = intval($src_width*$height/$src_height);
   $crop_x = intval(($crop_w-$width)/2);
  }

3、设置

2、安装PHP扩展:imagick

  if ($this->type == ‘gif’) {
   $this->_resizeGif($crop_w, $crop_h, true, $width,
$height,$crop_x, $crop_y);
  } else {
   $this->image->thumbnailImage ( $crop_w, $crop_h, true );
   $this->image->cropImage($width, $height,$crop_x, $crop_y);
  }
 }

在php.ini中添加

复制代码 代码如下:

本着gif图片的管理办法

extension=php_imagick.dll ,重启web server

wget
tar zxvf imagick-3.0.1.tgz
cd imagick-3.0.1/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
–with-imagick=/usr/local/imagemagick
make && make install
ldconfig
vi /usr/local/php/etc/php.ini
添加:extension = “imagick.so”

复制代码 代码如下:

二、linux下安装Imagick扩展:

重启lnmp

/**
  * 管理gif图片 必要对每一帧图片管理
  * @param unknown $t_w  缩放宽
  * @param unknown $t_h  缩放高
  * @param string $isCrop  是不是裁剪
  * @param number $c_w  裁剪宽
  * @param number $c_h  裁剪高
  * @param number $c_x  裁剪坐标 x
  * @param number $c_y  裁剪坐标 y
  */
 private function _resizeGif($t_w, $t_h, $isCrop=false, $c_w=0,
$c_h=0, $c_x=0, $c_y=0){
  $dest = new Imagick();
  $color_transparent = new ImagickPixel(“transparent”); //透明色
  foreach($this->image as $img){
   $page = $img->getImagePage();
   $tmp = new Imagick();
   $tmp->newImage($page[‘width’], $page[‘height’],
$color_transparent, ‘gif’);
   $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page[‘x’],
$page[‘y’]);

1.yum安装ImageMagick

bf88必发唯一官网 ,复制代码 代码如下:

   $tmp->thumbnailImage ( $t_w, $t_h, true );
   if($isCrop){
    $tmp->cropImage($c_w, $c_h, $c_x, $c_y);
   }

yum install ImageMagick ImageMagick-devel

/root/lnmp reload

   $dest->addImage($tmp);
   $dest->setImagePage($tmp->getImageWidth(),
$tmp->getImageHeight(), 0, 0);
   $dest->setImageDelay($img->getImageDelay());
   $dest->setImageDispose($img->getImageDispose());

2.测验是还是不是安装成功

接下去大家本着上述多少个情景分别建议消除办法:

  }
  $this->image->destroy ();
  $this->image = $dest;
 }

convert -version

情状一的消除办法如下:

保留时压缩管理

3.安装imagick扩展
 
01.wget xzvf
imagick-3.1.0RC2.tgz03.cd
imagick-3.1.0RC204.phpize05../configure06.make07.make install

复制代码 代码如下:

复制代码 代码如下:

4.编辑php.ini文件,在文件末尾增添如下代码

/**
    Imagick图像管理类
    用法:
        //引入Imagick物件
       
if(!defined(‘CLASS_IMAGICK’)){require(Inc.’class_imagick.php’);}
        $Imagick=new class_imagick();
        $Imagick->open(‘a.gif’);
        $Imagick->resize_to(100,100,’scale_fill’);
        $Imagick->add_text(‘1024i.com’,10,20);
        $Imagick->add_watermark(‘1024i.gif’,10,50);
        $Imagick->save_to(‘x.gif’);
        unset($Imagick);
/**/
define(‘CLASS_IMAGICK’,TRUE);
class class_imagick{
    private $image=null;
    private $type=null;
    // 构造
    public function __construct(){}
    // 析构
    public function __destruct(){
        if($this->image!==null){$this->image->destroy();}
    }
    // 载入图像
    public function open($path){
        if(!file_exists($path)){
            $this->image=null;
            return ;
        }
        $this->image=new Imagick($path);
        if($this->image){
           
$this->type=strtolower($this->image->getImageFormat());
        }
        $this->image->stripImage();
        return $this->image;
    }
    /**
        图像裁切
    /**/
    public function crop($x=0,$y=0,$width=null,$height=null){
        if($width==null)
$width=$this->image->getImageWidth()-$x;
        if($height==null)
$height=$this->image->getImageHeight()-$y;
        if($width<=0 || $height<=0) return;
        if($this->type==’gif’){
            $image=$this->image;
            $canvas=new Imagick();
            $images=$image->coalesceImages();
            foreach($images as $frame){
                $img=new Imagick();
                $img->readImageBlob($frame);
                $img->cropImage($width,$height,$x,$y);
                $canvas->addImage($img);
                $canvas->setImageDelay($img->getImageDelay());
                $canvas->setImagePage($width,$height,0,0);
            }
            $image->destroy();
            $this->image=$canvas;
        }else{
            $this->image->cropImage($width,$height,$x,$y);
        }
    }
    /**
        更动图像大小
        参数:
            $width:新的增长幅度
            $height:新的万丈
            $fit: 适应大小
                ‘force’: 把图像强制改为$width X $height
                ‘scale’: 按比例在$width X
$height内缩放图片,结果不完全等於$width X $height
                ‘scale_fill’:按百分比在$width X
$height内缩放图片,未有像素的地点填充顏色$fill_color=array(255,255,255)(红,绿,蓝,透明度[0不透明-127全透明])
                别的:智能形式,缩放图片并从中间裁切$width X
$height的分寸
        注意:
            $fit=’force’,’scale’,’scale_fill’时输出完整图像
            $fit=图像方位时输出内定地点部份的图像
        字母与图像的呼应关系如下:
            north_west   north   north_east
            west         center        east
            south_west   south   south_east
    /**/
    public function
resize_to($width=100,$height=100,$fit=’center’,$fill_color=array(255,255,255,0)){
        switch($fit){
        case ‘force’:
            if($this->type==’gif’){
                $image=$this->image;
                $canvas=new Imagick();
                $images=$image->coalesceImages();
                foreach($images as $frame){
                    $img=new Imagick();
                    $img->readImageBlob($frame);
                    $img->thumbnailImage($width,$height,false);
                    $canvas->addImage($img);
                   
$canvas->setImageDelay($img->getImageDelay());
                }
                $image->destroy();
                $this->image=$canvas;
            }else{
               
$this->image->thumbnailImage($width,$height,false);
            }
            break;
        case ‘scale’:
            if($this->type==’gif’){
                $image=$this->image;
                $images=$image->coalesceImages();
                $canvas=new Imagick();
                foreach($images as $frame){
                    $img=new Imagick();
                    $img->readImageBlob($frame);
                    $img->thumbnailImage($width,$height,true);
                    $canvas->addImage($img);
                   
$canvas->setImageDelay($img->getImageDelay());
                }
                $image->destroy();
                $this->image=$canvas;
            }else{
               
$this->image->thumbnailImage($width,$height,true);
            }
            break;
        case ‘scale_fill’:
            $size=$this->image->getImagePage();
            $src_width=$size[‘width’];
            $src_height=$size[‘height’];
            $x=0;
            $y=0;
            $dst_width=$width;
            $dst_height=$height;
            if($src_width*$height > $src_height*$width){
                $dst_height=intval($width*$src_height/$src_width);
                $y=intval(($height-$dst_height)/2);
            }else{
                $dst_width=intval($height*$src_width/$src_height);
                $x=intval(($width-$dst_width)/2);
            }
            $image=$this->image;
            $canvas=new Imagick();
           
$color=’rgba(‘.$fill_color[0].’,’.$fill_color[1].’,’.$fill_color[2].’,’.$fill_color[3].’)’;
            if($this->type==’gif’){
                $images=$image->coalesceImages();
                foreach($images as $frame){
                    $frame->thumbnailImage($width,$height,true);
                    $draw=new ImagickDraw();
                   
$draw->composite($frame->getImageCompose(),$x,$y,$dst_width,$dst_height,$frame);
                    $img=new Imagick();
                    $img->newImage($width,$height,$color,’gif’);
                    $img->drawImage($draw);
                    $canvas->addImage($img);
                   
$canvas->setImageDelay($img->getImageDelay());
                    $canvas->setImagePage($width,$height,0,0);
                }
            }else{
                $image->thumbnailImage($width,$height,true);
                $draw=new ImagickDraw();
               
$draw->composite($image->getImageCompose(),$x,$y,$dst_width,$dst_height,$image);
               
$canvas->newImage($width,$height,$color,$this->get_type());
                $canvas->drawImage($draw);
                $canvas->setImagePage($width,$height,0,0);
            }
            $image->destroy();
            $this->image=$canvas;
            break;
        default:
            $size=$this->image->getImagePage();
            $src_width=$size[‘width’];
            $src_height=$size[‘height’];
            $crop_x=0;
            $crop_y=0;
            $crop_w=$src_width;
            $crop_h=$src_height;
            if($src_width*$height > $src_height*$width){
                $crop_w=intval($src_height*$width/$height);
            }else{
                $crop_h=intval($src_width*$height/$width);
            }
            switch($fit){
                case ‘north_west’:
                    $crop_x=0;
                    $crop_y=0;
                    break;
                case ‘north’:
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=0;
                    break;
                case ‘north_east’:
                    $crop_x=$src_width-$crop_w;
                    $crop_y=0;
                    break;
                case ‘west’:
                    $crop_x=0;
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case ‘center’:
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case ‘east’:
                    $crop_x=$src_width-$crop_w;
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case ‘south_west’:
                    $crop_x=0;
                    $crop_y=$src_height-$crop_h;
                    break;
                case ‘south’:
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=$src_height-$crop_h;
                    break;
                case ‘south_east’:
                    $crop_【bf88必发唯一官网】php使用imagick模块达成图片缩放,文字图片水印。x=$src_width-$crop_w;
                    $crop_y=$src_height-$crop_h;
                    break;
                default:
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=intval(($src_height-$crop_h)/2);
            }
            $image=$this->image;
            $canvas=new Imagick();
            if($this->type==’gif’){
                $images=$image->coalesceImages();
                foreach($images as $frame){
                    $img=new Imagick();
                    $img->readImageBlob($frame);
                   
$img->cropImage($crop_w,$crop_h,$crop_x,$crop_y);
                    $img->thumbnailImage($width,$height,true);
                    $canvas->addImage($img);
                   
$canvas->setImageDelay($img->getImageDelay());
                    $canvas->setImagePage($width,$height,0,0);
                }
            }else{
               
$image->cropImage($crop_w,$crop_h,$crop_x,$crop_y);
                $image->thumbnailImage($width,$height,true);
                $canvas->addImage($image);
                $canvas->setImagePage($width,$height,0,0);
            }
            $image->destroy();
            $this->image=$canvas;
        }
    }
    /**
        增添图片水印
        参数:
            $path:水印图片(包括完整路线)
            $x,$y:水印座标
    /**/
    public function add_watermark($path,$x=0,$y=0){
        $watermark=new Imagick($path);
        $draw=new ImagickDraw();
       
$draw->composite($watermark->getImageCompose(),$x,$y,$watermark->getImageWidth(),$watermark->getimageheight(),$watermark);
        if($this->type==’gif’){
            $image=$this->image;
            $canvas=new Imagick();
            $images=$image->coalesceImages();
            foreach($image as $frame){
                $img=new Imagick();
                $img->readImageBlob($frame);
                $img->drawImage($draw);
                $canvas->addImage($img);
                $canvas->setImageDelay($img->getImageDelay());
            }
            $image->destroy();
            $this->image=$canvas;
        }else{
            $this->image->drawImage($draw);
        }
    }
    /**
        添Gavin字水印
        参数:
            $text:水印文字
            $x,$y:水印座标
    /**/
    public function
add_text($text,$x=0,$y=0,$angle=0,$style=array()){
        $draw=new ImagickDraw();
        if(isset($style[‘font’]))
$draw->setFont($style[‘font’]);
        if(isset($style[‘font_size’]))
$draw->setFontSize($style[‘font_size’]);
        if(isset($style[‘fill_color’]))
$draw->setFillColor($style[‘fill_color’]);
        if(isset($style[‘under_color’]))
$draw->setTextUnderColor($style[‘under_color’]);
        if($this->type==’gif’){
            foreach($this->image as $frame){
                $frame->annotateImage($draw,$x,$y,$angle,$text);
            }
        }else{
           
$this->image->annotateImage($draw,$x,$y,$angle,$text);
        }
    }
    /**
        图片存档
        参数:
            $path:存档的地点和新的档案名
    /**/
    public function save_to($path){
        $this->image->stripImage();
        switch($this->type){
        case ‘gif’:
            $this->image->writeImages($path,true);
            return ;
        case ‘jpg’:
        case ‘jpeg’:
           
$this->image->setImageCompressionQuality($_ENV[‘ImgQ’]);
            $this->image->writeImage($path);
            return ;
        case ‘png’:
            $flag = $this->image->getImageAlphaChannel();
            // 倘诺png背景不透明则减弱
            if(imagick::ALPHACHANNEL_UNDEFINED == $flag or
imagick::ALPHACHANNEL_DEACTIVATE == $flag){
               
$this->image->setImageType(imagick::IMGTYPE_PALETTE);
                $this->image->writeImage($path);
            }else{
                $this->image->writeImage($path);
            }unset($flag);
            return ;
        default:
            $this->image->writeImage($path);
            return ;
        }
    }
    // 直接出口图像到荧光屏
    public function output($header=true){
        if($header) header(‘Content-type: ‘.$this->type);
        echo $this->image->getImagesBlob();
    }
    /**
        创设降低图
        $fit为真时,将维持比例并在$width X $height内產生缩短图
    /**/
    public function
thumbnail($width=100,$height=100,$fit=true){$this->image->thumbnailImage($width,$height,$fit);}
    /**
        给图像增添边框
        $width: 左侧面框宽度
        $height: 上上面框宽度
        $color: 顏色
    /**/
    public function border($width,$height,$color=’rgb(220,220,220)’){
        $color=new ImagickPixel();
        $color->setColor($color);
        $this->image->borderImage($color,$width,$height);
    }
    //获得图像宽度
    public function
get_width(){$size=$this->image->getImagePage();return
$size[‘width’];}
    //取得图像中度
    public function
get_height(){$size=$this->image->getImagePage();return
$size[‘height’];}
    // 设置图像类型
    public function
set_type($type=’png’){$this->type=$type;$this->image->setImageFormat($type);}
    // 获得图像类型
    public function get_type(){return $this->type;}
    public function
blur($radius,$sigma){$this->image->blurImage($radius,$sigma);} //
模糊
    public function
gaussian_blur($radius,$sigma){$this->image->gaussianBlurImage($radius,$sigma);}
// 高斯模糊
    public function
motion_blur($radius,$sigma,$angle){$this->image->motionBlurImage($radius,$sigma,$angle);}
// 运动模糊
    public function
radial_blur($radius){$this->image->radialBlurImage($radius);} //
径向模糊
    public function
add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);}
// 加多噪点
    public function
level($black_point,$gamma,$white_point){$this->image->levelImage($black_point,$gamma,$white_point);}
// 调度色阶
    public function
modulate($brightness,$saturation,$hue){$this->image->modulateImage($brightness,$saturation,$hue);}
// 调解亮度,饱和度,色调
    public function
charcoal($radius,$sigma){$this->image->charcoalImage($radius,$sigma);}
// 壁画效果
    public function
oil_paint($radius){$this->image->oilPaintImage($radius);} //
水墨画效果
    public function flop(){$this->image->flopImage();} //
水平翻转
    public function flip(){$this->image->flipImage();} //
垂直翻转
}

// 保存到钦命路径
 public function save_to($path) {
  //压缩图片品质
  $this->image->setImageFormat(‘JPEG’);
  $this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
  $a = $this->image->getImageCompressionQuality() * 0.60;
  if ($a == 0) {
   $a = 60;
  }
  $this->image->setImageCompressionQuality($a);
  $this->image->stripImage();

extension=imagick.so

场景二的解决办法如下:

  if ($this->type == ‘gif’) {
   $this->image->writeImages ( $path, true );
  } else {
   $this->image->writeImage ( $path );
  }
 }

  1. 再一次起动apache服务器

先是用/usr/local/imagemagick/bin/convert
-version指令查看一下输出内容是不是已经开啟了四线程,Features:的值为空表达是单线程,假设Features:的值是openMP表达是八线程.imagick的八线程方式有贰个bug,他会变成多为重的cpu使用率弹指间飆升到100所以自然要动用它的单线程模式才行.

ImagickService.php

service httpd restart

复制代码 代码如下:

复制代码 代码如下:

三、案例

Version: ImageMagick 6.7.1-2 2014-05-29 Q16
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features:  

<?php

  1. 边框管理

 上面是自己安插不错开上下班时间展现的结果,若无布置不错会议及展览示下面的结果

/**
 * 图片管理服务类
 * 使用php扩大服务Imagick达成
 * ImageMagick 官方网址地址
[url]http:www.imagemagick.org/script/index.php[/url] 
 *
 * @author weiguang3
 * @since 20140403
 */
class ImagickService {
 private $image = null;
 private $type = null;

复制代码 代码如下:

复制代码 代码如下:

 // 构造函数
 public function __construct() {
 }

header(‘Content-type: image/jpeg’); 
$image = new Imagick(‘test.jpg’); 
$color=new ImagickPixel(); 
$color->setColor(“rgb(220,220,220)”); 
$image->borderImage($color,5,4); 
$image->blurImage(5,5,imagick::CHANNEL_GREEN); 
echo $image;

Version: ImageMagick 6.7.1-2 2014-05-29 Q16
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: openMP

 // 析构函数
 public function __destruct() {
  if ($this->image !== null)
   $this->image->destroy ();
 }

作者们先来看个简易的实例

 第一种结果是单线程格局,第三种结果是三十二线程格局,因为imagick的多线程形式有bug,所以假设你刚开端是用八线程方式安装的imagick那就必须要yum
remove imagemagick将其卸载掉重新安装才行.

 public function init(){

php_imagick程序示例

由此重写class,重装imagick之后一切日常,並且管理图像的职能比之之前有了大幅度晋级

 }

1.创设四个缩略图并呈现出来

 

 // 载入图像
 public function open($path) {
  $this->image = new Imagick ( $path );
  if ($this->image) {
   $this->type = strtolower ( $this->image->getImageFormat ()
);
  }
  return $this->image;
 }

复制代码 代码如下:

您只怕感兴趣的作品:

  • windows7下安装php的imagick和imagemagick扩张教程
  • 利用php_imagick完结复古效果的秘诀
  • php达成在今日头条云中央银行使imagick生成缩略图并上传的艺术
  • PHP
    Imagick完美兑现图片裁切、生成缩略图、增加水印
  • php使用Imagick生成图片的办法
  • PHP中行使imagick生成PSD文件缩略图教程
  • PHP中使用imagick达成把PDF转成图片
  • PHP中利用Imagick操作PSD文件实例
  • PHP中imagick函数的华语表明
  • PHP中选拔Imagick完结种种图片效果实例
  • php_imagick完毕图片剪切、旋转、锐化、减色或追加特效的措施
  • php
    Imagick获取图片ENVISIONGB颜色值
  • PHP使用imagick扩张实现统一图像的法子

 /**
  * 图片裁剪
  * 裁剪准绳:
  *   1. 莫斯中国科学技术大学学为空或为零   按上升的幅度缩放 中度自适应
  *   2. 宽度为空或为零  按中度缩放 宽度自适应
  *      3. 小幅度,高度到不为空或为零  按宽高比例等比例缩放裁剪 
暗中认可从头顶居中裁剪
  * @param number $width
  * @param number $height
  */
 public function resize($width=0, $height=0){
  if($width==0 && $height==0){
   return;
  }

<?php 
header(‘Content-type: image/jpeg’); 
$image = new Imagick(‘image.jpg’); 
// If 0 is provided as a width or height parameter,// aspect ratio is
maintained 
$image->thumbnailImage(100, 0); 
echo $image; 
?>

  $color = ”;// ‘rgba(255,255,255,1)’;
  $size = $this->image->getImagePage ();
  //原始宽高
  $src_width = $size [‘width’];
  $src_height = $size [‘height’];

2.开立三个索引下的缩略图,并保留

  //按上涨的幅度缩放 中度自适应
  if($width!=0 && $height==0){
   if($src_width>$width){
    $height = intval($width*$src_height/$src_width);

复制代码 代码如下:

    if ($this->type == ‘gif’) {
     $this->_resizeGif($width, $height);
    }else{
     $this->image->thumbnailImage ( $width, $height, true );
    }
   }
   return;
  }
  //按中度缩放 宽度自适应
  if($width==0 && $height!=0){
   if($src_height>$height){
    $width = intval($src_width*$height/$src_height);

<?php 
$images = new Imagick(glob(‘images/*.JPG’)); 
foreach($images as $image) { 
// Providing 0 forces thumbnailImage to maintain aspect ratio  
$image->thumbnailImage(1024,0); 

$images->writeImages(); 
?>

    if ($this->type == ‘gif’) {
     $this->_resizeGif($width, $height);
    }else{
     $this->image->thumbnailImage ( $width, $height, true );
    }
   }
   return;
  }

3.缩略GIF动画图片

  //缩放的后的尺寸
  $crop_w = $width;
  $crop_h = $height;

复制代码 代码如下:

  //缩放后裁剪的地方
  $crop_x = 0;
  $crop_y = 0;

<?php 
/* Create a new imagick object and read in GIF */
$im = new Imagick(“example.gif”); 
/* Resize all frames */
foreach ($im as $frame) { 
/* 50×50 frames */
$frame->thumbnailImage(50, 50); 
/* Set the virtual canvas to correct size */
$frame->setImagePage(50, 50, 0, 0); 
}/* Notice writeImages instead of writeImage */
$im->writeImages(“example_small.gif”, true); 
?>

  if(($src_width/$src_height) < ($width/$height)){
   //宽高比例小于指标宽高比例  宽度等比例放大     
按目的中度从尾部截取
   $crop_h = intval($src_height*$width/$src_width);
   //从顶端裁剪  不用总结 $crop_y
  }else{
   //宽高比例高于指标宽高比例   中度等比例放大      按对象宽度居中裁剪
   $crop_w = intval($src_width*$height/$src_height);
   $crop_x = intval(($crop_w-$width)/2);
  }

于今大家步入正题吧,

  if ($this->type == ‘gif’) {
   $this->_resizeGif($crop_w, $crop_h, true, $width,
$height,$crop_x, $crop_y);
  } else {
   $this->image->thumbnailImage ( $crop_w, $crop_h, true );
   $this->image->cropImage($width, $height,$crop_x, $crop_y);
  }
 }

示例:

 /**
  * 管理gif图片 须要对每一帧图片管理
  * @param unknown $t_w  缩放宽
  * @param unknown $t_h  缩放高
  * @param string $isCrop  是还是不是裁剪
  * @param number $c_w  裁剪宽
  * @param number $c_h  裁剪高
  * @param number $c_x  裁剪坐标 x
  * @param number $c_y  裁剪坐标 y
  */
 private function _resizeGif($t_w, $t_h, $isCrop=false, $c_w=0,
$c_h=0, $c_x=0, $c_y=0){
  $dest = new Imagick();
  $color_transparent = new ImagickPixel(“transparent”); //透明色
  foreach($this->image as $img){
   $page = $img->getImagePage();
   $tmp = new Imagick();
   $tmp->newImage($page[‘width’], $page[‘height’],
$color_transparent, ‘gif’);
   $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page[‘x’],
$page[‘y’]);

裁切/生成缩略图/增加水印, 自动物检疫验和拍卖 GIF

   $tmp->thumbnailImage ( $t_w, $t_h, true );
   if($isCrop){
    $tmp->cropImage($c_w, $c_h, $c_x, $c_y);
   }

调用格局:

   $dest->addImage($tmp);
   $dest->setImagePage($tmp->getImageWidth(),
$tmp->getImageHeight(), 0, 0);
   $dest->setImageDelay($img->getImageDelay());
   $dest->setImageDispose($img->getImageDispose());

复制代码 代码如下:

  }
  $this->image->destroy ();
  $this->image = $dest;
 }

include ‘imagick.class.php’; 
$image = new lib_image_imagick(); 
$image->open(‘a.gif’); 
$image->resize_to(100, 100, ‘scale_fill’); 
$image->add_text(‘1024i.com’, 10, 20); 
$image->add_watermark(‘1024i.gif’, 10, 50); 
$image->save_to(‘x.gif’);
imagick.class.php

 
 /**
  * 退换图像大小
  *  $fit: 适应大小格局
  *   ‘force’: 把图片强制变产生 $width X $height 大小
  *   ‘scale’: 按比例在平安框 $width X $height 内缩放图片,
输出缩放后图像大小 不完全等于 $width X $height
  *   ‘scale_fill’: 按百分比在安全框 $width X $height
内缩放图片,安全框内未有像素的地点填充色,
  *    使用此参数时可设置背景填充色 $bg_color =
array(255,255,255)(红,绿,蓝, 透明度)
  *    折射率(0不透明-127完全透明)) 别的: 智能模能
缩放图像并载取图像的中等部分 $width X $height 像素大小
  *  $fit = ‘force’,’scale’,’scale_fill’ 时: 输出完整图像
  *  $fit = 图像方位值 时, 输出钦赐地方某个图像
字母与图像的呼应关系如下:
  *   north_west north north_east
  *   west center east
  *   south_west south south_east
  */
 public function resize_to($width = 100, $height = 100, $fit =
‘center’, $fill_color = array(255,255,255,0)) {
  switch ($fit) {
   case ‘force’ :
    if ($this->type == ‘gif’) {
     $image = $this->image;
     $canvas = new Imagick ();

<?php 
class lib_image_imagick 

 private $image = null; 
 private $type = null; 
 // 构造函数 
 public function __construct(){} 
 
 // 析构函数 
 public function __destruct() 
 { 
     if($this->image!==null) $this->image->destroy();  
 } 
 // 载入图像 
 public function open($path) 
 { 
  $this->image = new Imagick( $path ); 
  if($this->image) 
  { 
      $this->type =
strtolower($this->image->getImageFormat()); 
  } 
  return $this->image; 
 } 
  
 public function crop($x=0, $y=0, $width=null, $height=null) 
 { 
     if($width==null) $width =
$this->image->getImageWidth()-$x; 
     if($height==null) $height =
$this->image->getImageHeight()-$y; 
     if($width<=0 || $height<=0) return; 
      
     if($this->type==’gif’) 
     { 
            $image = $this->image; 
         $canvas = new Imagick(); 
          
         $images = $image->coalesceImages(); 
         foreach($images as $frame){ 
             $img = new Imagick(); 
             $img->readImageBlob($frame); 
                $img->cropImage($width, $height, $x, $y); 
                $canvas->addImage( $img ); 
                $canvas->setImageDelay( $img->getImageDelay()
); 
                $canvas->setImagePage($width, $height, 0, 0); 
            } 
             
            $image->destroy(); 
         $this->image = $canvas; 
     } 
     else
     { 
         $this->image->cropImage($width, $height, $x, $y); 
     } 
 } 
 /*
 * 改变图像大小 
 $fit: 适应大小格局 
 ‘force’: 把图纸强制变变成 $width X $height 大小 
 ‘scale’: 按百分比在安全框 $width X $height 内缩放图片, 输出缩放后图像大小
不完全等于 $width X $height 
 ‘scale_fill’: 按比例在长治框 $width X $height
内缩放图片,安全框内未有像素的地点填充色, 使用此参数时可安装背景填充色
$bg_color = array(255,255,255)(红,绿,蓝, 反射率)
折射率(0不透明-127完全透明)) 
 别的: 智能模能 缩放图像并载取图像的中档某些 $width X $height
像素大小 
 $fit = ‘force’,’scale’,’scale_fill’ 时: 输出完整图像 
 $fit = 图像方位值 时, 输出钦定地方有个别图像  
 字母与图像的应和关系如下: 
  
 north_west   north   north_east 
  
 west         center        east 
  
 south_west   south   south_east 
  
 */
 public function resize_to($width = 100, $height = 100, $fit =
‘center’, $fill_color = array(255,255,255,0) ) 
 { 
      
     switch($fit) 
     { 
         case ‘force’: 
             if($this->type==’gif’) 
             { 
                 $image = $this->image; 
                 $canvas = new Imagick(); 
                  
                 $images = $image->coalesceImages(); 
                 foreach($images as $frame){ 
                     $img = new Imagick(); 
                     $img->readImageBlob($frame); 
                        $img->thumbnailImage( $width, $height, false
); 
                        $canvas->addImage( $img ); 
                        $canvas->setImageDelay(
$img->getImageDelay() ); 
                    } 
                    $image->destroy(); 
                 $this->image = $canvas; 
             } 
             else
             { 
                 $this->image->thumbnailImage( $width, $height,
false ); 
             } 
             break; 
         case ‘scale’: 
             if($this->type==’gif’) 
             { 
                 $image = $this->image; 
                 $images = $image->coalesceImages(); 
                 $canvas = new Imagick(); 
                 foreach($images as $frame){ 
                     $img = new Imagick(); 
                     $img->readImageBlob($frame); 
                        $img->thumbnailImage( $width, $height, true
); 
                        $canvas->addImage( $img ); 
                        $canvas->setImageDelay(
$img->getImageDelay() ); 
                    } 
                    $image->destroy(); 
                 $this->image = $canvas; 
             } 
             else
             { 
                 $this->image->thumbnailImage( $width, $height,
true ); 
             } 
             break; 
         case ‘scale_fill’: 
             $size = $this->image->getImagePage();  
             $src_width = $size[‘width’]; 
             $src_height = $size[‘height’]; 
              
                $x = 0; 
                $y = 0; 
                 
                $dst_width = $width; 
                $dst_height = $height; 
    if($src_width*$height > $src_height*$width) 
    { 
     $dst_height = intval($width*$src_height/$src_width); 
     $y = intval( ($height-$dst_height)/2 ); 
    } 
    else
    { 
     $dst_width = intval($height*$src_width/$src_height); 
     $x = intval( ($width-$dst_width)/2 ); 
    } 
                $image = $this->image; 
                $canvas = new Imagick(); 
                 
                $color =
‘rgba(‘.$fill_color[0].’,’.$fill_color[1].’,’.$fill_color[2].’,’.$fill_color[3].’)’; 
             if($this->type==’gif’) 
             { 
                 $images = $image->coalesceImages(); 
                 foreach($images as $frame) 
                 { 
                     $frame->thumbnailImage( $width, $height, true
); 
                     $draw = new ImagickDraw(); 
                       
$draw->composite($frame->getImageCompose(), $x, $y, $dst_width,
$dst_height, $frame); 
                        $img = new Imagick(); 
                        $img->newImage($width, $height, $color,
‘gif’); 
                        $img->drawImage($draw); 
                        $canvas->addImage( $img ); 
                        $canvas->setImageDelay(
$img->getImageDelay() ); 
                        $canvas->setImagePage($width, $height, 0,
0); 
                    } 
             } 
             else
             { 
                 $image->thumbnailImage( $width, $height, true ); 
                  
                 $draw = new ImagickDraw(); 
                    $draw->composite($image->getImageCompose(),
$x, $y, $dst_width, $dst_height, $image); 
                     
                 $canvas->newImage($width, $height, $color,
$this->get_type() ); 
                    $canvas->drawImage($draw); 
                    $canvas->setImagePage($width, $height, 0, 0); 
             } 
             $image->destroy(); 
             $this->image = $canvas; 
             break; 
   default: 
    $size = $this->image->getImagePage();  
       $src_width = $size[‘width’]; 
             $src_height = $size[‘height’]; 
              
                $crop_x = 0; 
                $crop_y = 0; 
                 
                $crop_w = $src_width; 
                $crop_h = $src_height; 
                 
          if($src_width*$height > $src_height*$width) 
    { 
     $crop_w = intval($src_height*$width/$height); 
    } 
    else
    { 
        $crop_h = intval($src_width*$height/$width); 
    } 
                 
       switch($fit) 
             { 
        case ‘north_west’: 
            $crop_x = 0; 
            $crop_y = 0; 
            break; 
           case ‘north’: 
               $crop_x = intval( ($src_width-$crop_w)/2 ); 
               $crop_y = 0; 
               break; 
           case ‘north_east’: 
               $crop_x = $src_width-$crop_w; 
               $crop_y = 0; 
               break; 
           case ‘west’: 
               $crop_x = 0; 
               $crop_y = intval( ($src_height-$crop_h)/2 ); 
               break; 
           case ‘center’: 
               $crop_x = intval( ($src_width-$crop_w)/2 ); 
               $crop_y = intval( ($src_height-$crop_h)/2 ); 
               break; 
           case ‘east’: 
               $crop_x = $src_width-$crop_w; 
               $crop_y = intval( ($src_height-$crop_h)/2 ); 
               break; 
           case ‘south_west’: 
               $crop_x = 0; 
               $crop_y = $src_height-$crop_h; 
               break; 
           case ‘south’: 
               $crop_x = intval( ($src_width-$crop_w)/2 ); 
               $crop_y = $src_height-$crop_h; 
               break; 
           case ‘south_east’: 
               $crop_x = $src_width-$crop_w; 
               $crop_y = $src_height-$crop_h; 
               break; 
           default: 
               $crop_x = intval( ($src_width-$crop_w)/2 ); 
               $crop_y = intval( ($src_height-$crop_h)/2 ); 
             } 
              
             $image = $this->image; 
             $canvas = new Imagick(); 
              
          if($this->type==’gif’) 
             { 
                 $images = $image->coalesceImages(); 
                 foreach($images as $frame){ 
                     $img = new Imagick(); 
                     $img->readImageBlob($frame); 
                        $img->cropImage($crop_w, $crop_h, $crop_x,
$crop_y); 
                        $img->thumbnailImage( $width, $height, true
); 
                         
                        $canvas->addImage( $img ); 
                        $canvas->setImageDelay(
$img->getImageDelay() ); 
                        $canvas->setImagePage($width, $height, 0,
0); 
                    } 
             } 
             else
             { 
                 $image->cropImage($crop_w, $crop_h, $crop_x,
$crop_y); 
                 $image->thumbnailImage( $width, $height, true ); 
                 $canvas->addImage( $image ); 
                 $canvas->setImagePage($width, $height, 0, 0); 
             } 
             $image->destroy(); 
             $this->image = $canvas; 
     } 
      
 } 
  
 // 增添水印图片 
 public function add_watermark($path, $x = 0, $y = 0) 
 { 
        $watermark = new Imagick($path); 
        $draw = new ImagickDraw(); 
        $draw->composite($watermark->getImageCompose(), $x, $y,
$watermark->getImageWidth(), $watermark->getimageheight(),
$watermark); 
     if($this->type==’gif’) 
     { 
         $image = $this->image; 
            $canvas = new Imagick(); 
         $images = $image->coalesceImages(); 
         foreach($image as $frame) 
         { 
                $img = new Imagick(); 
             $img->readImageBlob($frame); 
                $img->drawImage($draw); 
                 
                $canvas->addImage( $img ); 
                $canvas->setImageDelay( $img->getImageDelay()
); 
            } 
            $image->destroy(); 
         $this->image = $canvas; 
     } 
     else
     { 
         $this->image->drawImage($draw); 
     } 
 } 
  
 // 增多水印文字 
 public function add_text($text, $x = 0 , $y = 0, $angle=0,
$style=array()) 
 { 
        $draw = new ImagickDraw(); 
        if(isset($style[‘font’]))
$draw->setFont($style[‘font’]); 
        if(isset($style[‘font_size’]))
$draw->setFontSize($style[‘font_size’]); 
     if(isset($style[‘fill_color’]))
$draw->setFillColor($style[‘fill_color’]); 
     if(isset($style[‘under_color’]))
$draw->setTextUnderColor($style[‘under_color’]); 
      
     if($this->type==’gif’) 
     { 
         foreach($this->image as $frame) 
         { 
             $frame->annotateImage($draw, $x, $y, $angle, $text); 
         } 
     } 
     else
     { 
         $this->image->annotateImage($draw, $x, $y, $angle,
$text); 
     } 
 } 
  
 // 保存到内定路线 
 public function save_to( $path ) 
 { 
     if($this->type==’gif’) 
     { 
         $this->image->writeImages($path, true); 
     } 
     else
     { 
         $this->image->writeImage($path); 
     } 
 } 
 // 输出图像 
 public function output($header = true) 
 { 
     if($header) header(‘Content-type: ‘.$this->type); 
     echo $this->image->getImagesBlob();   
 } 
  
 public function get_width() 
 { 
        $size = $this->image->getImagePage();  
        return $size[‘width’]; 
 } 
  
 public function get_height() 
 { 
     $size = $this->image->getImagePage();  
        return $size[‘height’]; 
 } 
 // 设置图像类型, 私下认可与源类型一致 
 public function set_type( $type=’png’ ) 
 { 
     $this->type = $type; 
        $this->image->setImageFormat( $type ); 
 } 
 // 获取源图像类型 
 public function get_type() 
 { 
  return $this->type; 
 } 
 
 // 当前指标是还是不是为图片 
 public function is_image() 
 { 
  if( $this->image ) 
   return true; 
  else
   return false; 
 } 
 
 public function thumbnail($width = 100, $height = 100, $fit = true){
$this->image->thumbnailImage( $width, $height, $fit );} //
生成缩略图 $fit为真时将保障比例并在拉萨框 $width X $height
内生成缩略图片 
 /*
 增多二个边框 
 $width: 左侧面框宽度 
 $height: 上下面框宽度 
 $color: 颜色: CRUISERGB 颜色 ‘rgb(255,0,0)’ 或 16进制颜色 ‘#FF0000’
或颜料单词 ‘white’/’red’… 
 */
 public function border($width, $height, $color=’rgb(220, 220, 220)’) 
 { 
  $color=new ImagickPixel(); 
  $color->setColor($color); 
  $this->image->borderImage($color, $width, $height); 
 } 
  
 public function blur($radius,
$sigma){$this->image->blurImage($radius, $sigma);} // 模糊 
 public function gaussian_blur($radius,
$sigma){$this->image->gaussianBlurImage($radius, $sigma);} //
高斯模糊 
 public function motion_blur($radius, $sigma,
$angle){$this->image->motionBlurImage($radius, $sigma, $angle);}
// 运动模糊 
 public function
radial_blur($radius){$this->image->radialBlurImage($radius);} //
径向模糊 
 public function
add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);}
// 加多噪点 
  
 public function level($black_point, $gamma,
$white_point){$this->image->levelImage($black_point, $gamma,
$white_point);} // 调治色阶 
 public function modulate($brightness, $saturation,
$hue){$this->image->modulateImage($brightness, $saturation,
$hue);} // 调度亮度、饱和度、色调 
 public function charcoal($radius,
$sigma){$this->image->charcoalImage($radius, $sigma);} // 素描 
 public function
oil_paint($radius){$this->image->oilPaintImage($radius);} //
摄影效果 
  
 public function flop(){$this->image->flopImage();} // 水平翻转 
 public function flip(){$this->image->flipImage();} // 垂直翻转 
}

     $images = $image->coalesceImages ();
     foreach ( $images as $frame ) {
      $img = new Imagick ();
      $img->readImageBlob ( $frame );
      $img->thumbnailImage ( $width, $height, false );

梦想本文所述对大家的PHP程序设计具备匡助。

      $canvas->addImage ( $img );
      $canvas->setImageDelay ( $img->getImageDelay () );
     }
     $image->destroy ();
     $this->image = $canvas;
    } else {
     $this->image->thumbnailImage ( $width, $height, false );
    }
    break;
   case ‘scale’ :
    if ($this->type == ‘gif’) {
     $image = $this->image;
     $images = $image->coalesceImages ();
     $canvas = new Imagick ();
     foreach ( $images as $frame ) {
      $img = new Imagick ();
      $img->readImageBlob ( $frame );
      $img->thumbnailImage ( $width, $height, true );

你恐怕感兴趣的文章:

  • php基于CodeIgniter达成图片上传、剪切作用
  • php完毕图片上传、剪切成效
  • php等比例缩放图片及剪切图片代码共享
  • php图片水印增添,压缩,剪切的封装类达成
  • PHP增多图片水印、压缩、剪切的封装类
  • php工具型代码之印章抠图

      $canvas->addImage ( $img );
      $canvas->setImageDelay ( $img->getImageDelay () );
     }
     $image->destroy ();
     $this->image = $canvas;
    } else {
     $this->image->thumbnailImage ( $width, $height, true );
    }
    break;
   case ‘scale_fill’ :
    $size = $this->image->getImagePage ();
    $src_width = $size [‘width’];
    $src_height = $size [‘height’];

    $x = 0;
    $y = 0;

    $dst_width = $width;
    $dst_height = $height;

    if ($src_width * $height > $src_height * $width) {
     $dst_height = intval ( $width * $src_height / $src_width );
     $y = intval ( ($height – $dst_height) / 2 );
    } else {
     $dst_width = intval ( $height * $src_width / $src_height );
     $x = intval ( ($width – $dst_width) / 2 );
    }

    $image = $this->image;
    $canvas = new Imagick ();

    $color = ‘rgba(‘ . $fill_color [0] . ‘,’ . $fill_color [1] .
‘,’ . $fill_color [2] . ‘,’ . $fill_color [3] . ‘)’;
    if ($this->type == ‘gif’) {
     $images = $image->coalesceImages ();
     foreach ( $images as $frame ) {
      $frame->thumbnailImage ( $width, $height, true );

      $draw = new ImagickDraw ();
      $draw->composite ( $frame->getImageCompose (), $x, $y,
$dst_width, $dst_height, $frame );

      $img = new Imagick ();
      $img->newImage ( $width, $height, $color, ‘gif’ );
      $img->drawImage ( $draw );

      $canvas->addImage ( $img );
      $canvas->setImageDelay ( $img->getImageDelay () );
      $canvas->setImagePage ( $width, $height, 0, 0 );
     }
    } else {
     $image->thumbnailImage ( $width, $height, true );

     $draw = new ImagickDraw ();
     $draw->composite ( $image->getImageCompose (), $x, $y,
$dst_width, $dst_height, $image );

     $canvas->newImage ( $width, $height, $color, $this->get_type
() );
     $canvas->drawImage ( $draw );
     $canvas->setImagePage ( $width, $height, 0, 0 );
    }
    $image->destroy ();
    $this->image = $canvas;
    break;
   default :
    $size = $this->image->getImagePage ();
    $src_width = $size [‘width’];
    $src_height = $size [‘height’];

    $crop_x = 0;
    $crop_y = 0;

    $crop_w = $src_width;
    $crop_h = $src_height;

    if ($src_width * $height > $src_height * $width) {
     $crop_w = intval ( $src_height * $width / $height );
    } else {
     $crop_h = intval ( $src_width * $height / $width );
    }

    switch ($fit) {
     case ‘north_west’ :
      $crop_x = 0;
      $crop_y = 0;
      break;
     case ‘north’ :
      $crop_x = intval ( ($src_width – $crop_w) / 2 );
      $crop_y = 0;
      break;
     case ‘north_east’ :
      $crop_x = $src_width – $crop_w;
      $crop_y = 0;
      break;
     case ‘west’ :
      $crop_x = 0;
      $crop_y = intval ( ($src_height – $crop_h) / 2 );
      break;
     case ‘center’ :
      $crop_x = intval ( ($src_width – $crop_w) / 2 );
      $crop_y = intval ( ($src_height – $crop_h) / 2 );
      break;
     case ‘east’ :
      $crop_x = $src_width – $crop_w;
      $crop_y = intval ( ($src_height – $crop_h) / 2 );
      break;
     case ‘south_west’ :
      $crop_x = 0;
      $crop_y = $src_height – $crop_h;
      break;
     case ‘south’ :
      $crop_x = intval ( ($src_width – $crop_w) / 2 );
      $crop_y = $src_height – $crop_h;
      break;
     case ‘south_east’ :
      $crop_x = $src_width – $crop_w;
      $crop_y = $src_height – $crop_h;
      break;
     default :
      $crop_x = intval ( ($src_width – $crop_w) / 2 );
      $crop_y = intval ( ($src_height – $crop_h) / 2 );
    }

    $image = $this->image;
    $canvas = new Imagick ();

    if ($this->type == ‘gif’) {
     $images = $image->coalesceImages ();
     foreach ( $images as $frame ) {
      $img = new Imagick ();
      $img->readImageBlob ( $frame );
      $img->cropImage ( $crop_w, $crop_h, $crop_x, $crop_y );
      $img->thumbnailImage ( $width, $height, true );

      $canvas->addImage ( $img );
      $canvas->setImageDelay ( $img->getImageDelay () );
      $canvas->setImagePage ( $width, $height, 0, 0 );
     }
    } else {
     $image->cropImage ( $crop_w, $crop_h, $crop_x, $crop_y );
     $image->thumbnailImage ( $width, $height, true );
     $canvas->addImage ( $image );
     $canvas->setImagePage ( $width, $height, 0, 0 );
    }
    $image->destroy ();
    $this->image = $canvas;
  }
 }

 // 增多水印图片
 public function add_watermark($path, $x = 0, $y = 0) {
  $watermark = new Imagick ( $path );
  $draw = new ImagickDraw ();
  $draw->composite ( $watermark->getImageCompose (), $x, $y,
$watermark->getImageWidth (), $watermark->getimageheight (),
$watermark );

  if ($this->type == ‘gif’) {
   $image = $this->image;
   $canvas = new Imagick ();
   $images = $image->coalesceImages ();
   foreach ( $image as $frame ) {
    $img = new Imagick ();
    $img->readImageBlob ( $frame );
    $img->drawImage ( $draw );

    $canvas->addImage ( $img );
    $canvas->setImageDelay ( $img->getImageDelay () );
   }
   $image->destroy ();
   $this->image = $canvas;
  } else {
   $this->image->drawImage ( $draw );
  }
 }

 // 增加水印文字
 public function add_text($text, $x = 0, $y = 0, $angle = 0, $style =
array()) {
  $draw = new ImagickDraw ();
  if (isset ( $style [‘font’] ))
   $draw->setFont ( $style [‘font’] );
  if (isset ( $style [‘font_size’] ))
   $draw->setFontSize ( $style [‘font_size’] );
  if (isset ( $style [‘fill_color’] ))
   $draw->setFillColor ( $style [‘fill_color’] );
  if (isset ( $style [‘under_color’] ))
   $draw->setTextUnderColor ( $style [‘under_color’] );

  if ($this->type == ‘gif’) {
   foreach ( $this->image as $frame ) {
    $frame->annotateImage ( $draw, $x, $y, $angle, $text );
   }
  } else {
   $this->image->annotateImage ( $draw, $x, $y, $angle, $text );
  }
 }

 // 保存到钦赐路径
 public function save_to($path) {
  //压缩图片质量
  $this->image->setImageFormat(‘JPEG’);
  $this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
  $a = $this->image->getImageCompressionQuality() * 0.60;
  if ($a == 0) {
   $a = 60;
  }
  $this->image->setImageCompressionQuality($a);
  $this->image->stripImage();

  if ($this->type == ‘gif’) {
   $this->image->writeImages ( $path, true );
  } else {
   $this->image->writeImage ( $path );
  }
 }

 // 输出图像
 public function output($header = true) {
  if ($header)
   header ( ‘Content-type: ‘ . $this->type );
  echo $this->image->getImagesBlob ();
 }
 public function get_width() {
  $size = $this->image->getImagePage ();
  return $size [‘width’];
 }
 public function get_height() {
  $size = $this->image->getImagePage ();
  return $size [‘height’];
 }

 // 设置图像类型, 暗中同意与源类型一致
 public function set_type($type = ‘png’) {
  $this->type = $type;
  $this->image->setImageFormat ( $type );
 }

 // 获取源图像类型
 public function get_type() {
  return $this->type;
 }

 public function get_file_size(){
  if($this->image){
   return 0;//$this->image->getImageLength(); getImageLength not
find
  }else{
   return 0;
  }
 }

 public function get_file_type(){
  if($this->image){
   return $this->image->getimagemimetype();
  }else{
   return 0;
  }
 }

 public function get_sha1(){
  if($this->image){
   return sha1($this->image->__tostring());
  }else{
   return ”;
  }
 }

 // 当前指标是还是不是为图片
 public function is_image() {
  if ($this->image)
   return true;
  else
   return false;
 }

 /*
  * 增多三个边框 $width: 左侧面框宽度 $height: 上下边框宽度 $color:
颜色: ENCOREGB 颜色 ‘rgb(255,0,0)’ 或 16进制颜色 ‘#FF0000’ 或颜料单词
‘white’/’red’…
  */
 public function border($width, $height, $color = ‘rgb(220, 220, 220)’)
{
  $color = new ImagickPixel ();
  $color->setColor ( $color );
  $this->image->borderImage ( $color, $width, $height );
 }
 public function blur($radius, $sigma) {
  $this->image->blurImage ( $radius, $sigma );
 } // 模糊
 public function gaussian_blur($radius, $sigma) {
  $this->image->gaussianBlurImage ( $radius, $sigma );
 } // 高斯模糊
 public function motion_blur($radius, $sigma, $angle) {
  $this->image->motionBlurImage ( $radius, $sigma, $angle );
 } // 运动模糊
 public function radial_blur($radius) {
  $this->image->radialBlurImage ( $radius );
 } // 径向模糊
 public function add_noise($type = null) {
  $this->image->addNoiseImage ( $type == null ?
imagick::NOISE_IMPULSE : $type );
 } // 增添噪点
 public function level($black_point, $gamma, $white_point) {
  $this->image->levelImage ( $black_point, $gamma, $white_point
);
 } // 调度色阶
 public function modulate($brightness, $saturation, $hue) {
  $this->image->modulateImage ( $brightness, $saturation, $hue
);
 } // 调治亮度、饱和度、色调
 public function charcoal($radius, $sigma) {
  $this->image->charcoalImage ( $radius, $sigma );
 } // 素描
 public function oil_paint($radius) {
  $this->image->oilPaintImage ( $radius );
 } // 雕塑效果
 public function flop() {
  $this->image->flopImage ();
 } // 水平翻转
 public function flip() {
  $this->image->flipImage ();
 } // 垂直翻转
}

你或然感兴趣的稿子:

  • php中十二个不等阶段压缩优化图片操作示例
  • PHP
    完成等比压缩图片尺寸和尺寸实例代码
  • php
    gd等比例缩放压缩图片函数
  • 依据PHP完毕等比压缩图片大小
  • php上传图片并压缩的落到实处形式
  • PHP完成图片上传并压缩
  • PHP完毕图片压缩的两则实例
  • PHP等比例缩小图片的实例代码

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图