背景色
<?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);
?>