【php】图像处理函数

	php提供了丰富的图像处理函数,主要包括如下几种:

	①获取图像信息的函数
	②创建与销毁图像的函数
	③载入图像的函数
	④输出图像的函数
	⑤分配/取消图像颜色的函数
	⑥拷贝图像的函数
	⑦合并图像的函数
	⑧绘制线段与圆弧的函数
	⑨图像填充函数

	在使用php的图像处理函数之前,需要开启php.ini中的gd2库扩展
	
	extension=php_gd2.dll	

使用gd_info()函数可以查看当前安装的gd库信息:
	<?php
	var_dump(gd_info());
	?>

结果:
array(12) {
	["GD Version"]=>
	string(27) "bundled (2.0.34 compatible)"
	["FreeType Support"]=>
	bool(true)
	["FreeType Linkage"]=>
	string(13) "with freetype"
	["T1Lib Support"]=>
	bool(true)
	["GIF Read Support"]=>
	bool(true)
	["GIF Create Support"]=>
	bool(true)
	["JPG Support"]=>
	bool(true)
	["PNG Support"]=>
	bool(true)
	["WBMP Support"]=>
	bool(true)
	["XPM Support"]=>
	bool(false)
	["XBM Support"]=>
	bool(true)
	["JIS-mapped Japanese Font Support"]=>
	bool(false)
}


	常用函数:

(1)getimagesize():此函数主要用于获取图像的大小及相关信息,成功则返回一个数组,失败则返回false
	语法:
		array getimagesize(string filename);
案例:
	<?php
	$array = getimagesize("images/flower_1.jpg");
	print_r($array);
	?>

