找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3691|回复: 0

PHP 使用 Imagick 裁切/生成缩略图/添加水印, 自动检测和处理

[复制链接]
发表于 2012-1-20 11:31:57 | 显示全部楼层 |阅读模式

给骨头系统开发的图像库的 imagick 部分 ,支持 gif , 完美支持裁切、生成缩略图、添加水印 。
支持按方位生成缩略图像, 如:
// 把左上角优先
$image->resize_to(100, 100, 'north_west');
// 右边优先
$image->resize_to(100, 100, 'east');

...
更多参数看源代码
原图


效果图:



调用方式:
include 'imagick.class.php';
  1. $image = new lib_image_imagick();
  2. $image->open('a.gif');
  3. $image->resize_to(100, 100, 'scale_fill');
  4. $image->add_text('1024i.com', 10, 20);
  5. $image->add_watermark('1024i.gif', 10, 50);
  6. $image->save_to('x.gif');
复制代码
imagick.class.php
  1. <?php
  2. /*
  3. @版本日期: 版本日期: 2012年1月18日
  4. @著作权所有: 1024 intelligence ( http://www.1024i.com )
  5. 获得使用本类库的许可, 您必须保留著作权声明信息.
  6. 报告漏洞,意见或建议, 请联系 Lou Barnes(iua1024@gmail.com)
  7. */
  8. class lib_image_imagick
  9. {
  10.         private $image = null;
  11.         private $type = null;
  12.         // 构造函数
  13.         public function __construct(){}
  14.         // 析构函数
  15.         public function __destruct()
  16.         {
  17.             if($this->image!==null) $this->image->destroy();
  18.         }
  19.         // 载入图像
  20.         public function open($path)
  21.         {
  22.                 $this->image = new Imagick( $path );
  23.                 if($this->image)
  24.                 {
  25.                     $this->type = strtolower($this->image->getImageFormat());
  26.                 }
  27.                 return $this->image;
  28.         }
  29.        
  30.         public function crop($x=0, $y=0, $width=null, $height=null)
  31.         {
  32.             if($width==null) $width = $this->image->getImageWidth()-$x;
  33.             if($height==null) $height = $this->image->getImageHeight()-$y;
  34.             if($width<=0 || $height<=0) return;
  35.             
  36.             if($this->type=='gif')
  37.             {
  38.             $image = $this->image;
  39.                 $canvas = new Imagick();
  40.                 
  41.                 $images = $image->coalesceImages();
  42.                 foreach($images as $frame){
  43.                     $img = new Imagick();
  44.                     $img->readImageBlob($frame);
  45.                 $img->cropImage($width, $height, $x, $y);
  46.                 $canvas->addImage( $img );
  47.                 $canvas->setImageDelay( $img->getImageDelay() );
  48.                 $canvas->setImagePage($width, $height, 0, 0);
  49.             }
  50.             
  51.             $image->destroy();
  52.                 $this->image = $canvas;
  53.             }
  54.             else
  55.             {
  56.                 $this->image->cropImage($width, $height, $x, $y);
  57.             }
  58.         }
  59.         /*
  60.         * 更改图像大小
  61.         $fit: 适应大小方式
  62.         'force': 把图片强制变形成 $width X $height 大小
  63.         'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
  64.         'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明))
  65.         其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
  66.         $fit = 'force','scale','scale_fill' 时: 输出完整图像
  67.         $fit = 图像方位值 时, 输出指定位置部分图像
  68.         字母与图像的对应关系如下:
  69.        
  70.         north_west   north   north_east
  71.        
  72.         west         center        east
  73.        
  74.         south_west   south   south_east
  75.        
  76.         */
  77.         public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255,255,255,0) )
  78.         {
  79.             
  80.             switch($fit)
  81.             {
  82.                 case 'force':
  83.                     if($this->type=='gif')
  84.                     {
  85.                         $image = $this->image;
  86.                         $canvas = new Imagick();
  87.                         
  88.                         $images = $image->coalesceImages();
  89.                         foreach($images as $frame){
  90.                             $img = new Imagick();
  91.                             $img->readImageBlob($frame);
  92.                         $img->thumbnailImage( $width, $height, false );
  93.                         $canvas->addImage( $img );
  94.                         $canvas->setImageDelay( $img->getImageDelay() );
  95.                     }
  96.                     $image->destroy();
  97.                         $this->image = $canvas;
  98.                     }
  99.                     else
  100.                     {
  101.                         $this->image->thumbnailImage( $width, $height, false );
  102.                     }
  103.                     break;
  104.                 case 'scale':
  105.                     if($this->type=='gif')
  106.                     {
  107.                         $image = $this->image;
  108.                         $images = $image->coalesceImages();
  109.                         $canvas = new Imagick();
  110.                         foreach($images as $frame){
  111.                             $img = new Imagick();
  112.                             $img->readImageBlob($frame);
  113.                         $img->thumbnailImage( $width, $height, true );
  114.                         $canvas->addImage( $img );
  115.                         $canvas->setImageDelay( $img->getImageDelay() );
  116.                     }
  117.                     $image->destroy();
  118.                         $this->image = $canvas;
  119.                     }
  120.                     else
  121.                     {
  122.                         $this->image->thumbnailImage( $width, $height, true );
  123.                     }
  124.                     break;
  125.                 case 'scale_fill':
  126.                     $size = $this->image->getImagePage();
  127.                     $src_width = $size['width'];
  128.                     $src_height = $size['height'];
  129.                    
  130.                 $x = 0;
  131.                 $y = 0;
  132.                
  133.                 $dst_width = $width;
  134.                 $dst_height = $height;
  135.                             if($src_width*$height > $src_height*$width)
  136.                                 {
  137.                                         $dst_height = intval($width*$src_height/$src_width);
  138.                                         $y = intval( ($height-$dst_height)/2 );
  139.                                 }
  140.                                 else
  141.                                 {
  142.                                         $dst_width = intval($height*$src_width/$src_height);
  143.                                         $x = intval( ($width-$dst_width)/2 );
  144.                                 }
  145.                 $image = $this->image;
  146.                 $canvas = new Imagick();
  147.                
  148.                 $color = 'rgba('.$fill_color[0].','.$fill_color[1].','.$fill_color[2].','.$fill_color[3].')';
  149.                     if($this->type=='gif')
  150.                     {
  151.                         $images = $image->coalesceImages();
  152.                         foreach($images as $frame)
  153.                         {
  154.                             $frame->thumbnailImage( $width, $height, true );
  155.                             $draw = new ImagickDraw();
  156.                         $draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
  157.                         $img = new Imagick();
  158.                         $img->newImage($width, $height, $color, 'gif');
  159.                         $img->drawImage($draw);
  160.                         $canvas->addImage( $img );
  161.                         $canvas->setImageDelay( $img->getImageDelay() );
  162.                         $canvas->setImagePage($width, $height, 0, 0);
  163.                     }
  164.                     }
  165.                     else
  166.                     {
  167.                         $image->thumbnailImage( $width, $height, true );
  168.                         
  169.                         $draw = new ImagickDraw();
  170.                     $draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
  171.                     
  172.                         $canvas->newImage($width, $height, $color, $this->get_type() );
  173.                     $canvas->drawImage($draw);
  174.                     $canvas->setImagePage($width, $height, 0, 0);
  175.                     }
  176.                     $image->destroy();
  177.                     $this->image = $canvas;
  178.                     break;
  179.                         default:
  180.                                 $size = $this->image->getImagePage();
  181.                             $src_width = $size['width'];
  182.                     $src_height = $size['height'];
  183.                    
  184.                 $crop_x = 0;
  185.                 $crop_y = 0;
  186.                
  187.                 $crop_w = $src_width;
  188.                 $crop_h = $src_height;
  189.                
  190.                         if($src_width*$height > $src_height*$width)
  191.                                 {
  192.                                         $crop_w = intval($src_height*$width/$height);
  193.                                 }
  194.                                 else
  195.                                 {
  196.                                     $crop_h = intval($src_width*$height/$width);
  197.                                 }
  198.                
  199.                             switch($fit)
  200.                     {
  201.                                     case 'north_west':
  202.                                         $crop_x = 0;
  203.                                         $crop_y = 0;
  204.                                         break;
  205.                                 case 'north':
  206.                                     $crop_x = intval( ($src_width-$crop_w)/2 );
  207.                                     $crop_y = 0;
  208.                                     break;
  209.                                 case 'north_east':
  210.                                     $crop_x = $src_width-$crop_w;
  211.                                     $crop_y = 0;
  212.                                     break;
  213.                                 case 'west':
  214.                                     $crop_x = 0;
  215.                                     $crop_y = intval( ($src_height-$crop_h)/2 );
  216.                                     break;
  217.                                 case 'center':
  218.                                     $crop_x = intval( ($src_width-$crop_w)/2 );
  219.                                     $crop_y = intval( ($src_height-$crop_h)/2 );
  220.                                     break;
  221.                                 case 'east':
  222.                                     $crop_x = $src_width-$crop_w;
  223.                                     $crop_y = intval( ($src_height-$crop_h)/2 );
  224.                                     break;
  225.                                 case 'south_west':
  226.                                     $crop_x = 0;
  227.                                     $crop_y = $src_height-$crop_h;
  228.                                     break;
  229.                                 case 'south':
  230.                                     $crop_x = intval( ($src_width-$crop_w)/2 );
  231.                                     $crop_y = $src_height-$crop_h;
  232.                                     break;
  233.                                 case 'south_east':
  234.                                     $crop_x = $src_width-$crop_w;
  235.                                     $crop_y = $src_height-$crop_h;
  236.                                     break;
  237.                                 default:
  238.                                     $crop_x = intval( ($src_width-$crop_w)/2 );
  239.                                     $crop_y = intval( ($src_height-$crop_h)/2 );
  240.                     }
  241.                    
  242.                     $image = $this->image;
  243.                     $canvas = new Imagick();
  244.                    
  245.                         if($this->type=='gif')
  246.                     {
  247.                         $images = $image->coalesceImages();
  248.                         foreach($images as $frame){
  249.                             $img = new Imagick();
  250.                             $img->readImageBlob($frame);
  251.                         $img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  252.                         $img->thumbnailImage( $width, $height, true );
  253.                         
  254.                         $canvas->addImage( $img );
  255.                         $canvas->setImageDelay( $img->getImageDelay() );
  256.                         $canvas->setImagePage($width, $height, 0, 0);
  257.                     }
  258.                     }
  259.                     else
  260.                     {
  261.                         $image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  262.                         $image->thumbnailImage( $width, $height, true );
  263.                         $canvas->addImage( $image );
  264.                         $canvas->setImagePage($width, $height, 0, 0);
  265.                     }
  266.                     $image->destroy();
  267.                     $this->image = $canvas;
  268.             }
  269.             
  270.         }
  271.        
  272.        
  273.         // 添加水印图片
  274.         public function add_watermark($path, $x = 0, $y = 0)
  275.         {
  276.         $watermark = new Imagick($path);
  277.         $draw = new ImagickDraw();
  278.         $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
  279.             if($this->type=='gif')
  280.             {
  281.                 $image = $this->image;
  282.             $canvas = new Imagick();
  283.                 $images = $image->coalesceImages();
  284.                 foreach($image as $frame)
  285.                 {
  286.                 $img = new Imagick();
  287.                     $img->readImageBlob($frame);
  288.                 $img->drawImage($draw);
  289.                
  290.                 $canvas->addImage( $img );
  291.                 $canvas->setImageDelay( $img->getImageDelay() );
  292.             }
  293.             $image->destroy();
  294.                 $this->image = $canvas;
  295.             }
  296.             else
  297.             {
  298.                 $this->image->drawImage($draw);
  299.             }
  300.         }
  301.        
  302.         // 添加水印文字
  303.         public function add_text($text, $x = 0 , $y = 0, $angle=0, $style=array())
  304.         {
  305.         $draw = new ImagickDraw();
  306.         if(isset($style['font'])) $draw->setFont($style['font']);
  307.         if(isset($style['font_size'])) $draw->setFontSize($style['font_size']);
  308.             if(isset($style['fill_color'])) $draw->setFillColor($style['fill_color']);
  309.             if(isset($style['under_color'])) $draw->setTextUnderColor($style['under_color']);
  310.             
  311.             if($this->type=='gif')
  312.             {
  313.                 foreach($this->image as $frame)
  314.                 {
  315.                     $frame->annotateImage($draw, $x, $y, $angle, $text);
  316.                 }
  317.             }
  318.             else
  319.             {
  320.                 $this->image->annotateImage($draw, $x, $y, $angle, $text);
  321.             }
  322.         }
  323.        
  324.        
  325.         // 保存到指定路径
  326.         public function save_to( $path )
  327.         {
  328.             if($this->type=='gif')
  329.             {
  330.                 $this->image->writeImages($path, true);
  331.             }
  332.             else
  333.             {
  334.                 $this->image->writeImage($path);
  335.             }
  336.         }
  337.         // 输出图像
  338.         public function output($header = true)
  339.         {
  340.             if($header) header('Content-type: '.$this->type);
  341.             echo $this->image->getImagesBlob();               
  342.         }
  343.        
  344.         public function get_width()
  345.         {
  346.         $size = $this->image->getImagePage();
  347.         return $size['width'];
  348.         }
  349.        
  350.         public function get_height()
  351.         {
  352.             $size = $this->image->getImagePage();
  353.         return $size['height'];
  354.         }
  355.         // 设置图像类型, 默认与源类型一致
  356.         public function set_type( $type='png' )
  357.         {
  358.             $this->type = $type;
  359.         $this->image->setImageFormat( $type );
  360.         }
  361.         // 获取源图像类型
  362.         public function get_type()
  363.         {
  364.                 return $this->type;
  365.         }
  366.         // 当前对象是否为图片
  367.         public function is_image()
  368.         {
  369.                 if( $this->image )
  370.                         return true;
  371.                 else
  372.                         return false;
  373.         }
  374.        
  375.         public function thumbnail($width = 100, $height = 100, $fit = true){ $this->image->thumbnailImage( $width, $height, $fit );} // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片
  376.         /*
  377.         添加一个边框
  378.         $width: 左右边框宽度
  379.         $height: 上下边框宽度
  380.         $color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'...
  381.         */
  382.         public function border($width, $height, $color='rgb(220, 220, 220)')
  383.         {
  384.                 $color=new ImagickPixel();
  385.                 $color->setColor($color);
  386.                 $this->image->borderImage($color, $width, $height);
  387.         }
  388.        
  389.         public function blur($radius, $sigma){$this->image->blurImage($radius, $sigma);} // 模糊
  390.         public function gaussian_blur($radius, $sigma){$this->image->gaussianBlurImage($radius, $sigma);} // 高斯模糊
  391.         public function motion_blur($radius, $sigma, $angle){$this->image->motionBlurImage($radius, $sigma, $angle);} // 运动模糊
  392.         public function radial_blur($radius){$this->image->radialBlurImage($radius);} // 径向模糊
  393.         public function add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);} // 添加噪点
  394.        
  395.         public function level($black_point, $gamma, $white_point){$this->image->levelImage($black_point, $gamma, $white_point);} // 调整色阶
  396.         public function modulate($brightness, $saturation, $hue){$this->image->modulateImage($brightness, $saturation, $hue);} // 调整亮度、饱和度、色调
  397.         public function charcoal($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描
  398.         public function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油画效果
  399.        
  400.         public function flop(){$this->image->flopImage();} // 水平翻转
  401.         public function flip(){$this->image->flipImage();} // 垂直翻转
  402. }
复制代码

作者:iua1024 发表于2012-1-19 20:28:37 原文链接


您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-3 04:49 , Processed in 0.016609 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表