目录
PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF,imagick水印
您可能感兴趣的文章:
首页 php教程 php手册 PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF,imagick水印

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

Jun 13, 2016 am 08:45 AM
gif imagick phpcms 水印 缩略图 裁剪

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

给骨头系统开发的图像库的 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

<&#63;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&#63;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读取PDF生成png缩略图的两种方法
  • php使用imagick模块实现图片缩放、裁剪、压缩示例
  • PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
  • PHP中使用Imagick读取pdf并生成png缩略图实例
  • PHP中使用imagick生成PSD文件缩略图教程
  • PHP利用imagick生成组合缩略图
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Win11怎么禁用任务栏缩略图预览? 关闭鼠标移动任务栏图标显示缩略图技巧 Win11怎么禁用任务栏缩略图预览? 关闭鼠标移动任务栏图标显示缩略图技巧 Feb 29, 2024 pm 03:20 PM

本文将介绍如何在Win11系统中关闭鼠标移动任务栏图标时显示的缩略图功能。这一功能在默认情况下是开启的,当用户将鼠标指针悬停在任务栏上的应用程序图标上时,会显示该应用程序的当前窗口缩略图。然而,有些用户可能觉得这个功能不太实用或者会干扰到他们的使用体验,因此想要关闭它。任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过

美图秀秀怎么加水印呢?分享美图秀秀添加水印方法! 美图秀秀怎么加水印呢?分享美图秀秀添加水印方法! Mar 16, 2024 pm 09:55 PM

想知道美图秀秀怎么加水印吗?美图秀秀是一款非常好用的修图p图软件,提供抠图放在另一个图、改图片大小kb、去水印、换证件照底色、全屏加时间日期地点水印等功能,可以帮助用户快速的完成图片的制作。有用户制作了自己的图片不想被别人盗图想要铺满自己的水印,但不知道如何操作?小编现在为大家分享美图秀秀添加水印方法!喜欢就快来下载吧!一、美图秀秀怎么加水印呢?分享美图秀秀添加水印方法!1.打开在本站下载的美图秀秀2023版。美图秀秀2023版类别:拍摄美化下载美图秀秀2023版是一款功能丰富的图片美化和编辑软

剪映怎么去水印 怎么去除剪映水印 剪映怎么去水印 怎么去除剪映水印 Feb 22, 2024 pm 05:16 PM

打开剪映并选择模板,编辑好视频以后点击导出,点击无水印保存并分享。教程适用型号:iPhone13系统:iOS15.3.1版本:剪映6.8.0解析1打开剪映并选择一个剪辑模板。2点击打开模板后,点击右下角的剪同款选项。3从相册中选择照片填充片段并点击下一步。4如何点击页面右上角的导出选项。5最后点击无水印保存并分享就可以了。补充:剪映是什么软件1剪映是一款视频编辑软件,拥有全面的剪辑功能,可以变速、有滤镜和美颜效果,还有着丰富的曲库资源。自2021年2月起,剪映支持在手机移动端,Pad端,Mac电

Wink如何去视频水印 Wink如何去视频水印 Feb 23, 2024 pm 07:22 PM

Wink如何去视频水印?winkAPP中是有去除掉视频水印的工具,但是多数的小伙伴不知道wink中如何去除掉视频中的水印,接下来就是小编为玩家带来的Wink视频去水印方法图文教程,感兴趣的用户快来一起看看吧!Wink如何去视频水印1、首先打开winkAPP,在首页面专区中选择【去水印】功能;2、然后在相册中选择你需要去除水印的视频;3、接着选择视频之后,剪辑视频之后点击右上角【√】;4、最后点击如下图所示的【一键去印】之后点击【处理】即可。

扫描全能王去除水印怎么去除 扫描全能王去除水印怎么去除 Mar 05, 2024 pm 05:34 PM

去除水印是软件扫描全能王中的一个好用的工具,有些用户还不太清楚扫描全能王去除水印怎么去除,可以在保存界面点击编辑PDF中的去除水印即可闭,接下来就是小编为用户带来的去除水印方法的介绍,感兴趣的用户快来一起看看吧!扫描全能王使用教程扫描全能王去除水印怎么去除答:可以在保存界面点击编辑PDF中的去除水印详情介绍:1、进入软件,点击【相机】图标。2、拍摄扫描需要去水印的文件。3、点击【→】进入下一步。4、完成编辑后,点击【✓】。5、点击其中的【编辑PDF】。6、选择下方的【去除水印】即可。

小米14怎么设置拍照水印? 小米14怎么设置拍照水印? Mar 18, 2024 am 11:00 AM

为了让拍摄出的照片更具个性和独特性,小米14提供了拍照水印设置。通过设置拍照水印,用户可以在拍摄的照片上添加图案、文字和标志,使得每一张照片都能更好地记录下珍贵的时刻和回忆。接下来,我们将介绍如何在小米14中设置拍照水印,让您的照片更加个性化和生动。小米14怎么设置拍照水印?1、首先点击“相机”。2、然后点击“设置”。3、接着找到水印,随后就可以开始拍摄了。

我如何裁剪HTML中的IFrame? 我如何裁剪HTML中的IFrame? Aug 29, 2023 pm 04:33 PM

内联框架在HTML中称为iframe。标签指定内容中的一个矩形区域,浏览器可以在其中显示带有滚动条和边框的不同文档。要在当前HTML文档中嵌入另一个文档,请使用内联框架。可以使用HTMLiframe名称属性指定元素的引用。在JavaScript中,对元素的引用也是使用name属性进行的。iframe本质上用于在当前显示的网页中显示网页。包含iframe的文档的URL使用“src”属性指定。语法以下是HTML的语法<iframesrc="URL"title="d

小米14Ultra如何去除水印? 小米14Ultra如何去除水印? Feb 28, 2024 pm 02:28 PM

小米14Ultra是一款性能配置非常不错的全新机型,这款手机还采用了与众不同的外观设计,颜值更高辨识度也很不错,很多入手的消费者们想要了解一下小米14Ultra怎么去除水印?,下面本站小编就为大家介绍一下吧!小米14Ultra怎么去除水印?1.打开相机应用程序:找到并打开小米14上预装的相机应用程序。2.进入设置菜单:点击屏幕右下角或左下角(取决于您使用哪个版本)显示其他选项按钮。然后,在弹出菜单中选择“设置”。3.关闭水印选项:在设置菜单中,您将看到各种选项和参数。向下滑动直到找到“水印”选项

See all articles