绘制图像:
1.填充画布
imagefill();
2.画一个像素
imagesetpixel();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画点
for($i=0;$i<10000;$i++){
imagesetpixel($img,mt_rand(0,500),mt_rand(0,300),$black);
}
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
3.画线
imageline();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画点
for($i=0;$i<10;$i++){
imageline($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
}
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
4.画一个矩形
imagerectangle();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画点
for($i=0;$i<100;$i++){
imagerectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
}
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
5.画一矩形并填充
imagefilledrectangle();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画点
// for($i=0;$i<100;$i++){
// imagefilledrectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
// }
imagefilledrectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
6.画一个多边形
imagepolygon();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
$arr=array(
250,50,
50,250,
450,150,
);
imagepolygon($img,$arr,3,$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
7.画一个多边形并填充
imagefilledpolygon();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
$arr=array(
250,50,
50,250,
450,150,
450,50,
);
imagefilledpolygon($img,$arr,4,$green);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
***
多边形干扰素
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
for($i=0;$i<100;$i++){
$arr=array(
mt_rand(0,500),mt_rand(0,300),
50,250,
450,150,
450,50,
);
imagepolygon($img,$arr,4,$green);
}
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
8.画一个椭圆
imageellipse();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
for($i=0;$i<100;$i++){
imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
}
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
9.画一个椭圆并填充
imagefilledellipse();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
// for($i=0;$i<100;$i++){
// imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }
imagefilledellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
10.画一个椭圆弧
imagearc();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
// for($i=0;$i<100;$i++){
// imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }
imagearc($img,mt_rand(0,500),mt_rand(0,500),500,300,0,120,$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
11.画一个椭圆弧并填充
imagefilledarc();
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
// for($i=0;$i<100;$i++){
// imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }
imagefilledarc($img,250,150,200,200,0,340,$blue,IMG_ARC_PIE);
imagefilledellipse($img,272,98,20,20,$yellow);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
***
**
<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);
// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3、在画布上画图像或文字
imagefill($img,0,0,$white);
// 4、画
imagefilledarc($img,250,150,200,200,0,70,$blue,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,70,160,$red,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,160,230,$yellow,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,230,360,$green,IMG_ARC_PIE);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);
// 6、释放画布资源
imagedestroy($img);
?>
12.画字符串
imagettftext();
**
<?php
// 1.创建画布资源
$img=imagecreatetruecolor(500,300);
// 2.准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3.在画布上画图像或文字
imagefill($img,0,0,$white);
// 随时生产5位字符串
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$randStr=join('',array_slice($arr,0,5));
// 4.画点
$str='aXdwJ';
imagettftext($img,90,0,50,200,$blue,'D:/xampp/htdocs/php2/msyhbd.ttf',$randStr);
// 5.输出最终图像或保存最终图像
header('content-type: image/png');
imagepng($img);
// 6.释放画布资源
imagedestroy($img);
?>
生成5位随时数图片
<?php
// 1.创建画布资源
$img=imagecreatetruecolor(500,300);
// 2.准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);
// 3.在画布上画图像或文字
imagefill($img,0,0,$white);
// 随时生产5位字符串
// mt_rand — 生成更好的随机数
// range — 根据范围创建数组,包含指定的元素
// array_merge — 合并一个或多个数组
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
// shuffle-打乱数组
shuffle($arr);
// array_slice-从数组中取出一段
// join-将一个一维数组的值转化为字符串
$randStr=join('',array_slice($arr,0,5));
// 4.画点
$str='aXdwJ';
imagettftext($img,80,mt_rand(-30,30),50,200,$blue,'D:/xampp/htdocs/php2/msyhbd.ttf',$randStr);
// 5.输出最终图像或保存最终图像
header('content-type: image/png');
imagepng($img);
// 6.释放画布资源
imagedestroy($img);
?>