结果:
浏览器中的结果:
Array
(
    [0] => 350
    [1] => 318
    [2] => 2
    [3] => width="350" height="318"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

返回结果说明
	①索引 0 给出的是图像宽度的像素值
	②索引 1 给出的是图像高度的像素值
	③索引 2 给出的是图像的类型,返回的是数字,其中1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
	④索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签
	⑤索引 bits 给出的是图像的每种颜色的位数,二进制格式
	⑥索引 channels 给出的是图像的通道值,RGB 图像默认是 3
	⑦索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,
		如: header("Content-type: image/jpeg"); 

(2)imagesx(),imagesy():这两个函数分别用来获取图像的宽度和高度,单位为像素,返回值为整型
	语法:
		int imagesx(resource image)
		int imagesy(resource image)
	注意:
	参数为如 imagecreatetruecolor()、imagecreatefromjpeg() 等函数返回的图像资源
案例:
	<?php
	$img = imagecreatefromjpeg("images/flower_1.jpg");
	echo "图像宽度:",imagesx( $img ),"<br />";
	echo "图像高度:",imagesy( $img );
	?>

浏览器输出: 

图像宽度:350
图像高度:318


(3)如果我们要对图像进行处理,就如其它图像处理软件一样,需要创建一块画布。
	imagecreate()与imagecreatetruecolor()函数用于创建一幅空白图像
	语法:
		resource   imagecreate(int x,int y)
	注意:参数x、y分别代表要创建图像的宽度和高度,返回一个图像资源
案例:
<?
header("Content-type: image/png");
//创建图像
$im = @imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色
$bg = imagecolorallocate($im, 255, 255, 255);
//文字颜色
$text_color = imagecolorallocate($im, 0, 0, 255);
//水平画一行字,要输出中文等需要 TTF 字体支持的请使用 magettftext() 函数
imagestring($im, 5, 0, 0, "Hello world!", $text_color);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>

(4)图像处理完成后,使用imagedestroy()函数销毁图像资源以释放内存
	语法:
		bool   imagedestroy(resource image)

(5)此系列函数用于从文件或url载入一张图像,成功返回一个图像资源,失败返回一个空字符串
该系列函数有:
	①imagecreatefromgif():创建一块画布,并从 GIF 文件或 URL 地址载入一副图像
	②imagecreatefromjpeg():创建一块画布,并从 JPEG 文件或 URL 地址载入一副图像
	③imagecreatefrompng():创建一块画布,并从 PNG 文件或 URL 地址载入一副图像
	④imagecreatefromwbmp()创建一块画布并从 WBMP 文件或 URL 地址载入一副图像
	⑤imagecreatefromstring():创建一块画布,并从字符串中的图像流新建一副图像
语法:
	①resource imagecreatefromgif( string filename )
	②resource imagecreatefromjpeg( string filename )
	③resource imagecreatefrompng( string filename )
	④resource imagecreatefromwbmp( string filename )
	⑤resource imagecreatefromstring( string image )

(6)载入图像案例:
<?
header("Content-type: image/jpeg");
//创建并载入一幅图像
$im = @imagecreatefromjpeg("images/flower_1.jpg");
//错误处理
if(!$im){
    $im  = imagecreatetruecolor(150, 30);
    $bg = imagecolorallocate($im, 255, 255, 255);
    $text_color  = imagecolorallocate($im, 0, 0, 255);
    //填充背景色
    imagefilledrectangle($im, 0, 0, 150, 30, $bg);
    //以图像方式输出错误信息
    imagestring($im, 3, 5, 5, "Error loading image", $text_color);
} else {
    //输出该图像
    imagejpeg($im);
}
?>

此系列函数主要用于以不同格式将图像输出到浏览器或文件
php允许将图像以不同格式输出,该系列函数有:
	①imagegif():以 GIF 格式将图像输出到浏览器或文件
	②imagejpeg():以 JPEG 格式将图像输出到浏览器或文件
	③imagepng():以 PNG 格式将图像输出到浏览器或文件
	④imagewbmp():以 WBMP 格式将图像输出到浏览器或文件
语法:
	①bool imagegif ( resource image [, string filename] )
	②bool imagejpeg ( resource image [, string filename [, int quality]] )
	③bool imagepng ( resource image [, string filename] )
	④bool imagewbmp ( resource image [, string filename [, int foreground]] )


绘制一个圆弧并保存到image目录下:
<?php
header("Content-type: image/png");
$im = @imagecreate(200, 200)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
imagearc($im, 100, 100, 150, 150, 0, 360, $red);
imagepng($im,"images/circle.png");
imagedestroy($im);
?>
在 images 目录下就会生成一个 circle.png 文件。

(7)imagecolorallocate() 函数用于为图像分配颜色,返回一个标识符,代表了由给定的 RGB 成分组成的颜色,如果分配失败则返回 -1
	语法:
		int imagecolorallocate( resource image, int red, int green, int blue )
	注意:
		参数 red,green 和 blue 分别是所需要的颜色的 红,绿,蓝 成分,取值范围 0 - 255

imagecolorallocatealpha()和 imagecolorallocate() 用法相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明
	语法:
		int imagecolorallocatealpha( resource image, int red, int green, int blue, int alpha )




分配图像颜色案例:
	 <?php
header("Content-type: image/png");
//创建图像
$im = @imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色并填充
$bg = imagecolorallocate($im, 204, 204, 204);
//设定文字颜色
$red = imagecolorallocate($im, 255, 0, 0);
//水平画一行字
imagestring($im, 5, 0, 0, "Hello world!", $red);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>

(8)imagecolordeallocate() 函数用于取消先前由 imagecolorallocate() 和imagecolorallocatealpha() 函数为图像分配的颜色。 
	语法:
		bool imagecolordeallocate( resource image, int color )

案例:
	
	<?
	$im = @imagecreate(200, 50) or die("创建图像资源失败");
	$bg = imagecolorallocate($im, 255, 0, 0);
	imagecolordeallocate($im, $bg);
	?>

imagecopy() 函数用于拷贝图像或图像的一部分,成功返回 true,否则返回 false  
	语法:
		bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int src_w, int src_h )



imagecopy() 函数案例:

	  <?php
header("Content-type: image/jpeg");
//创建目标图像
$dst_im = imagecreatetruecolor(150, 150);
//源图像
$src_im = @imagecreatefromjpeg("images/flower_1.jpg");
//拷贝源图像左上角起始 150px 150px
imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );
//输出拷贝后图像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

(9)imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小,成功返回 true ,否则返回 false

	语法:
		bool imagecopyresized( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int dst_w, int dst_h, int src_w, int src_h )
	 
		本函数参数可参看 imagecopy() 函数,只是本函数增加了两个参数(注意顺序): 
		1.dst_w:目标图像的宽度。
		2.dst_h:目标图像的高度。

