PHP create image from application/octet stream

廉价感情. 提交于 2019-12-18 04:21:45

问题


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 library, which means I need to be able to create an image object from the data.

Typically, I have been using imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, etc, to handle files uploaded from web forms, but these don't seem to work when coming to me as application/octet-stream.

Any ideas on how I can achieve my goal?

EDIT

Here is the code I use to create the image identifier...my handler works perfectly throughout my site, the only difference I can tell between my site and the data from the iOS is the content-type

public function open_image($path) {
        # JPEG:
        $im = @imagecreatefromjpeg($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # GIF:
        $im = @imagecreatefromgif($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # PNG:
        $im = @imagecreatefrompng($path);
        if ($im !== false) { $this->image = $im; return $im; }

        $this->error_messages[] = "Please make sure the image is a jpeg, a png, or a gif.";
        return false;
    }

回答1:


Easy :)

$image = imagecreatefromstring( $data );

Specifically:

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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/5942666/php-create-image-from-application-octet-stream

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