我正在处理非英语语言,我希望将该文本显示为图像,以便即使在不支持该语言的平台上也能支持它。问题是我能够以文本格式正确显示文本,但是当涉及图像时。它根本不显示图像。
链接下载字体: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>";
}
?>
答案 0 :(得分: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);
?>
答案 1 :(得分: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;
}
现在,在要动态创建文本图像的位置创建项目文件,只需包含class并创建如下对象:
//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: