gpt4 book ai didi

php - 在 MySQL 中保存为 BLOB 后获取不同的图像

转载 作者:行者123 更新时间:2023-11-29 14:09:37 25 4
gpt4 key购买 nike

我刚刚保存了一张图像(PNG、200x209)

$img = chunk_split(base64_encode(file_get_contents("image.png")));
$sql = "INSERT INTO table (img) VALUES ('$img') WHERE userid = 10";
mysql_query($sql);

(img 具有 MEDIUMBLOB 类型)

然后尝试获取它(show.php):

header("Content-type: image/jpeg");

$sql = "SELECT img FROM table WHERE userid = 10 LIMIT 1";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
$image = base64_decode($row['img']);
}

echo $image;

当请求show.php时,它给出几乎相同的图像,但尺寸不同:136x94 =)

为什么会发生这种情况?

最佳答案

我建议您按原样保存图像。 MySQL的BLOB数据类型专门用于二进制数据。这个图像是一个二进制文件。并删除base64编码。它只会增加数据大小。

在代码中,您保存 png 图像但输出 jpeg。两种内容类型应该相同。

所以插入代码会是这样的,

$img = file_get_contents("image.png");
$sql = "INSERT INTO table (img) VALUES ('$img') WHERE userid = 10";
mysql_query($sql);

并将其显示为

header("Content-type: image/png");

$sql = "SELECT img FROM table WHERE userid = 10 LIMIT 1";
$res = mysql_query($sql);
echo mysql_result($res, 0);

关于php - 在 MySQL 中保存为 BLOB 后获取不同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13726892/

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