imagecopyresized() 生成图片缩略图案例:
<?php
header("Content-type: image/jpeg");
//原图文件
$file = "images/flower_1.jpg";
// 缩略图比例
$percent = 0.5;
// 缩略图尺寸
list($width, $height) = getimagesize($file);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 加载图像
$src_im = @imagecreatefromjpeg($file);
$dst_im = imagecreatetruecolor($newwidth, $newheight);
// 调整大小
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//输出缩小后的图像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

(10)imagecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 true ,否则返回 false
	语法:
		bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int src_w, int src_h, int pct )



注意:当为 pct = 100 时对于调色板图像本函数和 imagecopy() 完全一样

imagecopymerge() 函数实现水印功能:
<?php
header("Content-type: image/jpeg");
//原始图像
$dst = "images/flower_1.jpg";
//得到原始图片信息
$dst_im = imagecreatefromjpeg($dst);
$dst_info = getimagesize($dst);
//水印图像
$src = "images/logo.gif";
$src_im = imagecreatefromgif($src);
$src_info = getimagesize($src);	
//水印透明度
$alpha = 30;
//合并水印图片
imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
$src_info[1],$alpha);
//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

(11)imageline() 函数用于绘制一条线段。
	语法:
		bool imageline( resource image, int x1, int y1, int x2, int y2, int color )
	注意:
		用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角坐标为 0,0)画一条线段。
案例:
<?php
header("Content-type: image/png");
$im = @imagecreate(300, 300)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
imageline($im,0,30,200,30,$red);
imagepng($im);
imagedestroy($im);
?>

(12)imagesetstyle() 设定所有画线的函数(例如 imageline() 和 imagepolygon())在使用特殊颜色 IMG_COLOR_STYLED 或者用 IMG_COLOR_STYLEDBRUSHED 画一行图像时所使用的风格。如果成功则返回 TRUE ,失败则返回 FALSE 
	语法:
		bool imagesetstyle( resource image, array style )
	注意:style 参数是像素组成的数组。
案例:
<?php
header("Content-type: image/png");
$im = @imagecreate(300, 50)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
// 画一条虚线,5 个红色像素,4 个背景像素
$style = array($red, $red, $red, $red, $red, $bg, $bg, $bg, $bg);
imagesetstyle($im, $style);
imageline($im, 0, 20, 200, 20, IMG_COLOR_STYLED);
imagepng($im);
imagedestroy($im);
?>

(13)imagearc() 函数用于绘制椭圆弧(包括圆弧)。 

	语法:
		bool imagearc(resource image, int cx, int cy, int w, int h, int s, int e, int color )



imagearc() 函数用于绘制椭圆弧案例:

<?php
header("Content-type: image/png");
$im = @imagecreate(200, 200)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
imagearc($im, 100, 100, 150, 150, 0, 360, $red);
imagepng($im);
imagedestroy($im);
?>


(14)imagefill() 函数用于区域填充
	语法:
		bool imagefill( resource image, int x, int y, int color )
	注意:
		x,y 分别为填充的起始 x 坐标和 y 坐标,与 x, y 点颜色相同且相邻的点都会被填充。

案例:
<?php
header("Content-type: image/png");
$im = @imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
//用 $red 颜色填充图像
imagefill( $im, 0, 0, $red );
imagepng($im);
imagedestroy($im);
?>

(15)imagefilledarc() 函数画一椭圆弧并填充
	语法:
		bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
	注意:
		该函数参数用法可参考绘制椭圆弧函数 imagearc() ,只是本函数增加 style 参数表示填充方式



imagefilledarc() 使用案例:

<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(100, 100);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);
imagepng($im);
imagedestroy($im);
?>

(16)imagefilledrectangle() 函数画一矩形并填充。
	语法:
		bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )
	注意:
		x1,y1为左上角左边,x2,y2为右下角坐标。
案例:
	<?php
	header('Content-type: image/png');
	$im = imagecreatetruecolor(200, 200);
	$yellow = imagecolorallocate($im, 255, 255, 0);
	imagefilledrectangle($im, 20, 150, 40, 200, $yellow);
	imagefilledrectangle($im, 50, 80, 70, 200, $yellow);
	imagepng($im);
	imagedestroy($im);
	?>

