PHP create image from application/octet stream

北战南征 提交于 2019-11-29 05:15:29

Easy :)

$image = imagecreatefromstring( $data );

Specifically:

$data = file_get_contents($_FILES['myphoto']['tmp_name']);
$image = imagecreatefromstring( $data );

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.

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