2011-05-09 89 views
3

我的应用程序正在通过内容类型为“application/octet-stream”的移动设备发送图像。PHP从应用程序/八位字节流创建图像

我需要使用GD库处理这些图像,这意味着我需要能够从数据创建图像对象。

通常,我一直在使用imagecreatefromjpeg,imagecreatefrompng,imagecreatefromgif等来处理从Web表单上传的文件,但这些似乎不适用于以应用程序/八位字节流的形式来到我的应用程序。

关于如何实现我的目标的任何想法?

编辑

下面是我用它来创建图像识别......我的处理程序完美的作品在我的网站上的代码,唯一的区别我可以在我的网站,并从iOS的数据之间是讲的内容 - 键入

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

对于GD如何处理数据,MIME类型应该没有意义。显示您使用的代码 – 2011-05-09 21:23:53

+0

+1请向我们展示您使用的代码。 – 2011-05-09 21:32:21

+0

我把代码放在那里,谢谢:) – johnnietheblack 2011-05-09 21:32:31

回答

5

易:)

$image = imagecreatefromstring($data); 

具体来说:

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

HEYAAA,是$数据只是被找到$ _FILES ['myphoto'] ['tmp_name']? – johnnietheblack 2011-05-09 21:30:02

+0

@johnnietheblack - 不完全,但足够接近:'$ image = imagecreatefromstring(file_get_contents($ _ FILES ['myphoto'] ['tmp_name']));' – Christian 2011-05-09 21:32:05

+0

ahh,这很有道理......所以tmp文件基本上只是一个“文本文件”与字符串里面? (im显然是数据处理这方面的新手) – johnnietheblack 2011-05-09 21:33:50

0

我发现这个在笨论坛的方式来改变MIME和它的作品,我想你可以使用其他框架为好,这是link这个代码:

//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的延伸需要安装在服务器上。

它为我工作。

0

你也可以使用这个功能。它不需要任何其他依赖。并且也适用于其他类型。

function _getmime($file){ 
    if($info = @getimagesize($file)) { 
     return image_type_to_mime_type($info[2]); 
    } else { 
     return mime_content_type($file); 
    } 
} 
相关问题