(17)imagefilledpolygon() 函数画一多边形并填充。
	语法:
		bool imagefilledpolygon( resource image, array points, int num_points, int color )



绘制一个用红色填充的六边形案例:
	<?php
	header('Content-type: image/png');
	$points = array(
            50, 50,	// Point 1 (x, y)
            100, 50, 	// Point 2 (x, y)
            150, 100, 	// Point 3 (x, y)
            150, 150,	// Point 4 (x, y)
            100, 150, 	// Point 5 (x, y)
            50, 100	// Point 6 (x, y)
       );
	$im = imagecreatetruecolor(200, 200);
	$red = imagecolorallocate($im, 255, 0, 0);
	imagefilledpolygon($im, $points, 6, $red);
	imagepng($im);
	imagedestroy($im);
	?>


	问题?

如何创建缩略图?

	创建一个基本的缩略图需要以下5个步骤:
	①将源图像装载到一个php变量中
	②确定原有图像的高度和宽度
	③创建一个具有正确尺寸的空白缩略图
	④复制原有图像到空白缩略图
	⑤使用正确的内容类型显示缩略图
	
	
	
	案例:
	<?php
	$sourceimage="images/1.jpg";
	$original=imagecreatefromjpeg($sourceimage);		//将源图像载入带一个php变量中
	$dims=getimagesize($sourceimage);				//返回图像的宽度和高度
	
	$thumbwidth=200;								//指定缩略图宽度
	$thumbheight=200;								//指定缩略图高度
	
	$thumb=imagecreatetruecolor($thumbwidth,$thumbheight);	//创建一个指定宽度和高度的空白图像,缩略图将被放置在其中
	
	//此函数将图像的一个调整大小后的版本放置到空白缩略图中
	$imagecopyresampled($thumb,$original,0,0,0,0,$thumbwidth,$thumbheight,$dims[0],$dims[1]);//通过此函数生成缩略图
	
	header("content-type:image/jpeg");				//通过header()函数定义输出的内容类型头
	imagejpeg($thumb);							//使用imagejpeg()函数输出完成的缩略图
	imagedestroy($thumb);							//销毁缩略图所占资源
	imagedestroy($original);							//销毁源图所占资源
	?>
	

在gd2函数库中如何输出中文字符串?

	php中的gd2库支持中文,但必须要以utf-8格式的参数来进行传递,如果使用imageString()函数直接绘制中文字符串就会显示乱码,这是因为gd2函数库对中文只能接受utf-8编码格式,并且默认使用了英文的字体,所以要输出中文字符串,就必须对中文字符串进行转码,并设置中文字符使用的字体,否则输出的只能是乱码
	
	php在图像中添加中文字符串应用的是imagettftext()函数,
	
	语法格式:
	
	array imagettftext(resource image,float size,float angle,int x,int y, int color,string fontfile,string text);
	
	参数1:图像资源
	参数2:字体大小
	参数3:字体的角度,顺时针计算,0度为水平,也就是3点钟的方向(由左到右),90度则为由下到上的文字
	参数4:文字的x坐标值
	参数5:文字的y坐标值
	参数6:文字的颜色
	参数7:字体的文件名称
	参数8:字符串内容

	注意:imagettftext()函数只支持utf-8编码,所以在创建文件时必须也要使用utf-8编码格式,这样才能保证中文字符串的正常输出,但是如果页面本身使用的是gb2312编码格式,那么就需要使用iconv()函数对向图片中添加的中文字符串进行编码格式的转换,由gb2312编码转换为utf-8编码。


如何应用gd2函数为图片添加图像水印?

	使用图片作为水印的前提是该图片的背景必须是透明的,否则输出的效果将很不理想。
	图片水印添加的关键是getimagesize()和imagecopy()函数。应用getimagesize()函数获取上传图片和水印图片的大小,通过imagecopy()函数完成图片水印的添加
	
	语法格式:
	
	bool imagecopy(resource dst_im,resource src_im,int dst_x,int dst_y, int src_x ,int src_y,int src_w ,int src_h);
	
	意义:将src_im图像中的坐标从src_x,src_y	开始,宽度为src_w,,高度为src_h的一部分复制到dst_im图像中坐标为dst_x,dst_y的位置上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值