imagecreatefrompng() 制作黑色背景而不是透明背景?
我使用 PHP 和 GD 库制作缩略图,但我的代码将 png 透明度转换为纯黑色,有解决方案来改进我的代码吗?
这是我的 php 缩略图制作器代码:
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
谢谢
I make thumbnails using PHP and GD library but my code turn png transparency into a solid black color, Is there a solution to improve my code?
this is my php thumbnail maker code:
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 imagecreatetruecolor() 之后:
After imagecreatetruecolor():
操作顺序很重要。对于 .gif 图像,我发现我需要先复制调整大小的图像,然后将黑色背景指定为透明。对于 PNG,我发现下面的代码调整了图像大小并保留了透明背景。
另外,这段代码对我有用......
The order of operations is important. for .gif images i found that i needed to copy resized image first, then assign the black background as transparent. for PNGs I found the code below resized images and kept the transparency backgrounds.
also, this code worked for me...
如果正确答案的代码不起作用,请尝试以下操作:
if the code of the correct answer don't work try this :
只需添加这一行:
之前
Just add this line :
before
有时,如果 .jpg 图像内部有小错误(您看不到),所有透明像素都会变成黑色。尝试使用:
Sometimes if the .jpg image got small errors inside (you cannot see that), all transparent pixels turn to black color.Try to use:
这是我的总测试代码。这对我有用
Here is my total test code. It works for me
将 RGB 更改为 255,它会给您透明图像,而不是黑色。
change rgb to 255, it will give you transparent image, rather then black.
上面的一些方法使图像的黑色部分变成白色,而另一些则根本不起作用。然而,这对我有用 https://github.com/claviska/SimpleImage/issues/28
Some of the above made the black parts of the image turn white, while some didn't work at all. This, however, worked for me https://github.com/claviska/SimpleImage/issues/28