1

I create a component in my joomla website. the component shows some photos (not big, only 8KB). the photos are stored in mysql blob. i can upload the photos to the joomla database but i cannot display it on the website. whatever i do it only show some encoding character or blank. I tried to create a separate page but but the result is same. Here is what i have done : mycomp is my joomla component.

admin.mycomp.php

<?php
function showDetail($option)
{
    $db = &JFactory::getDBO();
    $id = mysql_real_escape_string(JRequest::getVar('id'));
    $query = "select id,myphoto from jos_myphotos where id = ".$id;
    $db->setQuery($query);
    $rows = $db->loadObjectList();
    HTML_myphoto::showPhoto($rows,$option);
}     
?>

admin.mycomp.html.php

<?php
class HTML_myphoto
{
   ...
   function showPhoto($row,$option)
   {
      ...
      header("Content-type: image/jpeg");
      echo $row->myphoto;  //this will show some encoding character
      echo base64_decode($row->myphoto);  //this will show blank page
      //change echo with print get the same result.
      ...
   }
   ...
}

I tried to create a separate page like this : admin.mycomp.html.php

<?php
class HTML_myphoto
{
...
function showPhoto($row,$option)
{
    ...
?>
    <img src="show_image.php?myphoto=<?php echo $row->myphoto;?>" width=200 height=300>
<?php   
    ...
}
...
}

show_image.php

<?php
     $myphoto = (isset($_GET['myphoto'])) $_GET['myphoto'] : false;
     if($myphoto)
     {
         header("Content-type: image/jpeg");
         echo $myphoto;  //this will show some encoding character
         echo base64_decode($myphoto);  //this will show blank page
         //change echo with print get the same result.
     }
?>

the result is same.

9
  • When I see show_image.php, it seems you're sending the whole file through the URL. This file should just receive an ID, get the content from the DB and echo it once (you echo it twice here) Commented Feb 8, 2011 at 10:29
  • Are you sure, the binary image data is base64 encoded in your db? Commented Feb 8, 2011 at 10:29
  • Either you put the blob as src, or you call an image like this: <img src="show_image.php?myphoto=<?php echo $row->id;?>"... Commented Feb 8, 2011 at 10:31
  • How do you save image to BLOB? Commented Feb 8, 2011 at 10:31
  • @Distdev: You can save image as string Commented Feb 8, 2011 at 10:32

2 Answers 2

1

I think you have 2 options:

  1. Either you make an image tag with its source in a PHP file receiving only a ID parameter and retrieving the photo's string in the DB and echoing it.
  2. Or you echo directly your photo's string in your tag:

    <img src="<?php echo base64_decode($myphoto); ?>" />
    

EDIT

I just checked in an old app where I store the favicons in a DB. You don't need to base64_decode when you display your image inline (my option 2).
So FYI, this image works:

<img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=" style="margin-right: 5px; vertical-align: middle;" class="bbns_itemDragger">

And it is stored in my DB like this (base64 encoded):

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=
Sign up to request clarification or add additional context in comments.

6 Comments

@Nabab If I use base64_decode($myphoto), it will show the blank page. If i remove base64_decode function it will show some encoding character
I'd say it means you don't base64_encode() when you put your file's string in the database, and you can't store raw binary data in a blob without encoding it.
@Nabab i tried your edited code (copy and paste) and it work but if i use like this : <img = src="data:image/jpeg;base64,<?php echo $myphoto;?>"> it still shows the encoding character. i am sure the images are stored in the database. i can view the images from mysql explorer.
How do we know what kind of encoding in the blob field mysql database? i didnt encode the images when i insert them to the database.
As BLOB is binary I guess you can store them as they are, but you have to echo them with base64_encode() - as HTML is not binary.
|
1

I'm sorry, did you skip some lines from show_image.php?! Cause $myphoto is just the id of the photo. You can't base64_decode an ID.

1 Comment

No, in that case, $myphoto is the image's whole string (!)

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.