gpt4 book ai didi

php - 如何在 MySQL 中存储内存中的图像

转载 作者:行者123 更新时间:2023-11-29 12:17:30 24 4
gpt4 key购买 nike

我正在尝试上传图像,更改其大小,然后将结果写入 MySQL 数据库。基本代码:

    // load original image
$image = imagecreatefromjpeg($file); // load original image

// create new image
$newImage = imagecreatetruecolor($newWidth, $newHeight);

// copy original to new, changing size
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight,$origWidth, $origHeight);

// save new image in database (in a BLOB field)
mysql_query("UPDATE myTable SET Photo='" . mysql_escape_string($newImage) . "' WHERE keyField=2");

但没有存储任何内容。 $newImage 似乎是尺寸正确的有效图像。我做不到什么?

最佳答案

作为@Wayne said 。我建议将图像保存在文件系统中并将文件路径保存在数据库中
这是将图像复制到文件系统的代码

 $imagepath="C:/your/file/complete/path";
if(isset($_FILES["file"])){
move_uploaded_file($_FILES["file"]["tmp_name"], $imagepath);
mysql_query("UPDATE myTable SET photo_path='" .$imagepath. "' WHERE keyField=2");
}

编辑:如果您确实需要保存在数据库中,请检查 this详细说明

关于php - 如何在 MySQL 中存储内存中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com