gd library_GD Library-用PHP绘图的基础

gd library

什么是GD库? ( What is the GD Library? )

gd library_GD Library-用PHP绘图的基础_第1张图片
(startupstockphotos.com/Pexels.com/CC0) (startupstockphotos.com/Pexels.com/CC0)

The GD library is used for dynamic image creation. From PHP we use ​the GD library to create GIF, PNG or JPG images instantly from our code. This allows us to do things such as create charts on the fly, created an anti-robot security image, create thumbnail images, or even build images from other images.

GD库用于动态图像创建。 在PHP中,我们使用GD库从我们的代码立即创建GIF,PNG或JPG图像。 这使我们能够做一些事情,例如即时创建图表,创建反机器人安全图像,创建缩略图图像甚至从其他图像构建图像。

If you are unsure if you have GD library, you can run phpinfo() to check that GD Support is enabled. If you don't have it, you can download it for free.

如果不确定是否具有GD库,则可以运行phpinfo()来检查是否已启用GD支持。 如果没有,您可以免费下载。

This tutorial will cover the very basics of creating your first image. You should already have some PHP knowledge before you start.

本教程将介绍创建第一个图像的基本知识。 在开始之前,您应该已经具有一些PHP知识 。

带有文字的矩形 ( Rectangle With Text )

(unsplash.com/Pexels.com/CC0) (unsplash.com/Pexels.com/CC0)
  1. With this code, we are creating a PNG image. In our first line, the header, we set the content type. If we were creating a jpg or gif image, this would change accordingly.

    使用此代码,我们将创建一个PNG图像。 在第一行标题中,我们设置内容类型。 如果我们要创建jpg或gif图像,则它会相应更改。
  2. Next, we have the image handle. The two variables in ImageCreate () are the width and height of our rectangle, in that order. Our rectangle is 130 pixels wide, and 50 pixels high.

    接下来,我们有图像手柄。 ImageCreate()中的两个变量是矩形的宽度和高度, 按此顺序。 我们的矩形宽130像素,高50像素。

  3. Next, we set our background color. We use ImageColorAllocate () and have four parameters. The first is our handle, and the next three determine the color. They are the Red, Green and Blue values (in that order) and must be an integer between 0 and 255. In our example, we have chosen red.

    接下来,我们设置背景色。 我们使用ImageColorAllocate()并具有四个参数。 第一个是我们的手柄,接下来的三个确定颜色。 它们是红色,绿色和蓝色值(按此顺序),并且必须是0到255之间的整数。在我们的示例中,我们选择了红色。

  4. Next, we choose our text color, using the same format as our background color. We have chosen black.

    接下来,我们选择文本颜色,使用与背景颜色相同的格式。 我们选择了黑色。
  5. Now we enter the text we want to appear in our graphic using ImageString (). The first parameter is the handle. Then the font (1-5), starting X ordinate, starting Y ordinate, the text itself, and finally it's color.

    现在,我们使用ImageString()输入要显示在图形中的文本。 第一个参数是句柄。 然后是字体(1-5),从X坐标开始,从Y坐标开始,文本本身,最后是颜色。

  6. Finally, ImagePng () actually creates the PNG image.

    最后, ImagePng()实际上创建了PNG图像。

玩字体 ( Playing with Fonts )

gd library_GD Library-用PHP绘图的基础_第2张图片
(Susie Shapira/Wikimedia Commons) (Susie Shapira /维基共享资源)

Although most of our code has stayed the same you will notice we are now using ImageTTFText () instead of ImageString (). This allows us to choose our font, which must be in TTF format.

尽管我们的大多数代码保持不变,但是您会注意到我们现在使用ImageTTFText()而不是ImageString() 。 这使我们可以选择必须为TTF格式的字体。

The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and, finally, our text. For the font parameter, you need to include the path to font file. For our example, we have placed the font Quel in a folder called Fonts. As you can see from our example, we have also set the text to print at a 15-degree angle.

第一个参数是我们的句柄,然后是字体大小,旋转度,起始X,起始Y,文本颜色,字体,最后是文本。 对于font参数,您需要包括字体文件的路径。 对于我们的示例,我们将字体Quel放置在名为Fonts的文件夹中。 从示例中可以看到,我们还将文本设置为以15度角打印。

If your text isn't showing, you may have the path to your font wrong. Another possibility is that your Rotation, X and Y parameters are placing the text outside of the viewable area.

如果未显示文本,则可能是您的字体路径错误。 另一种可能性是您的Rotation,X和Y参数将文本放置在可见区域之外。

画线 ( Drawing Lines )

gd library_GD Library-用PHP绘图的基础_第3张图片
(Pexels.com/CC0) (Pexels.com/CC0)

In this code, we use ImageLine () to ​draw a line. The first parameter is our handle, followed by our starting X and Y, our ending X and Y, and, finally, our color.​

在这段代码中,我们使用ImageLine()画一条线。 第一个参数是我们的句柄,其次是我们的起始X和Y,结束X和Y,最后是我们的颜色。

To make a cool volcano like we have in our example, we simply put this into a loop, keeping our starting coordinates the same, but moving along the x axis with our finishing coordinates.

为了像我们的示例中那样制作一个凉爽的火山,我们将其简单地放入一个循环中,保持我们的起始坐标不变,但是沿着x轴移动并带有最终坐标。

绘制椭圆 ( Drawing An Ellipse )

(Pexels.com/CC0) (Pexels.com/CC0)

The parameters we use with Imageellipse () are the handle, the X and Y center coordinates, the width and height of the ellipse, and the color. Like we did with our line, we can also put our ellipse into a loop to create a spiral effect.

我们与Imageellipse()一起使用的参数是手柄,X和Y中心坐标,椭圆的宽度和高度以及颜色。 就像我们在生产线上所做的一样,我们还可以将椭圆放入一个循环中以创建螺旋效果。

If you need to create a solid ellipse, you should use Imagefilledellipse () instead.

如果需要创建一个实心椭圆,则应改用Imagefilledellipse()

弧形和馅饼 ( Arcs & Pies )

(Calqui/Wikimedia Commons/CC BY-SA 3.0) (Calqui / Wikimedia Commons / CC BY-SA 3.0)

Using imagefilledarc we can create a pie, or a slice. The parameters are: handle, center X & Y, width, height, start, end, color, and type. The start and end points are in degrees, starting from the 3 o'clock position.

使用imagefilledarc,我们可以创建一个饼图或切片。 参数为:手柄,中心X和Y,宽度,高度,起点,终点,颜色和类型。 起点和终点以度为单位,从3点钟位置开始。

The types are:

类型是:

  1. IMG_ARC_PIE- Filled arch

    IMG_ARC_PIE-实心拱
  2. IMG_ARC_CHORD- filled with straight edge

    IMG_ARC_CHORD-填充直边
  3. IMG_ARC_NOFILL- when added as a parameter, makes it unfilled

    IMG_ARC_NOFILL-作为参数添加时,使其变为空
  4. IMG_ARC_EDGED- Connects to center. You will use this with nofill to make an unfilled pie.

    IMG_ARC_EDGED-连接到中心。 您将通过nofill使用它来制作未填充的饼。

We can lay a second arc underneath to create a 3D effect like shown in our example above. We just need to add this code in under the colors and before the first filled arc.

我们可以在下面放置第二条弧线以创建3D效果,如上面的示例所示。 我们只需要在颜色下和第一个实心圆弧之前添加此代码。

总结基础 ( Wrapping Up the Basics )

(Romaine/Wikimedia Commons/CC0) (Romaine / Wikimedia Commons / CC0)

So far all of the images we have created have been PNG format. Above, we are creating a GIF using the ImageGif () function. We also change are headers accordingly. You can also use ImageJpeg () to create a JPG, as long as the headers change to reflect it appropriately.

到目前为止,我们创建的所有图像都是PNG格式。 上面,我们正在使用ImageGif()函数创建一个GIF。 我们还相应地更改了标题。 您还可以使用ImageJpeg()创建JPG,只要标题更改为适当地反映它即可。

You can call the php file just like you would a normal graphic. For example:

您可以像正常图形一样调用php文件。 例如:

翻译自: https://www.thoughtco.com/gd-library-basics-drawing-with-php-2693791

gd library

你可能感兴趣的:(python,java,js,css,数据可视化,ViewUI)