简体   繁体   中英

Better way to get remote file type to replace PHP's 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;
}

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!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM