0

Good Afternoon, y'all!!

I have a database with a table containing a row with 4 columns called "ID", "login", "pass" and "image"(BLOB). I have a system with users authentication and etc, and I want to implement the image registered by the user to be shown in the topbar. Everything's working pretty well so far, the only problem is that the database image is printing like this:

ÿØÿàJFIF``ÿÛC  %# , #&')*)-0-(0%()(ÿÛC  (((((((((((((((((((((((((((((((((((((((((((((((((((
ÿÀ€€"ÿÄ    ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚  %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’
“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ  ÿĵw!1AQaq"2B‘¡±Á   #3R
ðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹º

Detail: When I open the image selected to the database in NOTEPAD, it's the same shown above.

IMAGE.PHP

include "dbconnection.php";

$defaultpath = "img/avatar.png";

$sql = "SELECT * FROM `bg_users`";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);

if (!isset($_SESSION)) {

$_SESSION['UserImage'] = $result['image'];

  print $_SESSION['UserImage'];
}
else {

  echo "src='img/avatar.png'";
}

?>

Again, the interaction between PHP and MySQL is completly fine. The problem is that PHP is not printing the file as image. I can't make header("Content-type: image/png"); because the page i want to include image.php already have a header parameter.

Could't someone help me on this? I would be very pleased

Thank You!

1 Answers1

1

First of all i wouldn't save images as blob in your database, easier to just store them in a folder and save the the path to it in the database.

If you want to do it your way, do the following:

echo '<img src="data:image/png;base64,'.base64_encode( $result['image'] ).'"/>';

This is a really bad practice though and might slow down the page load quite a bit. I'd suggest you re-evaluate you database structure and store the images directly and just save the path in the database

Jester
  • 1,408
  • 1
  • 9
  • 21
  • this is going to be terribly slow on page loading if the image is nothing more than few pixels. – bansi Apr 06 '16 at 15:38
  • @bansi yup, i'm not at all saying that this should be endorsed ;') but yeah i'll edit my question to make that clear – Jester Apr 06 '16 at 15:39
  • @Jester Thank you for the help. I'll re-evaluate the table as you said, redo the user registration to save the image file in a folder and the path in the database. Thank you again. – Filipe Soares Apr 06 '16 at 16:03
  • @FilipeSoares no problem, maybe even think about making a seperate folder for each user. That way you can expand to multiple photos for each user later if you want. ^^ – Jester Apr 06 '16 at 16:14
  • image in filesystem is the fastest option. but in any case you want the image to be in db itself, you need to create another php file which pulls the image from db and serve it (nothing other than the image), and you can use that php for your `src` – bansi Apr 06 '16 at 17:52