Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用imagejpeg将图像插入数据库_Php_Image_Gd - Fatal编程技术网

Php 使用imagejpeg将图像插入数据库

Php 使用imagejpeg将图像插入数据库,php,image,gd,Php,Image,Gd,基本上,我要做的就是将一个图像写入数据库 据我所知,imagejpeg函数-如果“string$filename”未设置或为空,则输出应为普通图像数据。 但事实并非如此,唯一的输出是“1”(true) 如何获取图像数据,而不首先将图像存储在文件系统中并重新加载它 下面是我的代码示例: // If it is the right filetype. if ($_FILES[$dom_element]['type'] == 'image/jpeg' || $_FILES[$dom_element][

基本上,我要做的就是将一个图像写入数据库

据我所知,imagejpeg函数-如果“string$filename”未设置或为空,则输出应为普通图像数据。 但事实并非如此,唯一的输出是“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);

文档中介绍了
$filename
参数:

如果未设置或为空,原始图像流将直接输出

关于返回值:

成功时返回TRUE,失败时返回FALSE

该函数将图像输出到标准输出。它不会
返回它

您可以从标准输出中捕获它,如下所示:

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

我强烈建议您避免在数据库中存储二进制数据,例如图像。您可以简单地在表中记录所有内容,包括图像名称、创建日期等,然后将该文件放到文件系统中


我知道这不是您要寻找的答案,但我认为这可能会有所帮助。

他还需要将列类型设置为BLOB。是的,他会的,如果还没有的话。首先,我质疑在数据库中存储图像有多明智,但这不是问题的重点…@user1128688很高兴它能工作。如果这个答案解决了问题,请用复选标记接受它。