PHP create image from application/octet stream

前端 未结 3 959
再見小時候
再見小時候 2020-12-17 04:22

My application is having images sent to it from mobile devices with the content-type \"application/octet-stream\".

I need to process these images using the GD librar

相关标签:
3条回答
  • 2020-12-17 05:02

    I found this in the codeigniter forum a way to change the mime and it works, I suppose you can use for other frameworks as well, this is the link and this the code:

    //if mime type is application/octet-stream (psp gives jpegs that type) try to find a   more specific mime type
    
    $mimetype = strtolower(preg_replace("/^(.+?);.*$/", "\\1", $_FILES['form_field']  ['type'])); //reg exp copied from CIs Upload.php
    
    if($mimetype == 'application/octet-stream'){
        $finfo = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
        if($finfo){
        $_FILES['form_field']['type'] = finfo_file($finfo, $_FILES['form_field']['tmp_name']);
        finfo_close($finfo);
        }
        else echo "finfo_open() returned false");
    }  
    

    Fileinfo extension needs to be installed on the server.

    It worked for me.

    0 讨论(0)
  • 2020-12-17 05:04

    Easy :)

    $image = imagecreatefromstring( $data );
    

    Specifically:

    $data = file_get_contents($_FILES['myphoto']['tmp_name']);
    $image = imagecreatefromstring( $data );
    
    0 讨论(0)
  • 2020-12-17 05:06

    you can use this function too. it not require any other dependency. and work perfectly for other types also.

    function _getmime($file){
        if($info = @getimagesize($file)) {
            return image_type_to_mime_type($info[2]);
        } else {
            return mime_content_type($file);
        }
    } 
    
    0 讨论(0)
提交回复
热议问题