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

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

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

更多参数看源代码

原图

效果图:

调用方式:

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

<?php 
/* 
@版本日期: 版本日期: 2012年1月18日 
@著作权所有: 1024 intelligence ( http://www.1024i.com ) 
获得使用本类库的许可, 您必须保留著作权声明信息. 
报告漏洞,意见或建议, 请联系 Lou Barnes(iua1024@gmail.com) 
*/ 

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: 颜色: 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 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF的更多相关文章

  1. Xcode不显示任务控制视图中所有窗口的标题(4个手指向下滑动)

    MacMissionControl是一个方便的功能,它允许我们导航一个应用程序的不同窗口.大多数应用程序会在每个窗口缩略图下方显示窗口标题,因此很容易找到您想要带到前面的窗口.但是,如果我在Xcode4.2.1中打开一些项目并进行4指滑动,我在窗口缩略图下面看不到任何“标题”或“项目名称”.这很烦人,因为有时窗口预览缩略图并不总是足以识别内部的内容.是否有任何需要更改才能显示的设置?或者它还不可能?

  2. 在iOS中处理大图片

    我发现有一个BitmapFactory类,它有一个inSampleSize选项,可以在Android平台中对照片进行缩减.在iOS上怎么做?

  3. 如何在iOS 7中的UITextField中添加缩略图UIImage?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  4. iOS – 如何在没有游戏的情况下从视频获取缩略图?

    我正在尝试从视频中获取缩略图并在我的tableview中显示它.这是我的代码:但是图像总是回归黑色.怎么了?

  5. ios – 如何将UIImage保存到文档目录?

    如何将缩略图与其来自的视频一起写入我的文档目录?到目前为止,这是我的代码:解决方法就这么简单:一个建议:如果可以按照苹果的指南再次下载图像,则将图像保存到库/缓存中.

  6. 在Swift中,使用SDWebImage做缓存后获取缩略图的大小

    0//不等于1,执行下一个循环ifcount![0],options:[],progress:nil,completed:{->Voidin//图片缓存完成,但是图片不一定有imageifimage!=nil{//将image转成二进制数据letdata=UIImagePNGRepresentationdataLenth+=data!.length}//dispath_group_leave要放到闭包的最后,才能够正确的测试出。dispatch_group_leave})}//调度组回到主队列dispat

  7. swift – generateCGImagesAsynchronouslyForTimes有时不会生成完整的缩略图

    我正在使用一个使用AVAssetImageGenerator.generateCGImagesAsynchronouslyForTimes的OSX应用程序,它通常工作正常.然而,一段时间后,我回来的缩略图只包含前几行像素,其余的是绿色,有时图像将是不同的绿色.很难跟踪,因为它不会一直发生,但是当大约一半的缩略图受到影响时.这是我期望看到的形象:但经常发生这种情况:以下是我用来生成缩略图的代码:这就

  8. android – MediaStore:获取图像数据,缩略图和文件夹

    我有两个清单.我们称它们为AlbumsList和PicturesList.>第一个显示相册封面,它的名称和图片数量.>第二个显示所选专辑中包含的所有图像.我已经使用File类完成了它,但它工作得太慢,当我只需要来自图库的那些时,它会在设备上找到所有图像.我读过有关MediaStore内容提供商但从未使用过它.所以我有两个问题:>如何使用MediaStore类找到“相册”,缩略图和图片中的图片数量?>如何使用MediaStore类获取特定文件夹中所有图像的文件路径和缩略图?

  9. android – 从图像中删除或更新exif缩略图

    无论如何从图像中删除exif缩略图?我在我的应用程序中裁剪图像并使用sanselan库复制所有exif数据无损.然后,我相应地更新宽度/高度/旋转.我找不到任何方法来更新exif缩略图或删除它,任何想法如何做到这一点?

  10. 如何从Android 4.0及更高版本的远程URL获取视频缩略图?

    实际上,我想显示视频网址中的视频缩略图并在ImageView中显示,然后点击ImageView,VideoView会在另一个活动中显示视频.所以最终我的问题是,我无法从URL获取视频缩略图.我已经尝试了很多,但无法获得任何适用于我的解决方案,有解决方案只能从SD卡视频获取缩略图和远程URL,我找到了一些解决方案,但它不起作用.Below是我试过的.有人能告诉我如何显示URL中的视频缩略图吗?

随机推荐

  1. PHP个人网站架设连环讲(一)

    先下一个OmnihttpdProffesinalV2.06,装上就有PHP4beta3可以用了。PHP4给我们带来一个简单的方法,就是使用SESSION(会话)级变量。但是如果不是PHP4又该怎么办?我们可以假设某人在15分钟以内对你的网页的请求都不属于一个新的人次,这样你可以做个计数的过程存在INC里,在每一个页面引用,访客第一次进入时将访问时间送到cookie里。以后每个页面被访问时都检查cookie上次访问时间值。

  2. PHP函数学习之PHP函数点评

    PHP函数使用说明,应用举例,精简点评,希望对您学习php有所帮助

  3. ecshop2.7.3 在php5.4下的各种错误问题处理

    将方法内的函数,分拆为2个部分。这个和gd库没有一点关系,是ecshop程序的问题。会出现这种问题,不外乎就是当前会员的session或者程序对cookie的处理存在漏洞。进过本地测试,includes\modules\integrates\ecshop.php这个整合自身会员的类中没有重写integrate.php中的check_cookie()方法导致,验证cookie时返回的username为空,丢失了登录状态,在ecshop.php中重写了此方法就可以了。把他加到ecshop.php的最后面去就可

  4. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  5. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  6. zen_cart实现支付前生成订单的方法

    这篇文章主要介绍了zen_cart实现支付前生成订单的方法,结合实例形式详细分析了zen_cart支付前生成订单的具体步骤与相关实现技巧,需要的朋友可以参考下

  7. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 基于win2003虚拟机中apache服务器的访问

    下面小编就为大家带来一篇基于win2003虚拟机中apache服务器的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. Yii2中组件的注册与创建方法

    这篇文章主要介绍了Yii2之组件的注册与创建的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

返回
顶部