使用GD2将png转换为jpg..透明度问题
我有一个带有白色背景和一些透明度的 image.png 。
我尝试将图像转换为 jpg:
$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagejpeg($resource); //> I TRIED WITH QUALITY = 100 TOO
问题是 png 获得透明度,而 jpg 获得相当大的黑色区域。这是 jpg 的样子:
http://img861.imageshack.us/img861/20/ context.jpg
有办法解决这个问题吗?
Edit1:
按照Abiusx的建议,我也尝试了这个:
$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagealphablending($data, false);
imagesavealpha($data, true);
imagejpeg($resource);
但结果是一样的。请注意源 .png 图像为:
(来源:tipradar.com)
感谢帕特里克评论:这里的技巧:GD!将 png 图像转换为 jpeg 并使 alpha 默认为白色而不是黑色
I have an image.png with white background and some trasparceny over it.
I tried this to convert the image into jpg:
$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagejpeg($resource); //> I TRIED WITH QUALITY = 100 TOO
Problem is where the png got the trasparency now the jpg got a pretty huge black zone. This is how jpg looks:
http://img861.imageshack.us/img861/20/context.jpg
Any way to solve the problem?
Edit1:
As suggested by Abiusx I tried this too:
$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagealphablending($data, false);
imagesavealpha($data, true);
imagejpeg($resource);
But the result was the same. Please note The source .png image is:
(source: tipradar.com)
Thanks to Patrick comment: here the trick: GD! Converting a png image to jpeg and making the alpha by default white and not black
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里回答:
Answer here:
GD! Converting a png image to jpeg and making the alpha by default white and not black
这是我用来调整 PNG 大小但保留透明度的函数,如果没有帮助,请告诉我提取您所需的部分:
This is the function I use to resize a PNG but preserve transparency, if it doesnt help, tell me to extract the parts necessary for you: