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

社会主义新天地 提交于 2019-12-10 14:53:59

问题


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

回答1:


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:

  • Android image file sent to php upload image file is application/octet-stream type and not image/jpeg?
  • Is it important to verify that the uploaded file is an actual image file?



回答2:


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, w x h

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




回答3:


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



来源:https://stackoverflow.com/questions/9707204/how-handling-images-mime-type-application-octet-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!