【问题标题】:How to determine the size of an image without downloading it (in full)?如何在不下载(完整)的情况下确定图像的大小?
【发布时间】:2011-11-19 03:42:57
【问题描述】:

我正在寻找远程托管 JPG 的尺寸、宽度和高度。我已经看到如何通过下载完整图像来做到这一点。

但是,如果我可以通过仅下载足够的内容来获取此信息来做到这一点,那将是理想的。

典型的图像大小为 200K,一开始只需阅读几 K可能对于 JPG 来说可能就足够了:

curl_setopt($ch, CURLOPT_RANGE, "0-4096");

作为参考,我遵循了这些答案/cmets 但无济于事(尽管它们可能在正确的轨道上):

有没有人能够将这些碎片拼凑在一起(有或没有 ImageMagick)?

【问题讨论】:

  • 你试过什么?您对JPEG文件格式的理解是什么?看看你可以从en.wikipedia.org/wiki/Exchangeable_image_file_format#Example 拼凑什么
  • 我尝试读取前 4096 个字节,但未能找到针对它运行 Imagemagick 'identify' 或 'grep -n $\xc0' (请参阅参考问题/cmets)。

标签: php imagemagick


【解决方案1】:

像这样使用 ImageMagick 的 ping 功能怎么样:

identify -ping -format "%wx%h" http://www.google.com/logos/giroux1.jpg
482x142

【讨论】:

  • 哇,从来不知道-ping!测试表明它只是稍微快一点,但省去了下载临时文件的步骤。
【解决方案2】:

根据Getting Image size of JPEG from its binary,图像的大小未存储在文件中的精确位置。根据扩展名,您可以在实际大小之前在文件中包含一大堆数据,例如拇指邮件。

以下 C++ 代码应该是您自己的 PHP 实现的良好开端:http://www.64lines.com/jpeg-width-height。您可以通过访问包含数据的字符串(如数组)轻松调整此代码:myString{$i} 而不是 data[i]

您应该加载相对较大的数据块并对其进行解析,然后在必要时加载更多数据。或者你可以逐块加载文件,但是大量连接带来的开销使不加载整个文件的目的失效。

无论如何,我不确定这些好处是否能证明您在这方面花费的时间是合理的。现在 200k 不算什么。

【讨论】:

  • 我会检查的,但对我来说可能太多了。 200K 并不是很多,真的 - 但尽管我目前的方法是完全下载它们,但它每天会运行大约 500+mb。
【解决方案3】:

上面多诺霍回答的改进版本。如here 所述,此文件检索 png 和 gif 文件的较小字节范围。

function remoteImage($url){

    $filename = parse_url($url);
    $pi = pathinfo($filename['path'],PATHINFO_EXTENSION);

    switch ($pi) {
    case "jpg" :
        $range = "0-15000";
        break;
    case "jpeg" :
        $range = "0-10000";
        break;
    case "png":
        $range = "0-100";
        break;
    case "gif":
        $range = "0-10";
        break;
    default:
        $range = "0-15000";
        break;
    }

    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_RANGE, $range);

    $fn = "partial2.png";
    $raw = curl_exec($ch);
    $result = array();

    if(file_exists($fn)){
        unlink($fn);
    }

    if ($raw !== false) {

        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($status == 200 || $status == 206) {

            $result["w"] = 0;
            $result["h"] = 0;

            $fp = fopen($fn, 'x');
            fwrite($fp, $raw);
            fclose($fp);

            $size = getImageSize($fn);

            if ($size===false) {
                //  Cannot get file size information
            } else {
                //  Return width and height
                list($result["w"], $result["h"]) = $size;

            }

        }
    }

    curl_close ($ch);
    return $result;
}

【讨论】:

    【解决方案4】:

    我设法回答了我自己的问题,并且我已经包含了 PHP 代码 sn-p。

    唯一的缺点(至少对我而言)是,这会在使用getImageSize 读取尺寸之前将部分图像下载写入文件系统。

    对我来说,10240 字节是检查 200 到 400K 大小的 jpg 图像的安全限制。

    function remoteImage($url){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        curl_setopt($ch, CURLOPT_RANGE, "0-10240");
    
        $fn = "partial.jpg";
        $raw = curl_exec($ch);
        $result = array();
    
        if(file_exists($fn)){
            unlink($fn);
        }
    
        if ($raw !== false) {
    
            $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
            if ($status == 200 || $status == 206) {
    
                $result["w"] = 0;
                $result["h"] = 0;
    
                $fp = fopen($fn, 'x');
                fwrite($fp, $raw);
                fclose($fp);
    
                $size = getImageSize($fn);
    
                if ($size===false) {
                //  Cannot get file size information
                } else {
                //  Return width and height
                    list($result["w"], $result["h"]) = $size;
                }
    
            }
        }
    
        curl_close ($ch);
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多