将一些文本写入图像

要查看 GD 库是否正确安装,我们可以尝试创建一个包含简单文本字符串的图像。

要创建图像,我们使用 imagecreate 函数,而添加文本本身是使用 imagestring 函数完成的。

imagecolorallocate 用于添加图像中使用的颜色。
请注意,对该函数的第一次调用设置了背景颜色。

//Create a 300*100 image
$img = imagecreate(300, 100);
//Backround and Text color
imagecolorallocate($img, 243, 243, 243);
$textcolor = imagecolorallocate($img, 33, 33, 33);
//Writa the text at given coordinates
imagestring($img, 5, 120, 40, 'onitroad', $textcolor);
//Output the image
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
exit();
使用 PHP 安装和使用 GD 库

GD 库使得在 PHP 中处理图像成为可能,但它可能需要单独安装才能工作。
如果我们使用的是基于 Ubuntu 或者 Debian 的 Linux 发行版,则使用 apt install 相当简单,但我们需要确保安装与 PHP 版本相对应的软件包。

要找出 PHP 版本,请在终端中键入以下内容:

php -v

一般来说,你经常能猜到包名,因为它有一个系统;至少有与 PHP 相关的包。
例如,要安装 GD 库,我们可以尝试以下操作:

sudo apt install php7.4-gd

如果我们使用的是 PHP 7,则可能需要输入:

sudo apt install php70-gd

笔记。
我们也可以搜索正确的包。
这是通过 apt-cache search php7 完成的

安装 GD 包后,我们还应该重新启动服务器:

sudo service apache2 restart
日期:2020-06-02 22:15:44 来源:oir作者:oir