2012-09-14 48 views
0

基本上我必須做的是將圖像寫入數據庫。使用imagejpeg將圖像重新映射到數據庫

據我瞭解imagejpeg函數 - 輸出應該是普通圖像數據,如果「字符串$文件名」沒有設置或NULL。 但它沒有,唯一的輸出是「1」(true)...

如何在不首先將圖像存儲在文件系統中並重新加載的情況下獲取圖像數據?

這是我的代碼示例:

// If it is the right filetype. 
if ($_FILES[$dom_element]['type'] == 'image/jpeg' || $_FILES[$dom_element]['type'] == 'image/pjpeg') { 

    // Create the image recource. 
    $image_image = imagecreatefromjpeg($_FILES[$dom_element]['tmp_name']); 
} 

// Resize the image. 
imagecopyresampled($image_image_p, $image_image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_width, $image_height); 

// Write the image in the database (does not work this way -> output = "1") 
mysql_select_db("$db_announcement"); 
$sql = "UPDATE announcement 
SET announcement_guest_image_data = '" . addslashes(imagejpeg($image_image_p, NULL, $settings_image_quality)) . "', 
announcement_guest_image_exists = 'yes' 
WHERE announcement_timestamp = '$timestamp' AND announcement_guest_mail = '$global_mail'"; 
$sql = mysql_query($sql); 

// Delete the image recources. 
imagedestroy($image_image); 
imagedestroy($image_image_p); 

回答

0

的文件說,有關$filename參數:

如果未設置或爲空,則原始圖像流將直接輸出

約在返回值:成功

返回TRUE或FALSE的失敗。

該功能將圖像輸出到標準輸出。它不是return它。

您可以從標準輸出捕捉到它,像這樣:

ob_start(); 
imagejpeg(...); 
$imageData = ob_get_clean(); 
+0

他需要使列類型成爲BLOB。 – wesside

+0

是的,如果它還沒有的話。我質疑將數據存儲在數據庫中有多聰明,但這不是這個問題的關鍵...... – deceze

+0

那就是它,完美的作品:-) – Andreas

0

我會強烈建議您避免存儲二進制數據,如數據庫圖像。您可以簡單地在桌面上記錄所有內容,包括圖像名稱,創建日期等,然後將該文件放在文件系統上。

我知道這不是你正在尋找的答案,但我認爲這可能有所幫助。