2011-05-09 88 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); 
    } 
} 
相關問題