2013-02-07 102 views
0

我正在處理非英語語言,我想將該文本顯示爲圖像,以便即使在不支持此語言的平臺中也能支持它。問題是我能夠以文本格式正確顯示文本,但是涉及到圖像。它根本不顯示圖像。使用php腳本將文本轉換爲圖像

鏈接下載的字體:http://qfs.mobi/f394372或只是搜索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

做字體的支持非英語字符? – Sergiu

+0

是的,該字體支持非英文字符。看看這個像$ text = getData(); //'કેમછો?';而不是調用getData,你可以硬編碼文本並檢查字體的工作方式。 – VendettaDroid

+0

我已更新與鏈接下載字體的問題。以防萬一你需要它。 – VendettaDroid

回答

2

您的代碼正在工作(您忘記了關閉範圍和f聯合不返回任何東西,但我想這是由於調試)。

發生了什麼可能是您正在加載的文本包含空格或多餘的換行符,然後在您的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); 

?> 
+0

感謝您的答案,但這不是問題。我已經完成了所有的關閉時間,我嘗試使用更大的PNG進行修剪,但仍然沒有運氣。 – VendettaDroid

+0

我現在已經嘗試使用相同的代碼,它可以工作...更新答案。 – LSerni