【问题标题】:PHP getimagesize() return value and errorsPHP getimagesize() 返回值和错误
【发布时间】:2011-10-21 05:57:30
【问题描述】:

所以根据这个:

http://php.net/manual/en/function.getimagesize.php

如果无法访问文件名图像,或者它不是有效的 图片,getimagesize()会产生E_WARNING级别的错误。上 读取错误,getimagesize()会产生E_NOTICE级别的错误。

但是当我尝试getimagesize('http://www.stackoverflow.com') 时,即使网址不是有效图片,也不会生成错误或警告

当我尝试echo getimagesize('http://www.stackoverflow.com') === FALSE 时,它返回 1,这意味着 getimagesize() 返回布尔值 false...

这是否意味着我在传入 URL 时不必担心错误,这实际上是 getimagesize() 的有效用法?

【问题讨论】:

    标签: php image warnings


    【解决方案1】:
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    

    然后你会看到警告信息。

    【讨论】:

    • @kamikaze_pilot 不要回显它,它不会返回错误消息。只需将代码放在脚本的开头即可。
    • ye 没有错误......在这里运行它:writecodeonline.com/php 也不会产生错误
    【解决方案2】:

    这里是代码

    参考http://php.net/manual/en/function.getimagesize.php来自用户贡献james dot relyea at zifiniti dot com

    <?php
    // Retrieve JPEG width and height without downloading/reading entire image.
    function getjpegsize($img_loc) {
        $handle = fopen($img_loc, "rb") or die("Invalid file stream.");
        $new_block = NULL;
        if(!feof($handle)) {
            $new_block = fread($handle, 32);
            $i = 0;
            if($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
                $i += 4;
                if($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
                    // Read block size and skip ahead to begin cycling through blocks in search of SOF marker
                    $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                    $block_size = hexdec($block_size[1]);
                    while(!feof($handle)) {
                        $i += $block_size;
                        $new_block .= fread($handle, $block_size);
                        if($new_block[$i]=="\xFF") {
                            // New block detected, check for SOF marker
                            $sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
                            if(in_array($new_block[$i+1], $sof_marker)) {
                                // SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.
                                $size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
                                $unpacked = unpack("H*", $size_data);
                                $unpacked = $unpacked[1];
                                $height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
                                $width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
                                return array($width, $height);
                            } else {
                                // Skip block marker and read block size
                                $i += 2;
                                $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                                $block_size = hexdec($block_size[1]);
                            }
                        } else {
                            return FALSE;
                        }
                    }
                }
            }
        }
        return FALSE;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2013-07-10
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      相关资源
      最近更新 更多