画布的生成

1.header

(1)header('content-type:image/png');

(2)header('content-type:image/gif');

(3)header('content-type:image/jpeg');

2创建画布

<?php
header('content-type:image/png');
//新建一个真彩色图像
$im = imagecreatetruecolor(200,100);//宽度为200px;高度为100px;
//创建颜色
$color = imagecolorallocate($im, 255, 0, 0);
//填充区域
imagefill($im, 0, 0, $color);
//输出画布
imagepng($im);
//销毁图像(释放占用的资源)
imagedestroy($im);
//销毁图像(释放占用的资源)
imagedestroy($im);

颜色管理:int imagecolorallcate(resource $img,int $red,int $green,int $blue)

red,green,blue分别是所需要的颜色的红、绿、蓝成分,这些参数是0到255的整数或者十六进制的0x00到0xFF

填充颜色: bool imagefill(resource $image,int $x,int $y,int $color)

image图像的坐标x,y(图像上左角为0,0)处用color颜色执行区域填充(x,y点颜色相同且相邻的点都会被填充)

绘制图形:imagesetpixel()在image图像中用color颜色在x,y坐标(图像左上角为0,0)上画一个点

imageline()用color颜色在图像image中从坐标x1,y1到x2,y2(图像左上角为0,0)画一条线段

imagerectangle()用color颜色在image图像上画一个矩形,其左上角坐标为x1,y1,右下角坐标为x2,y2,图像上的左上标为0,0

imagefilldrectangle()在image图像上画一个用color颜色填充的矩形,其左上角坐标为x1,y1,右下角坐标为x2,y2,图像上的左上标为0,0

绘制文字:array imagettftext(resource $image,float $size,float $angle,int $x,int $y,int $color,string $fontfile,string $text)

imagecreate fromgd --- 从gd文件或URL新建一图像

imagecreate fromgif --- 从文件或URL新建一图像

imagecreate fromstring --- 从字符串的图像流新建一图像

下面我们来实现一个画躁线和噪点,以及输入文字的代码

<?php
header('content-type:image/png');
//创建一个画布(在内存中存放)
$im = imagecreatetruecolor(200,100);
//创建颜色
$color = imagecolorallocate($im, 255, 0, 0);
//填充区域
imagefill($im, 0, 0, $color);

$color = imagecolorallocate($im, 0, 0, 0);
imagesetpixel($im, 100, 50, $color);

//随机画10个点
for($i=0;$i<10;$i++){
	//生成随机数
	$x = rand(0,200);
	$y = rand(0,100); 
	imagesetpixel($im, $x, $y, $color);
}

//随机画10条线
$color = imagecolorallocate($im, 0, 0, 255);
for($i=0;$i<10;$i++){
	$x1 = rand(0,200);
	$y1 = rand(0,100);
	$x2 = rand(0,200);
	$y2 = rand(0,100);	
	imageline($im, $x1, $y1, $x2, $y2, $color);
}
//画一个矩形
$color = imagecolorallocate($im, 0, 255, 0);
// imagerectangle($im, 50, 50, 100, 100, $color);
imagefilledrectangle($im, 50, 50, 100, 100, $color);
//输出文字
$text = "hello";
$color = imagecolorallocate($im, 255, 0, 255);
$font = "simsunb.ttf";
imagettftext($im, 20, 0, 10, 50, $color, $font, $text);
//输出画布
imagepng($im);
//销毁图像(释放占用的资源)
imagedestroy($im);
//销毁图像(释放占用的资源)
imagedestroy($im);

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值