将图像保存到mySQL数据库并打印


Saving image to mySQL db and printing it

要保存我正在使用的图像:

$image_bin = addslashes(file_get_contents($img_url));
mysql_query('INSERT INTO images
        (image_filename, image_type, image_bin)
    VALUES
        ("file.jpg", "image/jpeg", "'.$image_bin.'")');

显示:

$qry = 'SELECT image_filename, image_type, image_bin
    FROM images
    WHERE image_id = 1';
$result = mysql_query($qry);
$row = mysql_fetch_row($result);
header('Content-type: '.$row['image_type']);
echo stripslashes($row['image_bin']);

图像保存到数据库中,但图像大小变大。并且图像的颜色变得有些损坏。

例如,打开开发工具的Chrome中的图像截图:屏幕截图url

有人知道我做错了什么吗?

尝试。。。

//use mysql real escape
$image_bin = mysql_real_escape_string(file_get_contents($img_url));
mysql_query('INSERT INTO ' . PREFIX . 'news_images
    (image_filename, image_type, image_bin)
    VALUES
    ("file.jpg", "image/jpeg", "' . $image_bin . '")');

$qry = 'SELECT image_filename, image_type, image_bin
    FROM images
    WHERE image_id = 1';
$result = mysql_query($qry);
$row = mysql_fetch_row($result);
header('Content-type: ' . $row['image_type']);
echo $row['image_bin']; //no stripslashes