简体   繁体   中英

wtite a image to database using imagejpeg

Basically what I have to do, is write a Image to the database.

As far as I understand the imagejpeg function - the output should be the plain image data, if the "string $filename" is not set or NULL. But it does not, the only output is "1" (true)...

How do I get to the image data, without first storing the image in the filesystem and reloading it?

Here is my Code example:

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

The documentation says about the $filename parameter:

If not set or NULL, the raw image stream will be outputted directly .

And about the return value :

Returns TRUE on success or FALSE on failure.

The function outputs the image to the standard output. It does not return it.

You can capture it from the standard output like so:

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

I would highly recommend you to avoid storing binary data, such as images in database. You can simply record everything, including the image name, date of creation, etc. on your tables, then just put that file on the file system.

I know that it is not the answer you're looking for, however I thought that it might helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM