0

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

下载字体的链接: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>";
}
?>
4

2 回答 2

2

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

发生的情况可能是您正在加载的文本包含空格或额外的换行符,然后在您的 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);

?>
于 2013-02-07T23:45:58.673 回答
0

一些情况下我们需要在网页加载的时候动态创建图片,然后脑海中浮现一个问题——我可以在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;

你可以在这里看到完整的类,例如 convert-text-to-image-in-php:

于 2018-11-29T11:01:34.033 回答