php GD库的使用

两大功能:自己画,对于原有图片改变

PHP中GD库的使用


    PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType(加载操作系统中的字体)、Type1等字体库。三种都有用的
JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。
PNG 是可移植的网络图像,对图像采用了无损压缩标准。
GIF 原义是“图像互换格式”,是一种基于LZW算法的连续色调的无损压缩格式 。

此外需要自行安装GD库(wamp自带,linux装好麻烦),还有一种画图的库叫做ImageMagick,可以用于大网站,功能比GD库更全,运用难度差不多,14年的时候用的不多,不知道现在咋样==

这里面的函数会用就好,不用都背

使用GD库画图

步骤:

在PHP中创建一个图像应该完成如下所示的4个步骤:
1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。
2.在背景上绘制图像轮廓或输入文本。
3.输出最终图形
4.释放资源

画布管理

imagecreate -- 新建一个基于调色板的图像
resource imagecreate ( int x_size, int y_size )
本函数用来建立空新画布,参数为图片大小,单位为像素 (pixel)。支持256色。
imagecreatetruecolor -- 新建一个真彩色图像
resource imagecreatetruecolor ( int x_size, int y_size )
新建一个真彩色图像画布 ,需要 GD 2.0.1 或更高版本,不能用于 GIF 文件格式。
imagedestroy -- 销毁一图像
bool imagedestroy ( resource image )
imagedestroy() 释放与 image 关联的内存。

设置颜色

imagecolorallocate -- 为一幅图像分配颜色
语法:int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。

生成图片


imagegif -- 以 GIF 格式将图像输出到浏览器或文件
语法:bool imagegif (resource image [,string filename] )
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
语法:bool imagejpeg (resource image [,string filename [, int quality]] )
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
语法:bool imagepng (resource image [,string filename] )
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
语法:bool imagewbmp (resource image [, string filename [, int foreground]] )

<?php
	$img=imagecreatetruecolor(200,200);
	$white=imagecolorallocate($img,0xFF,0xFF,0xFF);
	$red=imagecolorallocate($img,255,0,0);
	$blue=imagecolorallocate($img,255,255,255);
	imagefill($img,0,0,$white);
	imageline($img,0,0,200,200,$blue);
	imageline($img,200,0,0,200,$red);
	header("Content-Type:images/png");
	imagePng($img);//第二个参数是输出图片到文件夹 P大写!!
	imagedestroy($img);

效果

使用GD库绘制各种常见图形

弱弱的吐槽一下函数名,和c#有什么区别嘛~

imagefill -- 区域填充
语法:bool imagefill(resource image,int x,int y, int color)
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagesetpixel -- 画一个单一像素
语法:bool imagesetpixel ( resource image, int x, int y, int color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
imageline -- 画一条线段
语法:bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。

imagerectangle -- 画一个矩形
语法:bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col )
imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。
imagefilledrectangle -- 画一矩形并填充
语法:bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )
imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。

imageellipse -- 画一个椭圆
语法:bool imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imageellipse() 在 image 所代表的图像中画一个中心为 cx,cy(图像左上角为 0, 0)的椭圆。w 和 h 分别指定了椭圆的宽度和高度,椭圆的颜色由 color 指定。
imagefilledellipse -- 画一椭圆并填充
语法:bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)为中心画一个椭圆。w 和 h 分别指定了椭圆的宽和高。椭圆用 color 颜色填充。如果成功则返回 TRUE,失败则返回 FALSE。

imagearc -- 画椭圆弧
bool imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color )
imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
imagefilledarc -- 画一椭圆弧且填充
bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。如果成功则返回 TRUE,失败则返回 FALSE。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(OR)后的值:
IMG_ARC_PIE        IMG_ARC_CHORD
IMG_ARC_NOFILL        IMG_ARC_EDGED

imagestring -- 水平地画一行字符串
语法:bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagestringup -- 垂直地画一行字符串
语法:bool imagestringup ( resource image, int font, int x, int y, string s, int col )
imagestring()用 col 颜色将字符串 s 垂直地画到 image 所代表的图像的 x, y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。

imagechar -- 水平地画一个字符 (用来做验证码)
语法:bool imagechar ( resource image, int font, int x, int y, string c, int color )
imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 是 1,2,3,4 或 5,则使用内置的字体(更大的数字对应于更大的字体)。
imagecharup -- 垂直地画一个字符
语法:bool imagecharup ( resource image, int font, int x, int y, string c, int color )
imagecharup() 将字符 c 垂直地画在 image 指定的图像上,位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 为 1,2,3,4 或 5,则使用内置的字体。
imagettftext -- 用 TrueType 字体向图像写入文本
语法 :array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

<?php
	$img=imagecreatetruecolor(200,200);
	$white=imagecolorallocate($img,0xFF,0xFF,0xFF);
	$red=imagecolorallocate($img,255,0,0);
	$blue=imagecolorallocate($img,0,0,0xFF);
	$pink=imagecolorallocate($img,0xFF,0,0xFF);
	$green=imagecolorallocate($img,0,0xFF,0);
	imagefill($img,0,0,$white);
	imageline($img,0,0,200,200,$blue);
	imageline($img,200,0,0,200,$red);
	imagerectangle($img,50,50,150,150,$blue);
	imagefilledrectangle($img,75,75,125,125,$pink);
	imageellipse($img,50,50,150,150,$red);
	imagefilledellipse($img,50,50,150,150,$pink);
	imagearc($img,150,50,100,100,-90,0,$red);
	imagestring($img,5,100,150,"hello world",$red);
	imagestringup($img,5,150,100,"hello world",$red); 
	imagettftext($img,20,0,20,100,$green,"./simkai.ttf","MissZhou加油啊");
	//字体的名字必须是英文的,我用汉仪雪君体就不成功==
	header("Content-Type:images/png");
	imagePng($img);//第二个参数是输出图片到文件夹 P大写!!
	imagedestroy($img);
	

