好吧,我用下面的代码采取任何旧形象为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);

此外,如果你还不清楚我是什么意思,拿这个图片并认为它原本只是红色文字在白色背景上

有帮助吗?

解决方案

imagecreatetruecolor功能创建一个黑色帆布。

使用imagefill功能,把它漆成白色...

其他提示

将此你原来的复制到新前:

$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);

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top