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 に答える