PHP Imagick扩展安装与图片处理实战指南
本实例详细解析了利用php_imagick扩展进行图片剪切、旋转、锐化、减色及添加各类特效的技术方案,旨在为开发者提供实用的参考示例。
这是一个允许PHP程序直接调用ImageMagick强大功能的扩展组件,通过它PHP能够获得与原生ImageMagick工具集相当的处理能力。
ImageMagick是一套功能强大、稳定且免费的开源工具集和开发包,支持读取、写入及处理包括TIFF、JPEG、GIF、PNG、PDF及PhotoCD等在内的185多种常见图像格式。借助该工具,开发者可根据Web应用需求动态生成图像,或对单张/批量图片执行尺寸调整、旋转、锐化、减色及特效增强等操作,并将结果保存为原格式或其他指定格式。
作为PHP生态中的图片处理利器,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环境为线程安全版本(Thread Safe),请下载以下对应文件:
下载地址: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服务器以生效。
二、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 HTTP服务器
执行命令:service httpd restart
三、实际应用场景案例
1. 边框处理示例 复制代码 代码如下:header('Content-type: image/jpeg'); $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. 创建缩略图并直接输出显示 复制代码 代码如下:
示例演示:
包含裁切、生成缩略图、添加水印功能,并具备自动检测及处理GIF动画的能力
调用方式示例: 复制代码 代码如下: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
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 内缩放,输出尺寸可能不完全等于设定值
'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程序设计工作有所帮助。