侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP Imagick完美实现图片裁切、生成缩略图、添加水印

php  /  管理员 发布于 5年前   375

本文实例讲解了PHP使用Imagick 裁切、生成缩略图、添加水印自动检测和处理,支持gif,分享给大家供大家参考,具体内容如下

调用方式:

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

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: 颜色: 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();} // 垂直翻转  } 

希望本文所述对大家学习php程序设计有所帮助。

您可能感兴趣的文章:

  • PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
  • PHP图像处理技术实例总结【绘图、水印、验证码、图像压缩】
  • 用来给图片加水印的PHP类
  • php给图片添加文字水印方法汇总
  • PHP图片处理之使用imagecopy函数添加图片水印实例
  • 超级好用的一个php上传图片类(随机名,缩略图,加水印)
  • php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
  • php下图片文字混合水印与缩略图实现代码
  • php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
  • php文字水印和php图片水印实现代码(二种加水印方法)
  • PHP图像处理 imagestring添加图片水印与文字水印操作示例


  • 上一条:
    PHP IPV6正则表达式验证代码
    下一条:
    PHP JWT初识及其简单示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客