how handling image's mime type "application/octet-stream"?

user504363 picture user504363 · Mar 14, 2012 · Viewed 31.5k times · Source

I have a function to make thumbnail of a url image on fly! I always pass to this functions images with type jpg, but the problem appears when I pass an image with ".jpg" extension. but when i try to get its mime type, i found that's "application/octet-stream" .. in this php page, this mime type refers to one of

IMAGETYPE_JPC,IMAGETYPE_JPX,IMAGETYPE_JB2

what I need to modify my function to handle this mime type ??

notice ^^^^^^

function thumb($path,$width,$height) // $path => image url
{
        $file_dimensions = getimagesize($path);
        $file_type = image_type_to_mime_type($file_dimensions[2]);
        list($Cwidth, $Cheight) = getimagesize($path);
        if ($file_type=='image/jpeg'||$file_type=='image/pjpeg'){
            // Load
        $thumb = imagecreatetruecolor($width, $height);
        $source = imagecreatefromjpeg($path);
        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $Cwidth, $Cheight);
        header('Content-Type: image/jpeg');
        imagejpeg($thumb);
        }
        else if ($file_type=='application/octet-stream')
        {
           // ^^^^^ what I should write here
        }
        else
        {
            echo "Not supported type";
        } 
} 

Answer

hakre picture hakre · Mar 14, 2012

We can't tell you because application/octet-stream is a sort of general-type-of-binary-file mime-type. It can be everything. You can try with imagecreatefromstring on the files binary content. But keep fingers crossed ;).

The actual issue here is that getimagesize is independent to the GD library you use for resizing the image. So it provides infos about files GD itself is not able to deal with. So you can just output some sort of "unsupported image type" until you find some additional library that is able to deal with the specific mime- or better saying image-type.

See as well: