0

I've just saved an image (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 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: 136x94 =)

Why that happens?

2
  • 1
    shouldn't you set content type as PNG? Commented Dec 5, 2012 at 15:43
  • 1
    You're using a blob field. There's NO reason to be doing base64/chunking. Blobs are BINARY LARGE OBJECT and can directly handle the .png file as is. Commented Dec 5, 2012 at 15:43

1 Answer 1

1

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,

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

And show it as

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

$sql = "SELECT img FROM table WHERE userid = 10 LIMIT 1";
$res = mysql_query($sql);
echo mysql_result($res, 0);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.