php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法_php技巧
本文实例讲述了php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法。分享给大家供大家参考。具体分析如下:
一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。
ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。
php_imagick是PHP对图片处理的一个扩展包,可以完成对图片改变大小、旋转、锐化、减色或增加特效等操作。
一、windows下安装Imagick扩展:
1、下载 ImageMagick并安装
http://image_magick.veidrodis.com/image_magick/binaries/ImageMagick-6.6.2-10-Q16-windows-dll.exe
2、下载php_imagick.dll
http://valokuva.org/outside-blog-content/imagick-windows-builds/php53/imagick-2.3.0-dev/vc9_nts/php_imagick.dll
如果你用的是线程安全的php,请下载
http://valokuva.org/outside-blog-content/imagick- windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll
3、设置
在php.ini中添加
extension=php_imagick.dll ,重启web server
二、linux下安装Imagick扩展:
1.yum安装ImageMagick
yum install ImageMagick ImageMagick-devel
2.测试是否安装成功
convert -version
3.安装imagick扩展
01.wget http://pecl.php.net/get/imagick-3.1.0RC2.tgz02.tar xzvf imagick-3.1.0RC2.tgz03.cd imagick-3.1.0RC204.phpize05../configure06.make07.make install
4.编辑php.ini文件,在文件末尾添加如下代码
extension=imagick.so
5. 重新启动apache服务器
service httpd restart
三、案例
1. 边框处理
$image = new Imagick('test.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->borderImage($color,5,4);
$image->blurImage(5,5,imagick::CHANNEL_GREEN);
echo $image;
我们先来看个简单的实例
php_imagick程序示例
1.创建一个缩略图并显示出来
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
2.创建一个目录下的缩略图,并保存
foreach($images as $image) {
// Providing 0 forces thumbnailImage to maintain aspect ratio
$image->thumbnailImage(1024,0);
}
$images->writeImages();
?>
3.缩略GIF动画图片
$im = new Imagick("example.gif");
/* Resize all frames */
foreach ($im as $frame) {
/* 50x50 frames */
$frame->thumbnailImage(50, 50);
/* Set the virtual canvas to correct size */
$frame->setImagePage(50, 50, 0, 0);
}/* Notice writeImages instead of writeImage */
$im->writeImages("example_small.gif", true);
?>
现在我们进入正题吧,
示例:
裁切/生成缩略图/添加水印, 自动检测和处理 GIF
调用方式:
$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
类 lib_image_imagick
{
私有 $image = null;
私有 $type = null;
// 构造函数
公共函数 __construct(){}
// 解析构造函数
公共函数 __destruct()
{
if($this->image!==null) $this->image->destroy();
}
// 加载图片
公共函数 open($path)
{
$this->image = new Imagick( $path );
if($this->图片)
{
$this->type = strtolower($this->image->getImageFormat());
}
返回$this->图像;
}
公共函数作物($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
if($this->type=='gif')
{
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach($images as $frame){
$img = 新的 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;
}
否则
{
$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 = 图像方位值时,输出指定位置部分图像
字母与图像的对应关系如下:
西北 北 东北
西 中心 东
西南 南 东南
*/
公共函数 resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255,255,255,0) )
{
开关($fit)
{
案例“强制”:
if($this->type=='gif')
{
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach($images as $frame){
$img = 新的 Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage( $width, $height, false );
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
}
$image->destroy();
$this->image = $canvas;
}
否则
{
$this->image->thumbnailImage( $width, $height, false );
}
休息;
案例“规模”:
if($this->type=='gif')
{
$image = $this->image;
$images = $image->coalesceImages();
$canvas = new Imagick();
foreach($images as $frame){
$img = 新的 Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage( $width, $height, true );
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
}
$image->destroy();
$this->image = $canvas;
}
否则
{
$this->image->thumbnailImage( $width, $height, true );
}
休息;
案例“scale_fill”:
$size = $this->image->getImagePage();
$src_width = $size['宽度'];
$src_height = $size['高度'];
$x = 0;
$y = 0;
$dst_width = $宽度;
$dst_高度=$高度;
if($src_width*$height > $src_height*$width)
{
$dst_height = intval($width*$src_height/$src_width);
$y = intval( ($height-$dst_height)/2 );
}
否则
{
$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 = 新的 Imagick();
$img->newImage($width, $height, $color, 'gif');
$img->drawImage($draw);
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
$canvas->setImagePage($width, $height, 0, 0);
}
}
否则
{
$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;
休息;
默认值:
$size = $this->image->getImagePage();
$src_width = $size['宽度'];
$src_height = $size['高度'];
$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);
}
否则
{
$crop_h = intval($src_width*$height/$width);
}
开关($fit)
{
case 'north_west':
$crop_x = 0;
$crop_y = 0;
休息;
案例“北”:
$crop_x = intval( ($src_width-$crop_w)/2 );
$crop_y = 0;
休息;
案例“north_east”:
$crop_x = $src_width-$crop_w;
$crop_y = 0;
休息;
案例“西”:
$crop_x = 0;
$crop_y = intval( ($src_height-$crop_h)/2 );
休息;
案例“中心”:
$crop_x = intval( ($src_width-$crop_w)/2 );
$crop_y = intval( ($src_height-$crop_h)/2 );
休息;
案例“东”:
$crop_x = $src_width-$crop_w;
$crop_y = intval( ($src_height-$crop_h)/2 );
休息;
case 'south_west':
$crop_x = 0;
$crop_y = $src_height-$crop_h;
休息;
案例“南”:
$crop_x = intval( ($src_width-$crop_w)/2 );
$crop_y = $src_height-$crop_h;
休息;
case 'south_east':
$crop_x = $src_width-$crop_w;
$crop_y = $src_height-$crop_h;
休息;
默认值:
$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 = 新的 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);
}
}
否则
{
$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;
}
}
// 添加水印图片
公共函数 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 = 新的 Imagick();
$img->readImageBlob($frame);
$img->drawImage($draw);
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
}
$image->destroy();
$this->image = $canvas;
}
否则
{
$this->image->drawImage($draw);
}
}
// 添加水印文字
公共函数 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);
}
}
否则
{
$this->image->annotateImage($draw, $x, $y, $angle, $text);
}
}
// 保存到指定路径
公共函数 save_to( $path )
{
if($this->type=='gif')
{
$this->image->writeImages($path, true);
}
否则
{
$this->image->writeImage($path);
}
}
// 输出图像
公共函数输出($header = true)
{
if($header) header('内容类型:'.$this->type);
echo $this->image->getImagesBlob();
}
公共函数 get_width()
{
$size = $this->image->getImagePage();
返回 $size['宽度'];
}
公共函数 get_height()
{
$size = $this->image->getImagePage();
返回 $size['高度'];
}
//图像设置类型,默认与源类型一致
公共函数 set_type( $type='png' )
{
$this->type = $type;
$this->image->setImageFormat( $type );
}
// 获取来源图像类型
公共函数 get_type()
{
返回$this->类型;
}
// 当前对象是否为图片
公共函数 is_image()
{
if( $this->image )
返回真;
否则
返回假;
}
public functionthumbnail($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程序设计有所帮助。

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

番茄小说是一款非常热门的小说阅读软件,我们在番茄小说中经常会有新的小说和漫画可以去阅读,每一本小说和漫画都很有意思,很多小伙伴也想着要去写小说来赚取赚取零花钱,在把自己想要写的小说内容编辑成文字,那么我们要怎么样在这里面去写小说呢?小伙伴们都不知道,那就让我们一起到本站本站中花点时间来看写小说的方法介绍吧。分享番茄小说写小说方法教程 1、首先在手机上打开番茄免费小说app,点击个人中心——作家中心 2、跳转到番茄作家助手页面——点击创建新书在小说的结

随着社交媒体的不断发展,小红书已经成为越来越多年轻人分享生活、发现美好事物的平台。许多用户在发布图片时遇到了自动保存的问题,这让他们感到十分困扰。那么,如何解决这个问题呢?一、小红书发布自动保存图片怎么解决?1.清除缓存首先,我们可以尝试清除小红书的缓存数据。步骤如下:(1)打开小红书,点击右下角的“我的”按钮;(2)在个人中心页面,找到“设置”并点击;(3)向下滚动,找到“清除缓存”选项,点击确认。清除缓存后,重新进入小红书,尝试发布图片看是否解决了自动保存的问题。2.更新小红书版本确保你的小

而后悔莫及、人们常常会因为一些原因不小心将某些联系人删除、微信作为一款广泛使用的社交软件。帮助用户解决这一问题,本文将介绍如何通过简单的方法找回被删除的联系人。1.了解微信联系人删除机制这为我们找回被删除的联系人提供了可能性、微信中的联系人删除机制是将其从通讯录中移除,但并未完全删除。2.使用微信内置“通讯录恢复”功能微信提供了“通讯录恢复”节省时间和精力,用户可以通过该功能快速找回之前被删除的联系人,功能。3.进入微信设置页面点击右下角,打开微信应用“我”再点击右上角设置图标、进入设置页面,,

随着抖音短视频的火爆,用户们在评论区互动变得更加丰富多彩。有些用户希望在评论中分享图片,以更好地表达自己的观点或情感。那么,抖音评论里怎么发图片呢?本文将为你详细解答这个问题,并为你提供一些相关的技巧和注意事项。一、抖音评论里怎么发图片?1.打开抖音:首先,你需要打开抖音APP,并登录你的账号。2.找到评论区:在浏览或发布短视频时,找到想要评论的地方,点击“评论”按钮。3.输入评论内容:在评论区输入你的评论内容。4.选择发送图片:在输入评论内容的界面,你会看到一个“图片”按钮或者“+”号按钮,点

在PowerPoint中,让图片逐一显示是一种常用的技巧,可以通过设置动画效果来实现。本指南详细介绍了实现这一技巧的步骤,包括基本设置、图片插入、添加动画、调整动画顺序和时间。此外,还提供了高级设置和调整,例如使用触发器、调整动画速度和顺序,以及预览动画效果。通过遵循这些步骤和技巧,用户可以轻松地在PowerPoint中设置图片逐一出现,从而提升演示文稿的视觉效果并吸引观众的注意力。

字体大小的设置成为了一项重要的个性化需求,随着手机成为人们日常生活的重要工具。以满足不同用户的需求、本文将介绍如何通过简单的操作,提升手机使用体验,调整手机字体大小。为什么需要调整手机字体大小-调整字体大小可以使文字更清晰易读-适合不同年龄段用户的阅读需求-方便视力不佳的用户使用手机系统自带字体大小设置功能-如何进入系统设置界面-在设置界面中找到并进入"显示"选项-找到"字体大小"选项并进行调整第三方应用调整字体大小-下载并安装支持字体大小调整的应用程序-打开应用程序并进入相关设置界面-根据个人

有网友发现打开浏览器网页,网页上的图片迟迟加载不出来,是怎么回事?检查过网络是正常的,那是哪里出现了问题呢?下面小编就给大家介绍一下网页图片加载不出来的六种解决方法。 网页图片加载不出来: 1、网速问题 网页显示不出图片有可能是因为电脑的网速比较慢,电脑中开启的软件比较多, 而我们访问的图片比较大,这就可能因为加载超时,导致图片显示不出来, 可以将比较占网速的软件将关掉,可以去任务管理器查看一下。 2、访问人数过多 网页显示不出图片还有可能是因为我们访问的网页,在同时间段访问的

手机游戏成为了人们生活中不可或缺的一部分,随着科技的发展。它以其可爱的龙蛋形象和有趣的孵化过程吸引了众多玩家的关注,而其中一款备受瞩目的游戏就是手机版龙蛋。帮助玩家们在游戏中更好地培养和成长自己的小龙,本文将向大家介绍手机版龙蛋的孵化方法。1.选择合适的龙蛋种类玩家需要仔细选择自己喜欢并且适合自己的龙蛋种类,根据游戏中提供的不同种类的龙蛋属性和能力。2.提升孵化机的等级玩家需要通过完成任务和收集道具来提升孵化机的等级,孵化机的等级决定了孵化速度和孵化成功率。3.收集孵化所需的资源玩家需要在游戏中
