【问题标题】:Converting Text to Image using php script使用 php 脚本将文本转换为图像
【发布时间】:2013-02-07 23:19:28
【问题描述】:

我正在处理非英语语言,我想将该文本显示为图像,以便即使在不支持该语言的平台上也能支持它。问题是我能够以文本格式正确显示文本,但是在图像方面。它根本不显示图像。

字体下载链接:http://qfs.mobi/f394372 或在 Google 中搜索 Surya.ttf

这是我的代码:

文字转图片代码:

<?php
// Set the content-type
//mb_internal_encoding("UTF-8");
header('Content-Type: image/png');
require_once('seven.php');
// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = getData();//'કેમ છો ?';
//echo $text;
// Replace path by your own font path
$font = 'Surya.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

getData() 的代码:

<?php
function getData(){
$url = "http://www.sandesh.com/article.aspx?newsid=119068";
//$url = "http://www.sandesh.com/article.aspx?newsid=115627";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$DOM = new DOMDocument;
$output = mb_convert_encoding($output, 'HTML-ENTITIES', "UTF-8");
@$DOM->loadHTML($output);
$items = $DOM -> getElementById('lblNews');

echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body><span>". $items -> nodeValue ."</span". "<br/></body></html>";
}
?>

【问题讨论】:

  • 字体是否支持非英文字符?
  • 是的,字体支持非英文字符。看看这个像 $text = getData();//'કેમ છો ?';而不是调用 getData 你可以硬编码文本并检查字体的工作原理。
  • 我已经用下载字体的链接更新了问题。以备不时之需。

标签: php html image


【解决方案1】:

您的代码正在运行(您忘记了结束跨度并且该函数没有返回任何内容,但我想这是由于调试所致)。

发生的情况可能是您正在加载的文本包含空格或额外的换行符,然后在您的 PNG 之外呈现。

尝试使用更大的 PNG 或尝试使用 trim() 修剪文本。

function getData()
{
    ...
    return trim($items->nodeValue);
}

测试代码(工作)

...或者至少返回一张我看不懂的图片:-)

<?php

        function getData(){
                $url = "http://www.sandesh.com/article.aspx?newsid=119068";
                //$url = "http://www.sandesh.com/article.aspx?newsid=115627";
                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
                $output = curl_exec($curl);
                curl_close($curl);
                $DOM = new DOMDocument;
                $output = mb_convert_encoding($output, 'HTML-ENTITIES', "UTF-8");
                @$DOM->loadHTML($output);
                $items = $DOM -> getElementById('lblNews');

                return trim($items -> nodeValue);
        }

// Set the content-type
//mb_internal_encoding("UTF-8");
// require_once('seven.php');
// Create the image
$im = imagecreatetruecolor(400, 400);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, ImageSX($im), ImageSY($im), $white);

// The text to draw
$text = getData();//'કેમ છો ?';
//echo $text;
// Replace path by your own font path
$font = 'Surya.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>

【讨论】:

  • 感谢您的回答,但这不是问题所在。我已经完成了所有的关闭跨度,我确实尝试使用更大的 PNG 进行修剪,但仍然没有运气。
  • 我现在已经尝试过使用相同的代码,它可以工作...更新答案。
【解决方案2】:

在某些情况下我们需要在网页加载时动态创建图像,然后脑海中浮现一个问题——我可以在 PHP 中将文本转换为图像吗?答案是肯定的!为什么不。要进行此活动,您只需执行几个步骤。这些步骤如下:

检查您的 GD 库扩展是否在您当前的 PHP 版本中启用。 如果它没有启用,只需取消注释即可。 在您的项目中包含 phpTextToImage 类。 创建这个类的一个对象。 使用您的文本调用创建图像功能。 使用您的文件名和位置调用保存函数。

function createImage($text, $textColor = '', $backgroundColor = '', $fontSize = 22, $imgWidth = 600, $imgHeight = 300) {

    //text font path
    $font = 'font/Pacifico-Regular.ttf';

    //create the image
    $this->image = imagecreatetruecolor($imgWidth, $imgHeight);

    $colorCode = array('#ffffff','#db3236', '#f4c20d', '#3cba54', '#4c53cc', '#56aad8', '#61c4a8');

    if ($backgroundColor == '') {
        /* select random color */
        $backgroundColor = $this->hexToRGB($colorCode[rand(0, count($colorCode) - 1)]);
    } else {
        /* select background color as provided */
        $backgroundColor = $this->hexToRGB($backgroundColor);
    }

    if ($textColor == '') {
        /* select random color */
        $textColor = $this->hexToRGB($colorCode[rand(0, count($colorCode) - 1)]);
    } else {
        /* select background color as provided */
        $textColor = $this->hexToRGB($colorCode[rand(0, count($textColor) - 1)]);
    }

    $textColor = imagecolorallocate($this->image, $textColor['r'], $textColor['g'], $textColor['b']);
    $backgroundColor = imagecolorallocate($this->image, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);

    imagefilledrectangle($this->image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backgroundColor);

    //break lines
    $splitText = explode("\\n", $text);
    $lines = count($splitText);
    $angle = 0;

    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->image) - $textWidth) / 2;
        $y = ((imagesy($this->image) + $textHeight) / 2) - ($lines - 2) * $textHeight;
        $lines = $lines - 1;
        //add the text
        imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $txt);
    }
    return true;
}

现在,在您想要动态创建文本图像的位置创建项目文件,然后只包含类并创建一个像这样的对象:

//include phpTextToImage class
require_once 'phpTextToImage.php';

//create img object
$img = new phpTextToImage;

you can see full class here with example convert-text-to-image-in-php:

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多