Home > Backend Development > PHP Tutorial > Talk about PHP drawing (2)_PHP tutorial

Talk about PHP drawing (2)_PHP tutorial

WBOY
Release: 2016-07-13 17:35:40
Original
992 people have browsed it

上次说了一种简单的回避GD的作图方法,而后又用GD作了最简单的一幅“图”——直线。
这次我就接着画直线向下说。上次代码中详细解释过的部分,这次不再赘述。

Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_black = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
// 今天用橘色吧。
// 跟 imageline 函数完全相同的用法,
ImageDashedLine($im,0,100,199,100,$col_orn);
// 这样就画了一条虚线。

// 下面我们来做个试验。用以说明一个问题。
$col_yel = ImageColorAllocate($im, 255,255,0);
// 黄色。
ImageLine($im,0,99,199,99,$col_yel);
// 在图象的最下沿画了一条黄色的线。
ImageLine($im,200,0,200,100,$col_orn);
// 试图在图象最右沿画一条澄色的线,结果什么也没有。
// 这表明,宽200,高100的图象,其坐标的范围是(0,0)到(199,99)。

ImagePNG($im);
ImageDestroy($im);
// 这一段先结束吧。
?>


    接下来这个效果就爽了!我也是现学现卖。PHP4.0.6以上增加了这个用法——可以用交替的
颜色画线!示例如下:

Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_black = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_red = ImageColorAllocate($im, 255,0,0);

$style=array($col_red,$col_red,$col_black,$col_orn,$col_orn,$col_orn,$col_black);
ImageSetStyle($im, $style);
ImageLine($im, 0, 50, 199, 50, IMG_COLOR_STYLED);

ImagePNG($im);
ImageDestroy($im);
?>

看看效果吧。

    其中我用空行分割开的那三行,说明一下。定义了一个数组 $style,它的成员是一系列的颜色;
然后执行了一个函数,而后用 IMG_COLOR_STYLED “颜色”画出来的是这么神奇的“直线”——
红色、黑色、橙色交替的效果。仔细看一下你就会发现,红、黑、橙交替的顺序,就是我们定义的
$style数组成员的序列:红、红、黑、橙、橙、橙、黑,然后周而复始……
    看明白了吗?注意,这个函数在PHP4.0.6以后才支持。

 

    有了我详细讲解的画线的基础,我想把画其他几何图形的函数一笔代过。需要提示大家的是,无论
画哪种几何图形,无非是抓住这种图形的几个要素。先不算颜色,各种图形的要素如下:

点,两个要素:横坐标、纵坐标

矩形,四个要素:左上角、右下角的横、纵坐标

弧,这样理解:弧可以包括圆弧、椭圆弧;画圆弧画他360度就可以成一个圆,画椭圆弧画他360度也就画
              成一个椭圆;所以这个弧的要素有六:中心点横、纵坐标,横轴长、纵轴长、弧的始、终点。

看下面这段例子。

Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_red = ImageColorAllocate($im, 255,0,0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);

ImageSetPixel($im,20,10,$col_orn);
// 小小一个点,不知道能否看得见?
ImageRectangle($im,25,20,95,55,$col_blu);
// 蓝色的矩形。
ImageArc($im,20,85,50,40,225,360,$col_grn);
// 绿色的椭圆弧,中心在(20,85),横轴50,纵轴40,225度至360度。
// 由此可见,这里的圆弧始、终点是以角度计量,
// 是以水平向右的方向为0度,顺时针计算的。
ImageArc($im,160,60,40,40,0,360,$col_orn);
// 橙色的整圆。只要横轴长与纵轴长相等,就是正圆。
// 上高中我们就学过:圆是椭圆的特例嘛!
// 最后再画一段圆弧。圆心能否在图象以外?
ImageArc($im,160,140,240,240,0,360,$col_red);
// 可以!

ImagePNG($im);
ImageDestroy($im);
?>

Of course when drawing, it is inevitable to paint a certain area with a certain color. GD has three coloring methods, one is rectangular area coloring,
one is coloring the enclosed area where the specified point is located, and the other is coloring the area surrounded by the specified color. Look at the following example:
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im , 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_yel = ImageColorAllocate($im, 255,255,0);
$col_red = ImageColorAllocate($im , 255,0,0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);

ImageFilledRectangle($im ,20,10,100,50,$col_blu);
ImageFilledRectangle($im,5,40,50,90,$col_red);
ImageFilledRectangle($im,40,80,100,95,$col_orn);
ImageFilledRectangle($im,90,35,110,90,$col_yel);
// The above is the first coloring. Draw the rectangle directly.
// I deliberately surrounded a small area with four rectangles of different colors,
// to illustrate the second coloring.

ImagePNG($im);
ImageDestroy($im);

// Take a look at the effect.

?>

Then:

Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im, 0,0, 0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_yel = ImageColorAllocate($im, 255,255,0);
$col_red = ImageColorAllocate($im, 255,0, 0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);

ImageFilledRectangle($im,20,10,100, 50,$col_blu);
ImageFilledRectangle($im,5,40,50,90,$col_red);
ImageFilledRectangle($im,40,80,100,95,$col_orn);
ImageFilledRectangle($ im,90,35,110,90,$col_yel);
// The above is the first coloring. Draw the rectangle directly.
// I deliberately surrounded a small area with four rectangles of different colors,
> // to illustrate the second coloring.

ImageFill($im,70,70,$col_grn);
// This is the second coloring.

ImageRectangle($im,120,40,190,90,$col_grn);
// Let’s draw a rectangle to make the frame. In fact, any shape of border can be used as a frame.
ImageFilltoBorder($im,130,50,$col_grn,$col_orn);
// Paint the green rectangle orange.
// As long as the specified point is within the scope of this "box", it has nothing to do with the position of the point in the area.
// This function actually works like this:
// Starting from the specified point, looking outwards for the boundary of the specified color. If found, stop.
// If not found, just The points passing by are painted in the required color.

ImagePNG($im);
ImageDestroy($im);

// Take a look at the effect.
// Now the picture we made is colorful, but in the browser, on the picture,
// Right-click -> Properties: only 214 bytes!

?>

Let’s stop here this time.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508315.htmlTechArticleLast time I talked about a simple drawing method to avoid GD, and then I used GD to make the simplest drawing method. A "picture" - a straight line. This time I will continue to draw a straight line downwards. Explained in detail in the last code...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template