综合案例:画时钟

<body>
	<img id="time" src="test3.php" />


	<script>
		setInterval(function(){
				document.getElementById("time").src="test3.php?"+Math.random();
				
			},1000);
	</script>	
</body>

<?php
	//获取系统时间
	date_default_timezone_set("PRC");

	$h = date("H");
	$i = date("i");
	$s = date("s");

	//1 创建资源(画布的大小)
	$img = imagecreatetruecolor(200, 250);	
	//设置画布的颜色
	$white =  imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
	$red =  imagecolorallocate($img, 255, 0, 0);
	$blue =  imagecolorallocate($img, 0, 0, 0XFF);
	$pink =  imagecolorallocate($img, 0XFF, 0, 0XFF);
	$green =  imagecolorallocate($img, 0, 0xFF, 0);
	imagefill($img, 0, 0, $white);
	//2. 制作各种颜色
	imageellipse($img, 100, 100, 190, 190, $blue);
	imagefilledellipse($img, 100, 100, 4, 4, $blue);
	imagestring($img, 3, 95, 8,  "12", $blue);
	imagestring($img, 3,180, 95, "03", $blue);
	imagestring($img, 3, 95, 180,  "06", $blue);
	imagestring($img, 3, 11, 95,  "09", $blue);
	//秒针
	$len = 80;
	 $a = $len*sin(pi()/30*$s);
	 $b = $len*cos(pi()/30*$s);
	$x = 100 + $a;
	$y = 100 - $b;
        imageline($img, 100, 100, $x, $y, $red);
	//数字的时间
	imagestring($img, 5, 20, 230, "NOW: {$h}:{$i}:{$s}", $red);
	//4保存,或输出给浏览, 写第二个参数就是保存
	header("Content-Type:images/gif");
	imagegif($img);
	//5. 释放资源
	imagedestroy($img);

PHP图片处理

从指定的图片文件或 URL地址来新建一个图像。成功则返回一个图像标识符,失败时返回一个空字符串,并且输出一条错误信息。由于格式不同,则需要分别使用对应图片背景处理函数。
resource imagecreatefrompng ( string filename ) 从 PNG 文件或 URL 新建一图像
resource imagecreatefromjpeg ( string filename )
    从 JPEG 文件或 URL 新建一图像
resource imagecreatefromgif ( string filename )
    从 GIF 文件或 URL 新建一图像  
resource imagecreatefromwbmp ( string filename ) 从 WBMP 文件或 URL 新建一图像

<?php
	$img=imagecreatefrompng("./cx.png");//一定要对应!!
	$color=imagecolorallocate($img,0,255,0);
	imageline($img,0,0,200,200,$color);
	imagepng($img,"ncx.png");
	imagedestroy($img);
等同于:

<?php
	$img=imagecreatefrompng("./cx.png");
	$color=imagecolorallocate($img,0,255,0);
	imageline($img,0,0,imagesx($img),imagesy($img),$color);
	imagepng($img,"ncx.png");
	imagedestroy($img);
	

存贮图像信息:

<?php
	$img=imagecreatefrompng("./cx.png");
	list($width,$height,$type)=getimagesize("./cx.png");
	$color=imagecolorallocate($img,0,255,0);
	imageline($img,0,0,imagesx($img),imagesy($img),$color);
	imagepng($img,"ncx.png");
	imagedestroy($img);
	


其他图像处理函数:
int imagesx ( resource image )
    取得图像宽度

int imagesy ( resource image )
    取得图像高度
 
array getimagesize ( string $filename [, array &$imageinfo ] )
    取得图像大小、类型等信息

一般化处理图像

<?php
	function cimgstr($imgname, $string) {
		list($width, $height, $type)= getimagesize($imgname);

		$types = array(1=>"gif", 2=>"jpeg", 3=>"png");

		//变量函数
		$createimage = "imagecreatefrom".$types[$type];

		$img = $createimage($imgname);
		
		$red =  imagecolorallocate($img, 0xFF, 0, 0);

		
		$x = ($width-imagefontwidth(5)*strlen($string))/2;
		$y = ($height-imagefontheight(5))/2;


		imagestring($img, 5, $x, $y, $string, $red);
		
		//变量函数
		$save = "image".$types[$type];
		$save($img, "new_".$imgname);
		imagedestroy($img);
	}


	cimgstr("dx.jpg", "meizi");
	cimgstr("map.gif", "meizi");
	cimgstr("cx.png", "meize");

	

图片缩放和裁剪


bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
        重采样拷贝部分图像并调整大小,是将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。成功时返回 TRUE, 或者在失败时返回 FALSE。其中dst_image 和 src_image 分别是目标图像和源图像的标识符。




  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值