1

ランダムに生成されたテキスト ファイルを画像に適用するために、次の PHP を使用しようとしています。(現在、ランダムな画像を使用しています。)

<?php
header ("Content-type: image/png");

$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}


//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y,  $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>

ただし、そのコードを実行すると、インポートされた画像が非常にブロックっぽくなります。これが私がテストしている画像です:
http://i.stack.imgur.com/LhNkv.png

実際の表示方法は次のとおりです:
http://i.stack.imgur.com/AAcHZ.png

私の調査によると、「imagecreate」は「パレット」画像を生成します。これは私のエラーと関係があるのではないかと思いましたが、ベース画像が決して歪んでいない例をたくさん見てきました。

あなたの洞察を前もって感謝します。

(うーん。画像は載せられないけどアップは大丈夫?)

更新
コードを次のように変更します。

<?php
header ("Content-type: image/png");

$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}


//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$x = imagesx($im) - $width ;
//$y = imagesy($im) - $height;
//$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
//$textColor = imagecolorallocate ($im, 0, 0,0);
//imagestring ($im, $font, $x, $y,  $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>

画像にテキストが書き込まれていないことを除いて、上記と同じブロックのような効果を生成します (明らかに?)。

4

1 に答える 1

3

アルファブレンディングの問題である可能性があります。画像を保存する前にこれらを追加してみてください:

imagealphablending($im, true); 
imagesavealpha($im, true); 
于 2011-07-15T03:00:09.753 に答える