在许多情况下,我们需要在我们的 Web 应用程序中动态创建动态图像并在图像上写入文本。
如果我们有这样的需求,请不要担心,我们将提供一个简单的 PHP 脚本来将文本转换为图像并将其保存为 PNG 或者 JPG 格式。
在本教程中,我们将向我们展示使用 PHP 从文本创建图像的最简单方法。
为了控制文本到图像的创建过程,将创建一个 PHP 类。
通过使用这个 PHP 类,我们可以轻松创建图像并向图像添加多行文本字符串。
GD 库将用于在我们的 PHP 脚本中创建和操作图像文件。
因此,在开始之前,请确保服务器上安装了 PHP GD。
在 PHP 中将文本字符串转换为图像
包含“TextToImage.php”文件(TextToImage 类)并创建一个对象。
调用“createImage()”函数并提供文本字符串。
如果要向图像添加多行,请在新行前添加“\n”。
//include TextToImage class require_once 'TextToImage.php'; //create img object $img = new TextToImage; //create image from text $text = 'Welcome to onitroad.\nThe World of Programming.'; $img->createImage($text);
要显示图像,请使用“showImage()”函数。
//display image $img->showImage();
要将图像文件保存为 PNG 或者 JPG 格式,请使用“saveAsPng()”或者“saveAsJpg()”函数。
指定保存图片的文件名和位置,否则图片会以默认名称保存到脚本所在目录。
//以 png 格式保存 $img->saveAsPng('codex-text-to-image','images/'); //以 jpg 格式保存 $img->saveAsJpg('codex-text-to-image','images/');
TextToImage 类
TextToImage PHP 类有助于生成图像并向图像添加文本。
TextToImage 类包含 4 个函数,这些函数将执行以下操作。
- 'createImage()' - 从文本创建图像。需要提供要写入图像的文本字符串。此外,我们可以指定文本字体大小、图像宽度和图像高度。
- 'showImage()' - 返回由 createImage() 函数创建的图像。
- 'saveAsPng()' - 将图像文件保存为 .png 格式。我们可以指定文件名和保存图像的位置。
- 'saveAsJpg()' - 将图像文件保存为 .jpg 格式。我们可以指定文件名和保存图像的位置。
<?php class TextToImage { private $img; /** * Create image from text * @param string text to convert into image * @param int font size of text * @param int width of the image * @param int height of the image */ function createImage($text, $fontSize = 20, $imgWidth = 400, $imgHeight = 80){ //text font path $font = 'fonts/the_unseen.ttf'; //create the image $this->img = imagecreatetruecolor($imgWidth, $imgHeight); //create some colors $white = imagecolorallocate($this->img, 255, 255, 255); $grey = imagecolorallocate($this->img, 128, 128, 128); $black = imagecolorallocate($this->img, 0, 0, 0); imagefilledrectangle($this->img, 0, 0, $imgWidth - 1, $imgHeight - 1, $white); //break lines $splitText = explode ( "\n" , $text ); $lines = count($splitText); foreach($splitText as $txt){ $textBox = imagettfbbox($fontSize,$angle,$font,$txt); $textWidth = abs(max($textBox[2], $textBox[4])); $textHeight = abs(max($textBox[5], $textBox[7])); $x = (imagesx($this->img) - $textWidth)/2; $y = ((imagesy($this->img) + $textHeight)/2)-($lines-2)*$textHeight; $lines = $lines-1; //add some shadow to the text imagettftext($this->img, $fontSize, $angle, $x, $y, $grey, $font, $txt); //add the text imagettftext($this->img, $fontSize, $angle, $x, $y, $black, $font, $txt); } return true; } /** * Display image */ function showImage(){ header('Content-Type: image/png'); return imagepng($this->img); } /** * Save image as png format * @param string file name to save * @param string location to save image file */ function saveAsPng($fileName = 'text-image', $location = ''){ $fileName = $fileName.".png"; $fileName = !empty($location)?$location.$fileName:$fileName; return imagepng($this->img, $fileName); } /** * Save image as jpg format * @param string file name to save * @param string location to save image file */ function saveAsJpg($fileName = 'text-image', $location = ''){ $fileName = $fileName.".jpg"; $fileName = !empty($location)?$location.$fileName:$fileName; return imagejpeg($this->img, $fileName); } }
日期:2020-06-02 22:15:27 来源:oir作者:oir