Home  >  Article  >  Backend Development  >  Talk about PHP drawing (1)_PHP tutorial

Talk about PHP drawing (1)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:591033browse

实在不敢说是在这里“讲”GD库,因为我用GD也才一两次而已,绝大多数的函数还没
有接触到。可是三斑竹小刁热情地向我约稿,我只好硬着头皮写一点自己的心得。希望能
够起到抛砖引玉的效果。

    其实,我们在web页面里实现“图”的效果不一定非用GD不可,比较容易解决的是柱状
图——用HTML就可以解决。比如:










    /* (1) */
   
 

        /* (2) */
       
          /* (3) */
       
     

/* (4) */
   


是一组数据,数据从哪里来,是无
关大局的,就看你的需要了;代码中需要说两句的地方我都加了注释,现在一一来说明。

(1) 这里要注意的是 valign="bottom",是为了让单元格的内容底部对齐。为什么加在里
    呢?可以让表格里这一行的内容都遵循这一对齐方式,不必在每一个里指定,这样可
    以使PHP执行结果的HTML页的原代码节约好几十个字节呐!节约浏览者的宝贵时间。
   
(2) 注意,最关键的东西在这里!

,我们就是利用table的height属性来
    实现不同高度的“柱”的。我这里为了让大家看得清楚,原始数据没有经过按比例的缩放,
    如果你的数据特别大,或者特别小,都不适宜直接赋给table的height属性,而应该根据情
    况按适当比例缩放这些数据。比如你估计你的这组数据的每一个数字都会在3000~8000之间,
    可以考虑将他们缩小25倍,即 height=""
   
(3) 提一下这一行里的 bgcolor="#xxxxxx",这是柱体的颜色(RGB)。其实,真正的柱状图应该
    每一个柱体用一种颜色,这里为了代码尽量简单,我用了这个for循环,因此也就没办法给
    每一个柱体指定一种颜色。——其实也是有办法的,我只是实在没有必要为了这个例子再写
    一个抽取颜色的函数来把初学者搞晕。所以,那一部分由你自己去完善吧。
   
(4) 在这里以与柱体相同的颜色显示真实的数据。当然,你也可以选择把这个数字放在柱体的顶
    上,可能更专业一些。然而我本人还是习惯于把它放在下面。
   
    借助于HTML的table,我们可以构造出各种柱状图,这个例子讲的是用bgcolor来显示色块,
除此以外,还可以用 background="(图片)" ,图片是带花纹的,于是柱状图的柱体就有了花纹。
而你把真实的数据用反差很大的颜色显示在上面注释(3)所示的那个
里,也是很好的效果。

    前面是回避GD的一个有效的方法,但要做复杂的图形,就非用GD不可了。

    sadly 的PHP4中文手册里,说GD函数库里有44个函数,但我看最新版的英文PHP4手册里,
GD的函数已经有80余个!由于笔者英文比较差,读英文的手册只能连蒙带猜,所以不能确定
新的GD库是否重新支持GIF了?不管怎样,我认为,既然我们在使用完全免费的PHP,何必要
“冒险”去用有版权的GIF?何不免费到底,用PNG呢?只要你不需用动画,PNG同样可以做出
象GIF一样小的文件!

    下面我就结合一段程序,一句代码一句代码地说说常用的这些GD函数。
   
从开头说起吧。

Header("Content-type: image/png");
// 这是发送一个HTTP头,告诉浏览器:“你听着,这是一个图象,可别当成文字来显示呀!”
// 由于我个人的喜好,用了PNG,当然你也可以用 Header("Content-type: image/gif");
// 或者 Header("Content-type: image/jpeg");
$im = ImageCreate (50, 100);
// 创建图象。注意,图象在创建的时候还没有被指定图象格式。
// ImageCreate函数,两个参数,无庸质疑,这是创建的图象的宽度和高度。
// 它的返回值是一个int数值,这个数值相当重要,你继续绘制这个图象、
// 直到你输出这个图象之前,无处不用到这个数值,我们暂且称之为图象的ID。
// 因为使用的频率相当高,所以,我们把它赋给一个名字比较短的变量。

// 现在我们先画一条线吧。画线的函数是这样的:
// imageline (int im, int x1, int y1, int x2, int y2, int col);
// 第一个参数im,就是图象的ID,后面的 x1,y1,x2,y2,不用说了,
// 是起点(x1,y1) 终点(x2,y2)的坐标呀!(图象的左上角坐标是 (0,0) )
// 最后一个参数是什么呀?是颜色!GD要求针对图象定义颜色,用定义的这些颜色来作图。
// 为什么要针对图象定义颜色?我猜测,是为了GIF、PNG等图象用之做“调色板”的。
// 这牵扯到图象本身的知识,这里不赘述了。
// 所以,画线之前,我们还要先定义颜色(真麻烦)。

// $col_red = ImageColorAllocate($im, 255,192,192);
// This function has four parameters, the first $im... Do I still need to say it every time? I won’t say it next time!
// The next three parameters are the red (R), green (G), and blue (B) components of the color to be defined, between 0 and 255.
// This again involves physics-optics knowledge. The different light components of the three primary colors of red, green and blue,
// produce ever-changing colors. The color I defined above is 255 red, 192 green, and 192 blue.
// If I'm not mistaken, this is a brighter red. Let's try to draw a line in a moment.
// Why wait a while? Because if a picture has only one color, nothing can be seen!
// Let’s make the background black first!
// Although it is not clearly stated in the manual, I found that the color defined first will be used as the background by default.

$col_black = ImageColorAllocate($im, 0,0,0);
// Defines a color. There is no red light, green light, or blue light. Natural black - black.
// Then define the color for drawing the line:
$col_red = ImageColorAllocate($im, 255,192,192);

// Now you can start drawing the red line:
imageline ($im , 10, 20, 45, 85, $col_red);
// Don’t worry, you won’t be able to see the image after finishing this sentence.

ImagePNG($im);
// This sentence outputs an image. ImagePNG() outputs a png image, ImageJPEG outputs a jpeg image,
// ImageGIF outputs a gif image... …
// Don’t forget there is a parameter here. If it is displayed on the screen instead of saved as a file,
// then omit this parameter - the saved file name. If you want to save it as a file here,
// You should write it like this: ImagePNG($im, "test.png");
// If you don’t specify a path, this file will be saved in your web page. in the directory.
// If it is JPEG, there is one more parameter, which is JPEG quality (0~100).
// If you want to display it on the screen, then ImageJPEG($im,"",80);
// If you want to save it, then ImageJPEG($im,"test.jpg",80);
// Note that if you want to save this image as a file,
// you cannot use Header("Content-type: image/png"); to send the HTTP header that means the image,
// Because once this happens, it means you will output the image.

ImageDestroy($im);
// Destroy the image in memory to free up memory space.
// That’s it: the simplest GD drawing is done.

// Through testing, it was found that the PNG format used to generate this image file only has 131 bytes.
// And the JPEG format, even with the worst quality (0), requires 855 bytes, the image quality is too bad to watch.
// The highest JPEG quality requires 2360 bytes, but the colors are still not as vivid as when using PNG.
// It can be seen that for this kind of image with a small number of colors, using PNG is much more cost-effective than JPEG.
?>

This time I will stop here and I will try to continue writing as soon as possible.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508283.htmlTechArticleI really don’t dare to say that I am “talking” about the GD library here, because I have only used GD once or twice. Most of the functions have not been touched yet. But Sanbanzhu Xiaodiao enthusiastically asked me for a manuscript, so I had no choice but to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn