PHP:如何检测 jpeg 是否全黑(无图像)?
我建立了一个社区网站,用户可以上传他们的照片,他们可以裁剪缩略图,就像 Facebook 一样。
但由于某种原因,其中一些会生成空白(实际上是黑色 jpeg 缩略图)。
我尝试了不同的解决方案,但听起来这种情况发生在大图像上,或者可能发生在未启用 JS 的计算机上?我不知道...
底线是,由于使用此功能的用户数量非常少,我正在考虑创建一个补丁:检测用户何时生成空白 jpeg。然后我就可以警告他们。
你知道该怎么做吗?
I built a community website where the users upload their photo and they can crop the thumbnail, ala Facebook to be clear.
But for some reason some of them generate a blank (actually black jpeg thumbnail).
I tried different solutions but it sounds like this is happening with big images or maybe in computers where JS is not enabled? I don't know...
Bottom line, since the number of users with this is very tiny, I was thinking of creating a patch: detect when the user generates a blank jpeg.Then I would be able to warn them.
Do you know how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将其大小调整为 1x1 PX 图像。然后检查该像素是否为黑色。并不完美,但如果原图中有大量非黑色,那么 1px 将不会是黑色。好处是速度会很快。
Resize it to a 1x1 PX image. Then check if the one pixel is black. Not perfect, but if there is a significant amount of non black in the original, that 1px will not be black. The benefit is that it will be fast.
我认为没有比使用
imagecolorat()
.至于为什么会出现这种情况,可能是因为CMYK JPG 文件。不确定 - 你必须尝试一下。如果这是原因,您可以使用
getimagesize()
检测 CMYK 及其频道
信息。I think there is no faster way than walking though each pixel using
imagecolorat()
.As to why this happens, it could be because of CMYK JPG files that GD can't digest. Not sure - you would have to try out. If that is the reason, you could detect CMYK using
getimagesize()
and itschannels
info.我喜欢这种随机算法,
即使 80% 的图像是黑色,它也有 99% 的成功率。您还可以增加循环以获得更好的百分比。
I like this random algoritm,
Even if 80% of the image is black it has 99% to succeed. you can also increase the loop for better percentage.
假设它是一个有效的黑色图像,您可以:
长方式 - 逐像素扫描黑色
短方式(如果始终是相同的黑色图像) - 将文件逐字节(或 md5 哈希)与示例黑色图像进行比较
Assuming its a valid black image you could:
long way - scan pixel by pixel for the colour black
short way (if its the same black image always) - compare file byte by byte (or md5 hash) to a sample black image
这是我的代码:
我用它来删除黑色拇指。
Here is my code:
I'm using it to remove black thumbs.
如果全黑,则文件大小会很小,因为它是压缩的。我知道 JPG 使用感知而不是 RLE,但它仍然会更小。例如,为什么不禁止 20k 以下的 JPG?举几个例子,比较大小,设置阈值。
If it's all black, the file size will be low, as it's compressed. I know JPG uses perceptual rather than RLE, but it will still be smaller. Why not disallow JPGs under 20k for example? Make a few examples, compare the sizes, set your thresholds.