なぜPHPでGDを持つすべての私の自動生成のサムネイルは黒の背景を持っていますか?
-
22-08-2019 - |
質問
私は160×120のサムネイルに古い画像を撮影するには、次のコードを使用しています。
さて、問題はオーバーフローがある場合、背景は常に黒です。私は、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);
あなたは私が何を意味するかは不明だ場合は、また、この画像を撮影するそれは元々ただ赤い文字が白の背景にあったと考える。
解決
ブラックキャンバスを作成する
...白いそれをペイントするimagefill関数を使用
他のヒント
あなたは新しいにオリジナルをコピーする前に
このを追加します:
$white = ImageColorAllocate($image_p, 255, 255, 255);
ImageFillToBorder($image_p, 0, 0, $white, $white);
EDITます:
実は、私はimagefill知りませんでした。 。ます。
$white = imagecolorallocate($image_p, 255, 255, 255);
imagefill($image_p, 0, 0, $white);
の代わりに、私はそれが解決するだろうと思う、imagecreate imagecreatetruecolorを使用していけない。
所属していません StackOverflow