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

给骨头系统开发的图像库的 imagick 部分 ,支持 gif , 完美支持裁切、生成缩略图、添加水印 。

支持按方位生成缩略图像, 如:

// 把左上角优先
$image->resize_to(100, 100, 'north_west');

// 右边优先
$image->resize_to(100, 100, 'east');

...

更多参数看源代码

原图


效果图:

 


调用方式:

[php]  view plain copy
  1. include 'imagick.class.php';  
  2.   
  3. $image = new lib_image_imagick();  
  4.   
  5. $image->open('a.gif');  
  6. $image->resize_to(100, 100, 'scale_fill');  
  7. $image->add_text('1024i.com', 10, 20);  
  8. $image->add_watermark('1024i.gif', 10, 50);  
  9. $image->save_to('x.gif');  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值