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

时间:2012-09-12 17:22:05

标签: php mysql image escaping store

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

$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中打开了开发者工具的图片截图: screensho url

任何人都知道我可能做错了什么?

1 个答案:

答案 0 :(得分:4)

...试

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