PHP中使用Imagick实现各种图片效果实例
imagick是一个功能强大的图像处理库。
说是翻译 其实就是简要介绍imagick 的主要功能的或者说是我觉得比较实用的功能函数的介绍 以及使用的例子。
因为本人的英语水平有限,所以采用比较通俗或者说比较贴近应用化的语言来描述。
先欣赏一组炫丽的效果:
偏置图像:
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->rollimage(20,39);
echo $image;
?>
thumbnailimage($width,$height) 改变图片大小
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->thumbnailimage(100,0);
echo $image;
?>
addnoiseimage(int $noise_type [, int $channel= imagick::channel_all ]);
功能:
adds random noise to the image
添加干扰素
noise constants ( $noise_type 类型)
imagick::noise_uniform (integer)
imagick::noise_gaussian (integer)
imagick::noise_multiplicativegaussian (integer)
imagick::noise_impulse (integer)
imagick::noise_laplacian (integer)
imagick::noise_poisson (integer)
channel constants ( $channel 类型)
imagick::channel_undefined (integer)
imagick::channel_red (integer)
imagick::channel_gray (integer)
imagick::channel_cyan (integer)
imagick::channel_green (integer)
imagick::channel_magenta (integer)
imagick::channel_blue (integer)
imagick::channel_yellow (integer)
imagick::channel_alpha (integer)
imagick::channel_opacity (integer)
imagick::channel_matte (integer)
imagick::channel_black (integer)
imagick::channel_index (integer)
imagick::channel_all (integer)
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->thumbnailimage(100,0);
$image->addnoiseimage(imagick::noise_poisson,imagick::channel_opacity);
echo $image;
?>
annotateimage 创建文本图像
例子:
<?php
$image = new imagick();
$draw = new imagickdraw();
$pixel = new imagickpixel( 'gray' );
$image->newimage(800, 75, $pixel);
$pixel->setcolor('black');
$draw->setfont('bookman-demiitalic');
$draw->setfontsize( 30 );
$image->annotateimage($draw, 10, 45, 0, 'the quick brown fox jumps over the lazy dog');
$image->setimageformat('png');
header('content-type: image/png');
echo $image;
?>
blurimage(float $radius , float $sigma [, int $channel ])
adds blur filter to image 图像模糊度处理
参数:
int $channel :
imagick::channel_undefined (integer)
imagick::channel_red (integer)
imagick::channel_gray (integer)
imagick::channel_cyan (integer)
imagick::channel_green (integer)
imagick::channel_magenta (integer)
imagick::channel_blue (integer)
imagick::channel_yellow (integer)
imagick::channel_alpha (integer)
imagick::channel_opacity (integer)
imagick::channel_matte (integer)
imagick::channel_black (integer)
imagick::channel_index (integer)
imagick::channel_all (integer)
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->blurimage(5,3);
echo $image;
?>
borderimage ( mixed $bordercolor , int $width , int $height ) 图片边框处理
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->borderimage($color,5,4);
$image->blurimage(5,5,imagick::channel_green);
echo $image;
?>
charcoalimage ( float $radius , float $sigma ) 图像素描处理
参数说明:
$radius :越小越薄。
$sigma: 越大 墨越深 反之。
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->borderimage($color,5,4);
$image->charcoalimage(0.0001,0.001);
//$image->blurimage(5,5,imagick::channel_green);
echo $image;
?>
chopimage ( int $width , int $height , int $x , int $y )
参数说明:删除一定范围的图像区域
就不做参数说明,一看便知.
colorizeimage( mixed $colorize , mixed $opacity )混合填充颜色
$colorize 颜色
$opacit 透明度
例子:
<?php
/*
胶卷底片效果
*/
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->negateimage(false);
$image->colorizeimage('#000',1.0);
echo $image;
?>
embossimage ( float $radius , float $sigma )
功能: 返回一个灰度级3d图像 不太好。
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->embossimage(1,1);
echo $image;
?>
(两张效果图)
flipimage(void)
功能: 创建图像倒影(垂直翻转)
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->flipimage();
echo $image;
?>
flopimage ( void )
功能: 图像水平横向翻转
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->flopimage();
echo $image;
?>
frameimage(mixed $matte_color,int $width, int $height,int $inner_bevel, int $outer_bevel)
功能:创建3d图像边框
参数说明:
$matte_color:颜色
$inner_bevel:边框内部倾斜度
$outer_bevel:外部边框倾斜度
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->frameimage($color,11,11,1,10);
echo $image;
?>
注意事项:
$width(宽度)不能小于$inner_bevel(边框内部倾斜度)
imagick::gammaimage (float $gamma [,int $channel= imagick::channel_all])
功能:调整图像灰度系数
参数说明:
float $gamma :灰度系数值
$channel 默认为 imagick::channel_all
imagick::channel_all
例子 1:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->gammaimage(30);
echo $image;
?>
例子 2:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->gammaimage(30);
echo $image;
?>
gaussianblurimage ( float $radius , float $sigma [, int $channel= imagick::channel_all ] )
功能:高斯模糊处理 类似于photo的高斯模糊
参数说明:
float $radius:高斯模糊的半径,像素,不包括中心象素。
float $sigma :高斯的标准偏差,以像素为单位。我觉得这个参数最重要。
int $channel :图像颜色模式。
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->gaussianblurimage(30,3);
echo $image;
?>
levelimage ( float $blackpoint , float $gamma , float $whitepoint [, int $channel= imagick::channel_all ] )
功能: 调整图像的色阶(adjusts the levels of an image)
参数说明
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->levelimage(4,4,4);
echo $image;
?>
例子2:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->levelimage(200,200,200,imagick::channel_green);
echo $image;
?>
magnifyimage( void )
功能说明:简便的图像等比例放大2倍(is a convenience method that scales an image proportionally to twice its original size. )
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->magnifyimage ();
echo $image;
?>
medianfilterimage ( float $radius )
功能:特是的滤镜 有点像photoshop 调色刀滤镜
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->medianfilterimage(5);
echo $image;
?>
minifyimage(void)
功能:图小缩小一倍(scales an image proportionally to half its size)
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->minifyimage();
echo $image;
?>
modulateimage ( float $brightness , float $saturation , float $hue )
功能:控制调整图像的 亮度、饱和度、色调。
参数说明:
float $brightness: 亮度
float $saturation :饱和度
float $hue 色调
例子1:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->modulateimage(100,1,100);
echo $image;
?>
例子2:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$image->modulateimage(250,1,250);
echo $image;
?>
motionblurimagemotionblurimage ( float $radius , float $sigma , float $angle [, int $channel= imagick::channel_default ] )
功能:模拟运动模糊(simulates motion blur) ,类似photoshop的动感模糊滤镜功能
参数说明:
float $radius: 高斯 半径,不包过中心像素。
float $sigma:标准偏差的高斯,以像素为单位。【重要参数】
float $angle:模糊角度。
int $channel:图像颜色模式。默认为 imagick::channel_default
例子1:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->motionblurimage (61,10,10);
echo $image;
?>
例子2:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->motionblurimage (201,10,100);
echo $image;
?>
oilpaintimage ( float $radius ):
功能说明: 模拟油画滤镜(simulates an oil painting)
例子:
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->oilpaintimage(1);
echo $image;
?>
radialblurimage ( float $angle [, int $channel= imagick::channel_all ] )
功能: 径向模糊(radial blurs an image)
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->radialblurimage(30);
echo $image;
?>
raiseimage ( int $width , int $height , int $x , int $y , bool $raise )
功能说明:创建3d图像按钮(creates a simulated 3d button-like effect)
<?php
ini_set('display_errors',1);
header('content-type: image/jpeg');
$image = new imagick('1.jpg');
$color=new imagickpixel();
$color->setcolor("rgb(220,220,220)");
$image->raiseimage(10,10,3,5,6);
echo $image;
?>
附1:要求缩小图片尺寸、增加半透明边框,读入exif信息,按指定要求显示在图片上
$towidth = '500';
$toheight = '700'; //设置图片调整大小时允许的最大宽度和高度
$sourcefile = './b.jpg'; //定义一个图像文件路径
//$image->writeimage('./b.jpg.bak'); //可以备份这个图片
$myimage = new imagick( $sourcefile ); //读入该图像文件
$exifobject = my_exif( $myimage ); //自写函数,读取exif信息(拍摄数据),按自己的要求排列exif信息,返回对象
//$myimage->setimageformat('jpeg'); //把图片转为jpg格式
$myimage->setcompressionquality( 100 ); //设置jpg压缩质量,1 - 100
$myimage->enhanceimage(); //去噪点
$sourcewidth = $myimage->getimagewidth(); //获取读入图像原始大小
if ( $sourcewidth > $towidth )
{
$myimage->scaleimage( $towidth, $toheight, true ); //调整图片大小
}
$myimage->raiseimage( 8, 8, 0, 0, 1 ); //加半透明边框
$resizewidth = $myimage->getimagewidth(); //读出调整之后的图片大小
$resizeheight = $myimage->getimageheight();
$drawback = new imagickdraw(); //实例化一个绘画对象,绘制半透明黑色背景给exif信息用
$drawback->setfillcolor( new imagickpixel('#000000') ); //设置填充颜色为黑色
$drawback->setfillopacity( 0.6 ); //填充透明度为0.6,参数0.1-1,1为不透明
$drawback->rectangle( $resizewidth / 2 - 190, $resizeheight - 50, $resizewidth / 2 + 190, $resizeheight - 12 ); //绘制矩形参数,分别为左上角x、y,右下角x、y
$myimage->drawimage( $drawback ); //确认到image中绘制该矩形框
$draw = new imagickdraw(); //实例化一个绘画对象,绘制exif文本信息嵌入图片中
$draw->setfont( './xianhei.ttf' ); //设置文本字体,要求ttf或者ttc字体,可以绝对或者相对路径
$draw->setfontsize( 11 ); //设置字号
$draw->settextalignment( 2 ); //文字对齐方式,2为居中
$draw->setfillcolor( '#ffffff' ); //文字填充颜色
$myimage->annotateimage( $draw, $resizewidth / 2, $resizeheight - 39, 0, $exifobject->row1 ); //绘制第一行文本,居中
$myimage->annotateimage( $draw, $resizewidth / 2, $resizeheight - 27, 0, $exifobject->row2 ); //绘制第二行文本,居中
$myimage->annotateimage( $draw, $resizewidth / 2, $resizeheight - 15, 0, $exifobject->row3 ); //绘制第三行文本,居中
/* output the image with headers */
header( 'content-type: image/jpeg' ); //php文件输出mime类型为jpeg图片
echo $myimage; //在当前php页面输出图片
//$image->writeimage('./b.new.jpg'); //如果图片不需要在当前php程序中输出,使用写入图片到磁盘函数,上面的设置header也可以去除
$myimage->clear();
$myimage->destroy(); //释放资源
//自写函数,读取exif信息,返回对象
function my_exif( $myimage )
{
$exifarray = array( 'exif:model' => '未知', 'exif:datetimeoriginal' => '未知', 'exif:exposureprogram' => '未知', 'exif:fnumber' => '0/10', 'exif:exposuretime' => '0/10', 'exif:isospeedratings' => '未知',
'exif:meteringmode' => '未知', 'exif:flash' => '关闭闪光灯', 'exif:focallength' => '未知', 'exif:exifimagewidth' => '未知', 'exif:exifimagelength' => '未知' ); //初始化部分信息,防止无法读取照片exif信息时运算发生错误
$exifarray = $myimage->getimageproperties( "exif:*" ); //读取图片的exif信息,存入$exifarray数组
//如果需要显示原数组可以使用print_r($exifarray);
$r->row1 = '相机:' . $exifarray['exif:model'];
$r->row1 = $r->row1 . ' 拍摄时间:' . $exifarray['exif:datetimeoriginal'];
switch ( $exifarray['exif:exposureprogram'] )
{
case 1:
$exifarray['exif:exposureprogram'] = "手动(m)";
break; //manual control
case 2:
$exifarray['exif:exposureprogram'] = "程序自动(p)";
break; //program normal
case 3:
$exifarray['exif:exposureprogram'] = "光圈优先(a,av)";
break; //aperture priority
case 4:
$exifarray['exif:exposureprogram'] = "快门优先(s,tv)";
break; //shutter priority
case 5:
$exifarray['exif:exposureprogram'] = "慢速快门";
break; //program creative (slow program)
case 6:
$exifarray['exif:exposureprogram'] = "运动模式";
break; //program action(high-speed program)
case 7:
$exifarray['exif:exposureprogram'] = "人像";
break; //portrait
case 8:
$exifarray['exif:exposureprogram'] = "风景";
break; //landscape
default:
$exifarray['exif:exposureprogram'] = "其它";
}
$r->row1 = $r->row1 . ' 模式:' . $exifarray['exif:exposureprogram'];
$exifarray['exif:fnumber'] = explode( '/', $exifarray['exif:fnumber'] );
$exifarray['exif:fnumber'] = $exifarray['exif:fnumber'][0] / $exifarray['exif:fnumber'][1];
$r->row2 = '光圈:f/' . $exifarray['exif:fnumber'];
$exifarray['exif:exposuretime'] = explode( '/', $exifarray['exif:exposuretime'] );
if ( ($exifarray['exif:exposuretime'][0] / $exifarray['exif:exposuretime'][1]) >= 1 )
{
$exifarray['exif:exposuretime'] = sprintf( "%.1fs", (float)$exifarray['exif:exposuretime'][0] / $exifarray['exif:exposuretime'][1] );
} else
{
$exifarray['exif:exposuretime'] = sprintf( "1/%ds", $exifarray['exif:exposuretime'][1] / $exifarray['exif:exposuretime'][0] );
}
$r->row2 = $r->row2 . ' 快门:' . $exifarray['exif:exposuretime'];
$r->row2 = $r->row2 . ' iso:' . $exifarray['exif:isospeedratings'];
$exifarray['exif:exposurebiasvalue'] = explode( "/", $exifarray['exif:exposurebiasvalue'] );
$exifarray['exif:exposurebiasvalue'] = sprintf( "%1.1fev", ((float)$exifarray['exif:exposurebiasvalue'][0] / $exifarray['exif:exposurebiasvalue'][1] * 100) / 100 );
if ( (float)$exifarray['exif:exposurebiasvalue'] > 0 )
{
$exifarray['exif:exposurebiasvalue'] = "+" . $exifarray['exif:exposurebiasvalue'];
}
$r->row2 = $r->row2 . ' 补偿:' . $exifarray['exif:exposurebiasvalue'];
switch ( $exifarray['exif:meteringmode'] )
{
case 0:
$exifarray['exif:meteringmode'] = "未知";
break;
case 1:
$exifarray['exif:meteringmode'] = "矩阵";
break;
case 2:
$exifarray['exif:meteringmode'] = "*重点平均";
break;
case 3:
$exifarray['exif:meteringmode'] = "点测光";
break;
case 4:
$exifarray['exif:meteringmode'] = "多点测光";
break;
default:
$exifarray['exif:meteringmode'] = "其它";
}
$r->row2 = $r->row2 . ' 测光:' . $exifarray['exif:meteringmode'];
switch ( $exifarray['exif:flash'] )
{
case 1:
$exifarray['exif:flash'] = "开启闪光灯";
break;
}
$r->row2 = $r->row2 . ' ' . $exifarray['exif:flash'];
if ( $exifarray['exif:focallengthin35mmfilm'] )
{
$r->row3 = '等效焦距:' . $exifarray['exif:focallengthin35mmfilm'] . "mm";
} else
{
$exifarray['exif:focallength'] = explode( "/", $exifarray['exif:focallength'] );
$exifarray['exif:focallength'] = sprintf( "%4.1fmm", (double)$exifarray['exif:focallength'][0] / $exifarray['exif:focallength'][1] );
$r->row3 = '焦距:' . $exifarray['exif:focallength'];
}
$r->row3 = $r->row3 . ' 原始像素:' . $exifarray['exif:exifimagewidth'] . 'x' . $exifarray['exif:exifimagelength'] . 'px';
if ( $exifarray['exif:software'] )
{
$r->row3 = $r->row3 . ' 后期:' . $exifarray['exif:software'];
}
return $r;
}
附2:处理图片水印
<?php
//获取水印图片
$logo = new imagick("logo.png");
$logo->setimageresolution(0.01,0.03);
//创建一个imagick对象,同时获取要处理的源图
$im = new imagick( "old_large_img_2.jpg" );
//获取源图片宽和高
$srcwh = $im->getimagegeometry();
//图片等比例缩放宽和高设置
if($srcwh['width']>710){
$srcw['width'] = 710;
$srch['height'] = $srcw['width']/$srcwh['width']*$srcwh['height'];
}else{
$srcw['width'] = $srcwh['width'];
$srch['height'] = $srcwh['height'];
}
//按照比例进行缩放
$im->thumbnailimage( $srcw['width'], $srch['height'], true );
// 按照缩略图大小创建一个有颜色的图片
$canvas = new imagick();
$canvas->newimage( $srcw['width'], $srch['height'], 'black', 'jpg' ); //pink,black
//添加水印
$im->compositeimage($logo,imagick::composite_over,$srcw['width']-280,$srch['height']-77);
$canvas->setcompressionquality(91);
//合并图片
$canvas->compositeimage( $im, imagick::composite_over, 0, 0);
//输出图片
header( "content-type: image/jpg" );
echo $canvas;
//生成图片
$canvas->writeimage( "test_img/old_large_img_2_96.jpg" );
?>