0

I'm currently having a problem with the image file which i need to call out from the database with php code by using joomla plugin (Sourcerer). But unfortunately it echo out many alien word. Is the code i use to echo out the image have some mistake?

Here is my sample code:

<?php

// Get default database object
$db =JFactory::getDBO();

// Get a new JDatabaseQuery object

$query = $db->getQuery(true);
// Build the query

$query = "

SELECT *

FROM ".$db->quoteName('college')."

WHERE ".$db->quoteName('userid').'='. $db->quote('0123').";

";

// Set the query for the DB oject to execute

$db->setQuery($query);
// Get the DB object to load the results as a list of objects

$results = $db->loadObjectList();

if($results){ 
foreach($results as $result){

echo "<label for='collegeid' class='field prepend-icon'>";
echo "<input type='text' id='collegeid' name='collegeid' class='gui-input' value='$result->collegeid' placeholder='College ID...'>";
echo "<label for='collegeid' class='field-icon'><i class='fa fa-user'></i></label>";

echo "</label>";

echo "</div><!-- end section -->";

echo "</div><!-- end .frm-row section -->";

echo "<div class='section'>";
echo "<label for='collegefullname' class='field prepend-icon'>";
echo "<input type='text' name='collegefullname' id='collegefullname' class='gui-input' value='$result->fullname' placeholder='College's full name...'>";
echo "<label for='collegefullname' class='field-icon'><i class='fa fa-envelope'></i></label>";
echo "</label>";
echo "</div>";

echo "


<div class='section'>
<label for='collegeshortname' class='field prepend-icon'>
<input type='text' name='collegeshortname' id='collegeshortname' class='gui-input' value='".$result->shortname."' placeholder='College's short name...'>
<label for='collegeshortname' class='field-icon'><i class='fa fa-envelope'></i></label> 
</label>
</div><!-- end section -->

<div class='section'>
<label for='description' class='field prepend-icon'>
<textarea class='gui-textarea' id='description' name='description' value='".$result->description."' placeholder='Description...'>".$result->description."</textarea>
<label for='comment' class='field-icon'><i class='fa fa-comments'></i></label>
<span class='input-hint'> 
Please write the description of the college... 
</span> 
</label>
</div><!-- end section -->

<div class='section'>
<h3>Upload College's Logo: </h3><br/>";

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

echo $result->logo;
.....

The result can be view at here: http://demo.matedis.com/add-and-edit-college

Marcus
  • 113
  • 1
  • 3
  • 13
  • In the long run you'll be better off using the JDatabaseQuery API, for example are you positive you have a space between `WHERE` and `'college'`? I guess you must or you would be getting an SQL error, but still using the API will prevent errors. – Elin Apr 30 '14 at 12:55
  • My query is don't have any problem, but the image i retrieve from the database has been shown full of alien word=.=...what should i do? – Marcus Apr 30 '14 at 14:08

1 Answers1

0

You cannot set header after you echo to the page: use base64 encoded image rather

Use base 64 encoding rather, and insert the result in the img tag, see this answer:

Assuming from your code that you are saving the file content only of jpeg images,

echo '<img src="data:image/jpeg;base64,'.base64_encode($result->logo).'" />';

will result in the expected markup:

<img src="data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAApAAD/7gAOQWRvYmUAZMAAAAAB/9sAhAAMCAgICQgMCQkMEQsJCxEUDwwMDxQXEhIUEhIXFhEUExMUERYWGhsdGxoWIyMmJiMjMjIyMjI4ODg4ODg4ODg4AQwLCwwODA8NDQ8UDg4OFBQPEBAPFBwTExQTExwjGhYWFhYaIyAiHR0dIiAmJiMjJiYwMC4wMDg4ODg4ODg4ODj/wAARCAAQABADASIAAhEBAxEB/8QAaAAAAwEAAAAAAAAAAAAAAAAAAQMEBwEBAQAAAAAAAAAAAAAAAAAAAgMQAAIBAwEJAQAAAAAAAAAAAAECAwARIVExYRIiMmITBAUUEQACAQQDAQAAAAAAAAAAAAABAgMAESES8DFhIv/aAAwDAQACEQMRAD8A0jiINiBnfmio2nU0pWaWFZYupuYBsA9p0qH6P2Y/TjDGKb9IZQPX8bsXBNiFZQUJtkWai9lP1jWqxQvIQsY2LYsOde1//9k="/>

aside from the fact that this is my site's logo ;-)

Community
  • 1
  • 1
Riccardo Zorn
  • 5,590
  • 1
  • 20
  • 36
  • But i store the image data in database as BLOB, how did i retrieve it? Should my code something like this? – Marcus Apr 30 '14 at 08:54
  • I improved my answer with an example – Riccardo Zorn Apr 30 '14 at 19:38
  • I just tested an empty document with just the image tag I pasted (the second one) and it works even in internet exploder. So it must be your image that's not raw, how did you insert it in the database? also try both "jpg" and "jpeg" (both work for me!) – Riccardo Zorn May 04 '14 at 22:49
  • The code is working on firefox, but when try on google chrome... the picture does't not shown up but it can be saves as images....is that a browser problem? – Marcus May 05 '14 at 16:35