I’ve just saved an image (PNG, 200×209)
$img = chunk_split(base64_encode(file_get_contents("image.png")));
$sql = "INSERT INTO table (img) VALUES ('$img') WHERE userid = 10";
mysql_query($sql);
(img has a MEDIUMBLOB type)
then trying to obtain it (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;
when requesting show.php it gives almost the same image, but with another dimension: 136×94 =)
Why that happens?
I recommend you save the image as is. The BLOB datatype of MySQL is specially used for binary data. And this image is a binary file. And remove base64 encodings. It just increases the data size.
In the code you are saving png image but outputting a jpeg. Both content type should be same.
So insert code would be something like this,
And show it as