duanhe0817825 2015-07-10 17:42 采纳率: 100%
浏览 419

获取远程文件类型以取代PHP getimagesize()的更好方法是什么?

I am using the PHP getimagesize() function to get the File type of an image on a remote server.

Looking at the PHP docs for getimagesize() here http://php.net/manual/en/function.getimagesize.php in the comments it says that this function first downloads the whole file first and then gets the information about the file.

The comment also supplied an alternative function to replace getimagesize() which claims to only download the first bytes until it gets the info it needs which is faster than first downloading the whole file.

The issue with this new function though is that it is named getJpegSize($img_loc) and claims to work for JPEG files. Since my users will obviously not be restricted to only JPG files, I am curious if there is a better way to do this that would be as fast but work for other image types as well?

The code for the new function is below. Also direct link to PHP Docs page where the comment and code is located: http://php.net/manual/en/function.getimagesize.php#88793

// Retrieve JPEG width and height without downloading/reading entire image.
// From http://php.net/manual/en/function.getimagesize.php
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;
}

展开全部

  • 写回答

1条回答 默认 最新

  • dongxing1965 2015-07-10 19:30
    关注

    Each image type has a different way to store its size. Since you don't have any restriction on the type, you should use getimagesize() and avoid adding too much complexity (unless you really need it). It will simplify your code and help with maintenance (if there is a bug in the function you pasted, good luck finding it!)

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部