简体   繁体   中英

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

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";
        } 
} 

I'm working around the same thing just now.

I was testing some images, .gif, .jpeg, .png ... using finfo I found the mime-type you read depends on the constants you use to read the file. More! You read application/octet-stream as mimetype from images! and that info is not wrong. See:

If you use finfo_open() without constants:

<?php
$finfo = finfo_open();
$FileInfo = finfo_file($finfo, $tmp_name);
finfo_close($finfo);

You get the mime type you expect:

If .svg -> HTML document, ASCII text, with very long lines, with no line terminators

If .jpg (from your phone camera) -> JPEG image data, EXIF standard 2.2

If .gif (saved from paint) -> GIF image data, version 89a, wxh

while using a constant like FILEINFO_MIME_TYPE

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE, $mf); // $mf is a magic file
$FileInfo = finfo_file($finfo, $tmp_name);
finfo_close($finfo);

you got a different value:

if .svg -> text/plain

if .jpg (from your phone camera) -> application/octet-stream

if .gif (saved from paint) -> application/octet-stream

So you must test what you read when testing mimetypes. See Fileinfo Predefined constants

Hope it helps

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:

In the case of application/octet-stream, You could get the original file name and check its extension. If its jpg you should be good to go

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