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

不要使用 imagecreatetruecolor 代替 imagecreate,我认为这会解决

于 2009-07-29T04:33:18.227 回答