检查图像是否为白色;如果是这样,请使其透明
我有一个图像,如果它是全白色的,我想使它透明。我已经有用于 GD 的以下代码来获取图像的一部分:
$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreate(150, 150);
imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
header('Content-type: image/png');
imagepng($destp);
但是我如何才能首先检查图像是否完全白色,如果是,则将其更改为透明,然后将其应用到 $destp?
I have an image, and I want to make it transparent if it's completely white. I have the following code already for GD for getting a part of the image:
$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreate(150, 150);
imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
header('Content-type: image/png');
imagepng($destp);
But how can I have it first check if the image is completely white, if so change it to transparent, and apply that to $destp
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
根据重新阅读问题和下面的讨论,我相信这就是您正在寻找的内容:
如果原始图像的 8x8 切片全是白色,则会生成空白图像,否则会生成空白图像将 8x8 切片重新采样为 150x150。
原文:
我以前从未使用过 PHP GD 做过任何事情,并且认为这将是一个有趣的挑战。这就是我最终实现这一目标的方法:
它允许我将其转变为:
变成这个(很难看到alpha 在这里,但你可以打开它来查看):
EDIT:
Based on re-reading the question, and the discussion below, I believe this is what you're looking for:
This produces a blank image if the original image's 8x8 slice is all white, otherwise it resamples the 8x8 slice to 150x150.
Original:
I haven't ever done anything with PHP GD before, and thought it would be a fun challenge. Here's how I ended up making this happen:
It allowed me to turn this:
Into this (hard to see the alpha here, but you can open it to see):
一个简单直接的解决方案是使用 imagecolorat 并迭代所有像素png 的,如果全部都是白色,请将其更改为透明。
希望这有帮助。
A simple strightforward solution would be to use imagecolorat and iterate through all the pixels of the png and if all are white change it to transparent.
Hope this helps.
快速浏览完 GD 手册页后,我认为以下内容应该适合您(无论如何,它在我的测试服务器上确实如此):
参考文献:
imagecolorat()
。imagecolorstotal()
。After a quick look through the GD manual pages, I think that the following should work for you (it did on my test server, anyway):
References:
imagecolorat()
.imagecolorstotal()
.