为什么我在 PHP 中使用 GD 自动生成的缩略图都是黑色背景?
好吧,我使用以下代码将任何旧图像转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
imagecreatetruecolor 函数 创建黑色画布。
使用 imagefill 函数将其涂成白色......
The imagecreatetruecolor function creates a black canvas.
Use the imagefill function to paint it white...
在将原始文件复制到新文件之前添加此内容:
编辑:
实际上,我不知道 imagefill 。 。 。
Add this before you copy the original into the new:
EDIT:
Actually, I didn't know about imagefill . . .
不要使用 imagecreatetruecolor 而不是 imagecreate,我认为这可以解决
dont use imagecreatetruecolor instead imagecreate, I think that would solve