简体   繁体   English

png图像的透明度问题

[英]Transparency issue with png images

I 've got this image: 我有这张图片: 带有白色背景的原始图像

i use php5 and gd2 library to achieve a transparent background, but the result is not what i have expected(you need a dark background to see the difference): 我使用php5和gd2库实现透明背景,但是结果不是我所期望的(您需要深色背景才能看到不同之处): 透明背景

and here is my code: 这是我的代码:

$size = getimagesize($img_url);
$img = imagecreatefrompng($img_url);
$im_tc = imagecreatetruecolor($size[0],$size[1]);
imagecopyresampled($im_tc,$img,0,0,0,0,$size[0],$size[1],$size[0],$size[1]);
imagedestroy($img);
$white  = imagecolorallocatealpha($im_tc, 255, 255, 255,127);
imagecolortransparent($im_tc, $white);
imagepng($im_tc,'image.png');

Any help would be appreciated! 任何帮助,将不胜感激! :) :)

Your original image doesn't have a transparent background, but you're trying to create one by changing all pure-white pixels to transparent. 您的原始图像没有透明背景,但是您尝试通过将所有纯白色像素更改为透明来创建背景。 As the text has a shadow/glow around it, those pixels aren't quite white, and won't become transparent. 由于文本周围有阴影/发光,因此这些像素不是很白,也不会变得透明。 You have a couple options here: 您在这里有几个选择:

  • Use an image editor (GIMP, Photoshop, etc.) and their fine-grained selection tools to perform a smart selection of the "background", then remove that. 使用图像编辑器(GIMP,Photoshop等)及其细粒度的选择工具对“背景”进行智能选择,然后将其删除。 Photoshop, at least, has extremely useful options available on its magic wand tool to choose threshold, feathering, and more. 至少,Photoshop在其魔术棒工具上具有极其有用的选项,可以用来选择阈值,羽化等等。
  • Pick more colours to make "transparent." 选择更多颜色以使“透明”。 Instead of just white, choose all of the shades of gray, minus the exact colour that ".gr" is set in. This should make all of the shadowed parts of the image transparent as well, although the edges may be less than perfect (ie: not feathered, slightly jagged.) 不仅要选择白色,还应选择所有灰色阴影,减去设置为“ .gr”的确切颜色。这也应使图像的所有阴影部分都透明,尽管边缘可能不完美(即:没有羽毛,略呈锯齿。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM