PHP GD库笔记

背景色

<?php
	//创建图像大小 
	//imagecreate($width, $height)
	$image = imagecreate(200, 100);
	
	//创建图像背景色(红,绿,蓝)
	//imagecolorallocate($image, $red, $green, $blue)
	imagecolorallocate($image, 255, 255, 255);
	
	//告诉浏览器,这是什么,没有告诉浏览器,浏览器会识别不了图像,默认为'Content-Type:text/html'
	header('Content-Type:image/png');
	//输出图像
	imagegif($image);
	//销毁图像资源
	//该函数不是必须的,但使用它是一个好习惯。
	imagedestroy($image);
?>


绘制线条

<?php
	//创建图像大小 
	$image = imagecreate(100, 100);
	//创建图像背景色(红,绿,蓝)
	imagecolorallocate($image, 255, 255, 255);
	
	//创建线颜色
	$lineColor = imagecolorallocate($image, 150, 150, 150);
	//绘制一条线
	//imageline($image, $x1, $y1, $x2, $y2, $color)
	imageline($image, 10, 10, 90, 90, $lineColor);
	
	//输出图像
	header('Content-Type:image/png');
	imagepng($image);
?>


画布

<?php
	$imageWidth = 100;
	$imageHeight = 30;
	
	//创建画布
	//imagecreatetruecolor($width, $height)
	$image = imagecreatetruecolor($imageWidth, $imageHeight);
	
	//创建画布颜色
	//imagecolorallocate($image, $red, $green, $blue)
	//十六进制0xcc转换成十进制204
	$backgroundColor = imagecolorallocate($image, 0xcc, 0xcc, 0xcc);
	
	//用画布颜色填充画布
	//imagefill($image, $x, $y, $color)
	imagefill($image, 0, 0, $backgroundColor);
	
	header('Content-Type:image/gif');
	imagegif($image);
?>


制作验证码干扰元素效果

<?php
	$imageWidth = 100;
	$imageHeight = 100;
	
	$image = imagecreate($imageWidth, $imageHeight);
	imagecolorallocate($image, 255, 255, 255);
	
	//mt_rand() 比rand() 快四倍,生成更好的随机数。
	
	//制作干扰线
	for ($i = 0; $i < 10; $i++) {
		$randColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
		imageline($image, mt_rand(0, $imageWidth), 0, mt_rand(0, $imageHeight*5), $imageHeight, $randColor);
	}
	
	//制作干扰点
	for ($i = 0; $i < 300; $i++) {
		$randColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
		//绘制点
		//imagesetpixel($image, $x, $y, $color)
		imagesetpixel($image, mt_rand(0, $imageWidth), mt_rand(0, $imageHeight), $randColor);
	}
	
	header('Content-Type:image/gif');
	imagegif($image);
?>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值