欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php使用imagick模块实现图片缩放、裁剪、压缩示例

程序员文章站 2022-07-21 22:48:26
php 使用imagick模块 缩放,裁剪,压缩图片 包括gif图片 缩放 裁剪 复制代码 代码如下:/**  * 图片裁剪  * 裁剪规则:&nb...

php 使用imagick模块 缩放,裁剪,压缩图片 包括gif图片

缩放 裁剪

复制代码 代码如下:

/**
  * 图片裁剪
  * 裁剪规则:
  *   1. 高度为空或为零   按宽度缩放 高度自适应
  *   2. 宽度为空或为零  按高度缩放 宽度自适应
  *      3. 宽度,高度到不为空或为零  按宽高比例等比例缩放裁剪  默认从头部居中裁剪
  * @param number $width
  * @param number $height
  */
 public function resize($width=0, $height=0){
  if($width==0 && $height==0){
   return;
  }

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

  //按宽度缩放 高度自适应
  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);

    if ($this->type == 'gif') {
     $this->_resizegif($width, $height);
    }else{
     $this->image->thumbnailimage ( $width, $height, true );
    }
   }
   return;
  }

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

  //缩放后裁剪的位置
  $crop_x = 0;
  $crop_y = 0;

  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图片的处理方法

复制代码 代码如下:

/**
  * 处理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']);

   $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;
 }

保存时压缩处理

复制代码 代码如下:

// 保存到指定路径
 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 );
  }
 }

imagickservice.php

复制代码 代码如下:

<?php

/**
 * 图片处理服务类
 * 使用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() {
 }

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

 public function init(){

 }

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

 /**
  * 图片裁剪
  * 裁剪规则:
  *   1. 高度为空或为零   按宽度缩放 高度自适应
  *   2. 宽度为空或为零  按高度缩放 宽度自适应
  *      3. 宽度,高度到不为空或为零  按宽高比例等比例缩放裁剪  默认从头部居中裁剪
  * @param number $width
  * @param number $height
  */
 public function resize($width=0, $height=0){
  if($width==0 && $height==0){
   return;
  }

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

  //按宽度缩放 高度自适应
  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);

    if ($this->type == 'gif') {
     $this->_resizegif($width, $height);
    }else{
     $this->image->thumbnailimage ( $width, $height, true );
    }
   }
   return;
  }

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

  //缩放后裁剪的位置
  $crop_x = 0;
  $crop_y = 0;

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

   $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;
 }

 
 /**
  * 更改图像大小
  *  $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) {
  //压缩图片质量
  $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: 颜色: rgb 颜色 '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 ();
 } // 垂直翻转
}