为什么我在 PHP 中使用 GD 自动生成的缩略图都是黑色背景?

发布于 2024-07-21 01:45:07 字数 935 浏览 5 评论 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);

另外,如果您不清楚我的意思,请看这张图片并考虑一下它最初只是白底红字

Well I am using the following code to take any old image into a 160x120 thumbnail, the problem is if there is overflow the background is always black. I've been snooping around the PHP docs but none of these functions seem to have any kind of color parameters. Any ideas or pointers would be great!

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

Also if you're unclear what I mean, take this image and consider that it was originally just red text on a white background

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

゛清羽墨安 2024-07-28 01:45:07

imagecreatetruecolor 函数 创建黑色画布。

使用 imagefill 函数将其涂成白色......

The imagecreatetruecolor function creates a black canvas.

Use the imagefill function to paint it white...

骑趴 2024-07-28 01:45:07

在将原始文件复制到新文件之前添加此内容:

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

Add this before you copy the original into the new:

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

EDIT:

Actually, I didn't know about imagefill . . .

$white = imagecolorallocate($image_p, 255, 255, 255); 
imagefill($image_p, 0, 0, $white);
一影成城 2024-07-28 01:45:07

不要使用 imagecreatetruecolor 而不是 imagecreate,我认为这可以解决

dont use imagecreatetruecolor instead imagecreate, I think that would solve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文