PHP : binary image data, checking the image type

前端 未结 7 1592
刺人心
刺人心 2020-11-30 02:44

I have some images in bin, I want to check the header to check the format (jpg, png, etc)

I don\'t want to use temp files! I have a solution using TEMP FILES.

7条回答
  •  没有蜡笔的小新
    2020-11-30 03:25

    Here's an implementation of the function as described by Wrikken

    function getImgType($filename) {
        $handle = @fopen($filename, 'r');
        if (!$handle)
            throw new Exception('File Open Error');
    
        $types = array('jpeg' => "\xFF\xD8\xFF", 'gif' => 'GIF', 'png' => "\x89\x50\x4e\x47\x0d\x0a", 'bmp' => 'BM', 'psd' => '8BPS', 'swf' => 'FWS');
        $bytes = fgets($handle, 8);
        $found = 'other';
    
        foreach ($types as $type => $header) {
            if (strpos($bytes, $header) === 0) {
                $found = $type;
                break;
            }
        }
        fclose($handle);
        return $found;
    }
    

提交回复
热议问题