除了使用GD处理常规图像外,我们还可以从头开始创建自己的图像。 库中的不同功能可用于绘制基本形状,例如椭圆,圆形,矩形,多边形和简单的线条。 通过一些数学运算,这些形状可以创建漂亮的图案。 还有一些功能可以在渲染的图像上绘制文本,这提供了很多可能性。
本教程将教您如何在PHP中绘制基本形状以及如何使用自己喜欢的字体呈现文本。
使用GD在PHP中绘制基本形状
我们将在本节中了解基本形状,然后在后面介绍线的粗细,笔刷和线型。
画线
您可以使用imageline($image, $x1, $y1, $x2, $y2, $color)
函数在两个给定点之间绘制一条简单直线。 $image
参数是一个图像资源,它将使用诸如imagecreatetruecolor()
或imagecreatefromjpeg()
类的函数在前面创建。 在整个教程中,我们将使用imagecreatetruecolor()
从头开始创建新图像。 如果$y1
等于$y2
该函数将绘制一条水平线。 同样,如果$x1
等于$x2
,它将画一条垂直线。
画圆和弧
函数imagearc($image, $cx, $cy, $width, $height, $start, $end, $color)
可以使用$cx
和$cy
为中心绘制圆弧。 $width
和$height
参数确定不同轴上弧的大小。 $start
和$end
参数指定弧的开始和结束角度(以度为单位)。 如果要绘制从0到360度的完整弧,可以使用替代imageellipse($image, $cx, $cy, $width, $height, $color)
函数。
绘制矩形和多边形
您可以使用imagerectangle($image, $x1, $y1, $x2, $y2, $color)
函数在图像上绘制矩形。 $x1
和$y1
值确定矩形的左上角。 $x2
和$y2
值确定右下角。 还有一个imagepolygon($image, $points, $num_points, $color)
函数,可以创建具有任意数量的边或点的多边形。 $points
参数是一个数组,其中两个元素配对在一起以获取特定点的坐标。
PHP 7中已添加了另一个名为imageopenpolygon()
函数,该函数不会在第一个点和最后一个点之间画线。
放在一起创建图形
在下面的示例中,我们使用了所有这些功能来创建带有小屋,太阳和地面的线条图。
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;
$img = imagecreatetruecolor($img_width, $img_height);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$orange = imagecolorallocate($img, 255, 200, 0);
imagefill($img, 0, 0, $white);
imagerectangle($img, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*8/10, $red);
imagerectangle($img, $img_width*4/10, $img_height*5/10, $img_width*8/10, $img_height*8/10, $red);
imagepolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*5/10], 3, $red);
imageopenpolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*7/10, $img_height*2/10, $img_width*8/10, $img_height*5/10], 3, $red);
imageellipse($img, 100, 100, 100, 100, $orange);
imagearc($img, $img_width*3/10, $img_height*8/10, 100, 200, 180, 360, $red);
imageline($img, 0, $img_height*8/10, $img_width, $img_height*8/10, $green);
imagepng($img);
?>
这里重要的是要弄清楚不同坐标的值。 我想绘制相对于原始图像大小的所有内容,因此我使用$img_height
和$img_width
变量来计算不同点的坐标。
控制线条的粗细,样式和颜色填充
上面的图像有一些问题,例如线条很细且没有着色。 所有这些问题都可以很容易地使用类似的功能是固定imagesetthickness()
和imagefilledrectangle()
线的粗细
imagesetthickness($image, $thickness)
函数设置绘制矩形,多边形,圆弧等时渲染线的粗细。例如,将$thickness
设置$thickness
5将使任何使用imagerectangle()
, imagearc()
, imagepolygon()
等5像素厚。
绘制填充形状
每个绘图功能还具有填充颜色版本,该颜色版本将给定的颜色填充到特定图形中。 例如, imagefilledrectangle()
将使用给定的颜色填充绘制的矩形。
使用笔刷
一个非常有用的GD函数是imagesetbrush($image, $brush)
。 此函数中的$brush
参数只是另一个可用于绘制线条的图像资源。 例如,您可以使用花朵的透明矢量绘图作为画笔,为图像添加漂亮的花朵图案。 下面给出的代码段被编写为在绘制点时使用云的图像作为画笔。 这在我们的天空中添加了单个云。
<?php
$img = imagecreatetruecolor($img_width, $img_height);
$clouds = imagecreatefrompng('clouds.png');
$clouds = imagescale($clouds, 250);
imagesetthickness($img, 5);
imagesetbrush($img, $clouds);
imageline($img, $img_width*9/10, 50, $img_width*9/10, 50, IMG_COLOR_BRUSHED);
?>
我在png上找到了此云图像 ,并将其缩小到适合我们项目的大小。
小屋图像的完整代码如下。 我们仅为每个图形添加了两个版本,一个用于绘制轮廓,另一个用于填充颜色。
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;
$img = imagecreatetruecolor($img_width, $img_height);
$clouds = imagecreatefrompng('clouds.png');
$clouds = imagescale($clouds, 250);
imagesetthickness($img, 5);
imagesetbrush($img, $clouds);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 200, 250);
$orange = imagecolorallocate($img, 255, 200, 0);
$brown = imagecolorallocate($img, 220, 110, 0);
imagefill($img, 0, 0, $white);
imagefilledrectangle($img, 0, 0, $img_width, $img_height*8/10, $blue);
imagefilledrectangle($img, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*8/10, $red);
imagefilledrectangle($img, $img_width*4/10 - 2, $img_height*5/10, $img_width*8/10, $img_height*8/10, $red);
imagefilledpolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*5/10], 3, $red);
imagefilledpolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*7/10, $img_height*2/10, $img_width*8/10, $img_height*5/10, $img_width*4/10, $img_height*5/10], 4, $red);
imagefilledarc($img, 100, 100, 100, 100, 0, 360, $orange, IMG_ARC_PIE);
imagefilledarc($img, $img_width*3/10, $img_height*8/10, 100, 200, 180, 360, $brown, IMG_ARC_PIE);
imagefilledrectangle($img, 0, $img_height*8/10, $img_width, $img_height, $green);
imagerectangle($img, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*8/10, $black);
imagerectangle($img, $img_width*4/10 - 2, $img_height*5/10, $img_width*8/10, $img_height*8/10, $black);
imagepolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*5/10], 3, $black);
imageopenpolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*7/10, $img_height*2/10, $img_width*8/10, $img_height*5/10], 3, $black);
imagearc($img, 100, 100, 100, 100, 0, 360, $black);
imagearc($img, $img_width*3/10, $img_height*8/10, 100, 200, 180, 360, $black);
imageline($img, $img_width*9/10, 50, $img_width*9/10, 50, IMG_COLOR_BRUSHED);
imagerectangle($img, -100, $img_height*8/10, $img_width*11/10, $img_height*11/10, $black);
imagepng($img);
?>
这是上面的PHP GD代码的最终结果。
在图像上渲染文本
PHP GD带有四个不同的功能,可让您在水平或垂直方向上呈现多个字符或仅呈现一个字符。 这些函数是imagechar()
, imagecharup()
, imagestring()
和imagestringup()
。 它们都接受相同的六个参数,因此我们将在这里讨论imagechar()
函数。
$font
参数imagechar($image, $font, $x, $y, $string, $color)
函数只是呈现文本的大小。 它仅接受1到5之间的整数值。 $string
参数是您要呈现的文本。 如果将多个字符的字符串传递给char函数,则仅第一个字符将呈现在图像上。 imagecharup()
和imagestringup()
函数将从底部到顶部垂直呈现文本。
在渲染文本时,我们上面讨论的四个功能非常有限。 您会发现即使最大的字体大小值对于正常使用来说也太小。 另外,文本只能水平和垂直书写。
幸运的是,GD还具有imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text)
函数,该函数可以使用所需的任何字体呈现文本。 $fontfile
参数用于指定要用于显示文本的TrueType字体的路径。 $x
和$y
参数确定渲染文本的起始位置。
下面的示例使用所有这些功能来创建一些漂亮的文本效果。
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;
$img = imagecreatetruecolor($img_width, $img_height);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 200, 250);
$orange = imagecolorallocate($img, 255, 200, 0);
$brown = imagecolorallocate($img, 220, 110, 0);
imagefill($img, 0, 0, $white);
imagestringup($img, 5, $img_width*19/20, $img_height*19/20, 'This sentence was written using imagestringup()!', $blue);
imagestring($img, 5, $img_width/20, $img_height/20, 'This sentence was written using imagestring()!', $red);
$passion_one = dirname(__FILE__) . '/PassionOne-Regular.ttf';
imagettftext($img, 32, 0, $img_width/20, $img_height*2/10, $black, $passion_one, 'This is Passion One Font!');
$merriweather = dirname(__FILE__) . '/Merriweather-Regular.ttf';
imagettftext($img, 24, 90, $img_width*9/10, $img_height*19/20, $black, $merriweather, 'This is Merriweather Regular Font!');
$patua_one = dirname(__FILE__) . '/PatuaOne-Regular.ttf';
imagettftext($img, 32, 0, $img_width/20, $img_height*3/10 + 2, $black, $patua_one, 'This is Patua One Font!');
imagettftext($img, 32, 0, $img_width/20, $img_height*3/10, $orange, $patua_one, 'This is Patua One Font!');
$monoton = dirname(__FILE__) . '/Monoton-Regular.ttf';
imagettftext($img, 72, 0, $img_width/20, $img_height*5.5/10 - 5, $brown, $monoton, 'MONOTON');
imagettftext($img, 72, 0, $img_width/20, $img_height*5.5/10 + 5, $orange, $monoton, 'MONOTON');
imagettftext($img, 72, 0, $img_width/20, $img_height*5.5/10, $blue, $monoton, 'MONOTON');
$kaushan = dirname(__FILE__) . '/KaushanScript-Regular.ttf';
imagettftext($img, 84, 0, $img_width/20, $img_height*8/10 - 2, $brown, $kaushan, 'Good Night!');
imagettftext($img, 84, 0, $img_width/20, $img_height*8/10 + 2, $black, $kaushan, 'Good Night!');
imagettftext($img, 84, 0, $img_width/20 - 2, $img_height*8/10, $brown, $kaushan, 'Good Night!');
imagettftext($img, 84, 0, $img_width/20 + 2, $img_height*8/10, $black, $kaushan, 'Good Night!');
imagettftext($img, 84, 0, $img_width/20, $img_height*8/10, $white, $kaushan, 'Good Night!');
imagepng($img);
?>
如您所见,我们以相同的字体在稍有不同的位置渲染了相同的文本,以创建一些效果,例如基本文本阴影。 要记住的重要一点是,任何文本函数呈现的文本在出现重叠的情况下都会完全隐藏其下方的文本。 这是运行上述代码后获得的最终图像。
最后的想法
本教程的目的是使您熟悉不同的GD函数,以从PHP开始从头绘制基本形状。 借助一些数学运算,您将能够使用这些功能来创建更复杂的形状,例如正多边形,圆角矩形等。
PHP GD还具有一些非常有用的功能,可用于在图像上渲染文本。 使用漂亮的字体可以确保将渲染的文本放在从不同文件路径加载的常规图像上时不会显得怪异。
您是否还在PHP中创建了更多精美的文本效果? 请在评论中与我们分享。
翻译自: https://code.tutsplus.com/tutorials/rendering-text-and-basic-shapes-using-gd--cms-31767