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

3

13

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";
        } 
} 
Quatrefoil answered 14/3, 2012 at 17:45 Comment(0)
Z
2

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:

Zorazorah answered 14/3, 2012 at 17:47 Comment(3)
Yes, because the GD library does not support to open (, resize and output) these formats. Or the mime-type you get is mainly informative. You must use some other image library that is able to deal with the formats.Zorazorah
sorry, I am not expert in image processing .. can you tell me about 'other image library is able to deal with the formats'Quatrefoil
Sorry, I can't tell you. If you like to find out more, you should add the actual value(s) of $file_dimensions[2] (next to the mime-type) to your question. As written, the mime-type is really general, so more interesting is the image type.Zorazorah
P
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

Porche answered 17/6, 2018 at 6:41 Comment(0)
B
-1

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

Blues answered 18/2, 2015 at 13:39 Comment(2)
Whosoever downvoted, should also add his viewpoint. This perfectly worked for me.Blues
I'll down vote it, and I'll tell you why. There's a setting for php that determines the maximum post size setting, but less than the total amount of space allowed for php to use in memory. An example: post_max_size='2m' and memory_limit="128M" would result in the image being served via stream, instead of a simple image/jpeg. That's because it's serving the image in chunks until the entire image is uploaded.Minestrone

© 2022 - 2024 — McMap. All rights reserved.