PHP之GD2使用

来源:互联网 发布:淘宝企业店铺开通 编辑:程序博客网 时间:2024/04/27 18:38

我们的php和java一样,有自己的图像处理函数,下面我们就介绍一下php中的绘制图像的大致流程

1.创建一个画布
2.开始绘画
3.输出图像
4.销毁图片(释放内存)

接下来我们介绍几个php中常用的GD2函数

创建一个基于真彩的画布imagecreatetruecolor(int x_size,int y_size);分配一个颜色imagecolorallocate(resource image,int red,int green,int blue);//红绿蓝区域填充imagefill(resource image,int x,int y,int color);矩形框imagerectangle(resource image,int x1,int y1,int x2,int y2,int color);画一个像素点imagesetpixel(resource image,int x,int y,int color);画一条线段imageline(resource image,int x1,int y1,int x2,int y2,int color);绘制文本内容imagettftext(resource image,float size,float angle,int x,int y,int color,string content);以png格式将图像输出到浏览器或文件imagepng(resource image[,string filename]);销毁一个图像imagedestroy(resource image);int rand([int min,int max]);//产生一个随机数strlen();//获取字符串的长度header();//设置相应头信息

PS:我自己的理解,就是我们先创建一个画布,然后就像我们画画一样,利用自己喜欢的颜色,去绘制图像,有时不要将问题想得太复杂,不然会适得其反,

同时,我们一定要注意代码的顺序,不然我们会得到意想不到的效果,

1.画布:imagecreate(int width,int height);

2.颜色:imagecolorallocate(resource image,int red,int green,int blue);

3.利用颜色我们在画布上进行绘制图像

imagefill,imagerectangle等

4.输出图像(向浏览器输出)

imagepng,imagegif,imagejpeg,imageline等

5.销毁图片,释放内存

imagedeatroy();


demo:

<?php$im=imagecreate(200,200);$color=imagecolorallocate($im,8,2,133);imagefill($im,$color);header("content-type:image/png");imagepng($im);imagedeatroy($im);?>

希望大家多多动手,当你运行出结果后,你收获的不只是知识

原创粉丝点击