簡體   English   中英

使用imagejpeg將圖像添加到數據庫

[英]wtite a image to database using imagejpeg

基本上,我要做的就是將Image寫入數據庫。

據我了解imagejpeg函數-輸出應為純圖像數據,如果未設置“字符串$ filename”或為NULL。 但事實並非如此,唯一的輸出是“ 1”(正確)...

我如何獲取圖像數據,而無需先將圖像存儲在文件系統中並重新加載它?

這是我的代碼示例:

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

文檔中介紹了$filename參數:

如果未設置或為NULL,則將直接輸出原始圖像流。

關於返回值

成功返回TRUE,失敗返回FALSE。

該功能將圖像輸出到標准輸出。 它不會return它。

您可以從標准輸出中捕獲它,如下所示:

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

我強烈建議您避免將二進制數據(例如圖像)存儲在數據庫中。 您可以簡單地在表中記錄所有內容,包括圖像名稱,創建日期等,然后只需將該文件放在文件系統中即可。

我知道這不是您要尋找的答案,但是我認為這可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM