图像操作中常用到的代码

1.header指令

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

header ( 'Content-Type: image/gif' );

header ( 'Content-Type: image/jpeg' );

2.创建画布(在内存中存放)

关键字:imagecreatetruecolor  新建一个真彩色图像
200  图像的宽度
100  图像的高度

$img = imagecreatetruecolor(200,100);

3.输出图像

  关键字:imagepng  以PNG格式将图像输出到浏览器上

imagepng($img);

4.销毁图像(释放占用的资源)
关键字:imagedestroy    销毁图像

imagedestroy($img);

5.颜色管理
关键字:imagecolorallocate 为一个图像分配颜色

$color = imagecolorallocate($img,0xcc,0xcc,0xcc);


6.填充颜色
关键字:imagefill  颜色填充
0代表坐标

imagefill($img,0,0,$color);

7.绘制图形
关键字:imagesetpixel  画一个点
先绘制一个黑色的点
 

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

随机画10个点
 

$color = imagecolorallocate($img, 0, 0, 0);
//随机画10个点
for ($i=0; $i <10 ; $i++) { 
    $x = rand(0,200);
    $y = rand(0,100);
    imagesetpixel($img, $x, $y, $color);
}
imagesetpixel($img, 100, 50, $color);

imageline  画一条线
 

$color = imagecolorallocate($img, 0, 0,255); 画一条蓝色的线
imageline ($img,0,0,200,100,$color)

随机画10条线
 

$color = imagecolorallocate($img, 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($img, $x1, $y1, $x2, $y2, $color);
}

画矩形
关键字: imagecolorallocate( 画一个矩形)   imagefilledrectangl( 画一个矩形并填充)  

$color = imagecolorallocate($img, 0, 255, 0);
//imagerectangle($img, 50, 50, 100, 100, $color);
imagefilledrectangle($img, 50, 50, 100, 100, $color);

8.绘制文字
关键字:imagettftext  用true type 字体向图像写入文本

$text = "hello";
$color = imagecolorallocate($img, 255, 0, 255);
$font = "simsun.ttc";
imagettftext($img, 20, 0, 10, 50, $color, $font, $text);


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值