PHP:使用GD / Imagemagick确定通过Curl下载的视觉损坏图像(但仍然有效) [英] PHP: Determine Visually Corrupted Images (yet valid) downloaded via Curl with GD/Imagemagick

查看:93
本文介绍了PHP:使用GD / Imagemagick确定通过Curl下载的视觉损坏图像(但仍然有效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Curl via Proxies用我开发的刮刀下载图像。

I'm using Curl via Proxies to download images with a scraper I have developed.

不幸的是,它得到的奇怪图像看起来像这些和最后一个完全空白:/

Unfortunately, it gets the odd image which looks like these and the last one is completely blank :/





  • 当我通过imagemagick(使用识别)测试图像时,它告诉我它们是有效的图像。

  • 当我通过exif_imagetype()和imagecreatefromjpeg()再次测试图像,这两个函数告诉我图像是有效的。

有没有人有确定图像是否具有大部分灰度或者是完全空白/白色并且这些确实是损坏的图像的方法?

Does anyone have a way to determine if the image has majority of greyness or is completely blank/white and these are indeed corrupted images?

我已经用o做了很多检查这里有问题,但我对其他解决方案没有太多运气。所以请注意建议这是重复。

I have done a lot of checking with other questions on here, but I haven't had much luck with other solutions. So please take care in suggesting this is a duplicate.

谢谢

在了解了imgcolorat后,我进行了搜索并偶然发现了一些代码。我想出了这个:

After knowing about imgcolorat, I did a search and stumbled on some code. I came up with this:

<?php

$file = dirname(__FILE__) . "/images/1.jpg";

$img = imagecreatefromjpeg($file);

$imagew = imagesx($img);
$imageh = imagesy($img);
$xy = array();

$last_height = $imageh - 5;

$foo = array();

$x = 0;
$y = 0;
for ($x = 0; $x <= $imagew; $x++) 
{
    for ($y = $last_height;$y <= $imageh; $y++ ) 
    {
        $rgb = @imagecolorat($img, $x, $y);

        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        if ($r != 0)
        {
            $foo[] = $r;
        }
    }
}

$bar = array_count_values($foo);

$gray = (isset($bar['127']) ? $bar['127'] : 0) + (isset($bar['128']) ? $bar['128'] : 0) + (isset($bar['129']) ? $bar['129'] : 0);
$total = count($foo);
$other = $total - $gray;

if ($gray > $other)
{
    echo "image corrupted \n";
}
else
{
    echo "image not corrupted \n";
}
?>

有人看到一些潜在的陷阱吗?我想到了获得图像的最后几行,然后将r 127,128,129(灰色)的总和与其他颜色的总和进行比较。如果灰色大于其他颜色,则图像肯定会损坏。

Anyone see some potential pitfalls with this? I thought about getting the last few rows of the image and then comparing the total of r 127,128,129 (which are gray) against the total of other colours. If gray is greater than the other colours then the image is surely corrupted.

欢迎提出意见! :)

推荐答案

如果它返回的图像是有效文件,那么我建议运行两次刮擦(即下载)它两次并检查它们是否相同)。

If the image it is returning is a valid file, then I would recommend running the scrape twice (ie. download it twice and check to see if they are the same).

另一种选择是检查图像的最后几个像素(即右下角)到看看它们是否与那种灰色完全匹配。如果他们这样做,那么重新下载。 (显然这种方法会失败,如果你下载一个实际上应该是那个角落灰色的图像,那个确切的颜色......但如果你检查几个最后一个像素,它应该减少到达可接受水平的几率)。

Another option would be to check the last few pixels of the image (ie. bottom-right corner) to see if they match that color of grey exactly. If they do, then redownload. (obviously this approach fails if you download an image that is actually supposed to be grey in that corner, in that exact colour...but if you check several of the last pixels it should reduce the chance of that to an acceptable level).

这篇关于PHP:使用GD / Imagemagick确定通过Curl下载的视觉损坏图像(但仍然有效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