Bryner
Bryner

Reputation: 407

BLOB images not displaying fully

When retrieving a BLOB image from my database, it only renders about 1/5th of the image and stops.

Example:

Partial BLOB Image Displayed

fetchavatar.php - Retrieve image from database.

// Start session.
session_start();

$id = $_SESSION['id'];

$stmt = $con->prepare("SELECT * FROM avatars WHERE user_id = ?");
$stmt->bindParam(1, $id);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

$fetchedrows = $stmt->rowCount();

$_SESSION['avatar'] = base64_encode($result['data']);

$avatar = "<img src='data:image/jpeg;base64,". $_SESSION['avatar'] . "'>";

upload.php - Insert image into database.

include "fetchavatar.php";

$name = $_FILES['avatar']['name'];
$temp = $_FILES['avatar']['tmp_name'];
$size = $_FILES['avatar']['size'];
$type = $_FILES['avatar']['type'];

$fp = fopen($temp, 'r');
$img = fread($fp, filesize($temp));

fclose($fp);

if($fetchedrows) {
  $stmt = $con->prepare("UPDATE avatars SET name = ?, type = ?, size = ?, data = ? WHERE user_id = ?;");
} else {
  $stmt = $con->prepare("INSERT into avatars (name, type, size, data, user_id) VALUES(?, ?, ?, ?, ?);");
}

$stmt->bindParam(1, $name);
$stmt->bindParam(2, $type);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $img);
$stmt->bindParam(5, $id);
$stmt->execute();

profile.php - Display profile page.

<html>

...

<?php include "fetchavatar.php"; ?>

...

<body>
 <div id="profile_div">
 <h1>Profile Page</h1>

  <?php echo $avatar ?>

...

</body>
</html>

Upvotes: 1

Views: 1967

Answers (1)

Sam Teng Wong
Sam Teng Wong

Reputation: 2439

change your datatype from blob to longblob.. =)

Upvotes: 4

Related Questions