0

次のコードを使用して、古い画像を160x120のサムネイルに取り込みます。問題は、オーバーフローがある場合、背景が常に黒になることです。私はPHPのドキュメントを調べてきましたが、これらの関数にはどのような色のパラメーターも含まれていないようです。どんなアイデアや指針も素晴らしいでしょう!

$original = 'original_image.jpg';
$thumbnail = 'output_thumbnail.jpg';

list($width,$height) = getimagesize($original);
$width_ratio = 160 / $width;
if ($height * $width_ratio <= 120)
{
    $adjusted_width = 160;
    $adjusted_height = $height * $width_ratio;
}
else
{
    $height_ratio = 120 / $height;
    $adjusted_width = $width * $height_ratio;
    $adjusted_height = 120;
}
$image_p = imagecreatetruecolor(160,120);
$image = imagecreatefromjpeg($original);
imagecopyresampled($image_p,$image,ceil((160 - $adjusted_width) / 2),ceil((120 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);
imagejpeg($image_p,$thumbnail,100);

また、私が何を意味するのかわからない場合は、この画像を撮って、元々は白い背景に赤いテキストだったと考えてください

4

3 に答える 3

2

imagecreatetruecolor関数は、黒いキャンバスを作成します。

imagefill関数を使用して白くペイントします...

于 2009-05-18T20:42:20.083 に答える
1

オリジナルを新しいものにコピーする前に、これを追加してください。

$white = ImageColorAllocate($image_p, 255, 255, 255); 
ImageFillToBorder($image_p, 0, 0, $white, $white);

編集:

実は、imagefillについて知りませんでした。。。

$white = imagecolorallocate($image_p, 255, 255, 255); 
imagefill($image_p, 0, 0, $white);
于 2009-05-18T20:44:18.223 に答える
0

imagecreateの代わりにimagecreatetruecolorを使用しないでください、それは解決すると思います

于 2009-07-29T04:33:18.227 に答える