I have following code for get image from my database table field.
public function getStudentses() {
$students_data = array();
$query = $this->db->query(
"SELECT * FROM " . DB_PREFIX . "students
WHERE customer_id = '" . (int)$this->customer->getId() . "'");
foreach ($query->rows as $result) {
$students_data[$result['students_id']] = array(
'students_id' => $result['students_id'],
'firstname' => $result['firstname'],
'filename' => $result['filename'],
'image' => $result['image'],
);
}
return $students_data;
}
This is my HTML code for display image:
<img src="<?php echo $result['image']; ?>" />
but when I render my page the image is not display properly its like some symbols like
s4�.���N����萗�p�A#4pbr��]�����F�G�>�v��W
Why its like this? How can I fix my error?
Modern browsers have this support for using raw image data as source.
You can try to use something like:
<img src="data:image/png;<?php echo $result['image']; ?>" />
You should encode the image using base64 and then output something like:
<img src="data:image/png;base64,<?php echo base64_encode($result['image']); ?>" />
I have use image/png for example. If your image is something else, you should use corresponding encType.
Just make sure to read this and this.
You will most likely need to change the header to an image
http://php.net/manual/en/function.header.php
header('Content-Type: image/jpeg');
Most common practice for this (what I've seen) is to build html:
<img src="image.png.php?student_id=<?php echo $result['students_id']; ?>" />
And then image.png.php:
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "students "
"WHERE students_id = '" .(int)$request->getRequestParam('sudent_id') . "'");
// And now display raw content as image
$student = reset( $query->rows);
if( !$student){
header('HTTP/1.0 404 Not Found');
die( 'Blah blah... Blah blah blah');
}
header( 'Content-Type: image/jpeg');
echo $student['image'];
This way you'll be able to create links to images usable from whatever place you'll want (without any database access required on remote site).
Assuming you are storing the raw binary of the image, you can do this:
'image' => base64_encode($result['image']),
Then in the html:
<img src="data:image/png;base64,<?php echo $result['image']; ?>" />
This assumes the image is a png. If not, change the mime type accordingly.
if the image is stored by this code
s4�.���N����萗�p�A#4pbr��]�����F�G�>�v��W
its because you are just copying and pasting this code from database.
be sure where it stored the the name/id of the image and the path of the image and get the image by its path and id/name or follow what Prasanth said to you.
this my help you
How to retrieve images from MySQL database and display in an html tag
Related
I have a image stored in mysql as mediumblob and now I want to show it in html page, so I am doing this with it:
$c = base64_encode($resu[0]->image);
$image = '<img src="data:image/jpeg;base64,'.$c.'" />';
echo $image;
But I am getting only half of original image, so am I missing something here ?
Just an idea, may be you can seperate the logic to display the image.
What I mean is that you can create a file like image.php that accepts the id or filename
of the image and then display the image. Then you can simply refer the image in your HTML
by, for example, doing something like this:
<img src="image.php?imgId=12547"/>
in image.php file something like the following
$imgId=isset(GET['imgId'])?GET['imgId']:0;
$sql = "SELECT * FROM theBlogs WHERE ID = $imgId;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['imageContent'];
Just perform a quick test with strlen on $resu[0]->image variable and check blob size in DB and check if they are different sizes for sure.
Display in li tag means use this
<li data-thumb='<?php echo "data:image/jpeg;base64,".base64_encode($img1 ); ?>'>
<div class="thumb-image">
<img <?php echo 'src="data:image/jpeg;base64,'.base64_encode( $img1 ).'"';?>
data-imagezoom="true" class="img-responsive" alt="">
</div>
</li>
I tried to link stored image(blob data) inside MySQL DB to PHP but only thing I achieved was whole page of characters. This is what I did:
$query = "SELECT image FROM uploads WHERE id = {$id}";
$image_array = mysql_query($query, $connection);
$row = mysql_fetch_array($image_array);
$image = $row['image'];
echo $image;
// echo base64_decode($content);
This displays raw data how it is stored. I would like to link it into HTML tag or atleast display it on page, where I display a lot another stuff to so
header('Content-type: image/png');
is not solution for me.
Any advice?
To show the image correctly you just need to put one header with Content-type: image/png before echo
header("Content-type: image/png");
echo (base64_decode($row['image']));
If you want to place instead in an image tag you just need to use this code
echo '<img src="data:image/png;base64,' . $row['image'] . '" />';
You need to give proper headers, otherwise the page wont know what you're sending it.
// define results into variables
$name=mysql_result($result,0,"file_name");
$size=mysql_result($result,0,"file_size");
$type=mysql_result($result,0,"file_type");
$content=mysql_result($result,0,"file_stream");
// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
You should use something that is called 'Inline image'. Use this code to display the image on a page, asssuming the $image variable contains base64 encoded data:
<img src="data:image/png;base64,<?php echo $image; ?>" alt="Larry" />
Source: https://en.wikipedia.org/wiki/Data_URI_scheme
I'm trying to display an image that is stored in my database. I know it is not best practice to store an image in the database, but just for this purpose its what I need. My problem is when I go to display the image, all I get is a load of scrambled code, which I guess is the image in code but not visually what I want. When I use the header tag to identify the image all I get is a thumbnail to mean it isn't reading it in right. Any help would be great.
Below is code I'm using. I've tried it in both ways using it to display in either the php or in html but can't display the image:
while($row = mysql_fetch_array($result))
{
$Long = $row['Long'];
$Lat = $row['Lat'];
$img = $row['file'];
echo "<b><center>Database Output</b><br><br>";
echo "<td>" .$row['file']."</td>";
echo "----";
echo "<td>" .$row['Lat']."</td></center>";
//echo "<td>" .$row['file']."</td>";
}
?>
<div id="map">
<img src="<? $img?>" alt="">
Thanks for any input
Either you embed the image into your html page as a data URI
<img src="data:image/jpeg;base64,<?php echo base64_encode($img) ?>" />
which is hideously NASTILY horrible for 'large' files - you make it impossible for the browser to cache the image, forcing the user download the base64-encoded data EVERY time they load the page.
or you have a sub-script to serve up the actual image, and have something more like:
<img src="getimage.jpg?id=XXX">
and
<?php
$data = get_image_from_db($_GET['id']);
header('Content-type: image/jpeg');
echo $data;
I've stored my Images into (Medium) BLOB fields and want to retrieve them embedded within my PHP-generated web pages.
When I test retrieving the stored images using
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
everything looks just fine.
However, I have not yet found a way to retrieve the image(s) cleanly into the middle of my documents. For example, using
$image = $row['file_data'];
echo '<img src="data:image/jpeg;base64,'.$image['file_data'].'" alt="photo"><br>';
...or...
$im = imageCreateFromString($image);
I just wind up with a bunch of hexadecimal garbage on screen.
I intitially stored the Images using:
ob_start();
imagejpeg($resizedImage, null, 100);
$content = ob_get_contents();
ob_end_clean();
$sql = sprintf(
"insert into images (filename, mime_type, file_size, file_data, event_id)
values ('%s', '%s', %d, '%s',%d)",
mysql_real_escape_string($fileName),
mysql_real_escape_string($mimeType),
$imageSize,
mysql_real_escape_string($content),
$eventID
);
$result = $cn->query($sql);
Does anyone PLEASE have a working code snippet to successfully display the stored .jpg mid-file in the PHP output?
echo '<img src="data:image/jpeg;base64,'.base64_encode($image['file_data']).'" alt="photo"><br>';
However, remember that old IE versions do not support this kind of inline images! Besides that, the browser cannot cache such an image except together with its containing HTML page.
You should create some sort of "image server". You're already close to that.
For example, create something like image.php that will get a image name and will generate it on the fly.
So, for example, say you want to get somePic.jpg image. You can get it through:
image.php?name=somePic.jpg
<?php
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
?>
Your tag:
<img src='image.php?name=somePic.jpg' />
Or more general:
echo "<img src='image.php?name={$image['filename']}' />"
Why not just call your test page image.php, then have it called from the browser on the rendered page:
<img src="image.php?imageid=123" alt="photo" />
I have two separate files, one is to display the html/php document image, and the other is a php file that renders the image using the header function content-type:image/jpeg.
I tried using it with one image and it works well. However, I need to display multiple images. How could I do this?
The html/php doc has an img tag that points out to the php file that renders the image
echo "<image src=Image.php>";
The image.php
$selectimage = mysql_query("SELECT Image from ImageTbl", $con);
if($selectimage)
{
header("Content-type:image/jpeg");
while($row = mysql_fetch_array($selectimage))
{
echo $row["Image"];
}
}
make two files one for image another for fetching the row like this
image.php
$image_id = $_GET["id"];
header("Content-type:image/jpeg");
//query database to get only one image from id
echo $row["Image"];
another file
getimages.php
//query for image data
while($row = mysql_fetch_array())
{
echo "<img src='image.php?id=$row[id]' />";
}
You can't output all the images together, because to the browser, it will look like the data of multiple images mushed together, which is nonsensical. Also, each image tag can only display one image. To solve this, give the image table an ID field to identify the image.
Then in the file that outputs HTML, do something like this (passing the ID for the image you need):
echo "<image src='Image.php?id=1>";
echo "<image src='Image.php?id=2>";
echo "<image src='Image.php?id=3>";
And then in the file that outputs the image, do:
$id = intval($_REQUEST['id']); // intval will validate the ID to be an int
$selectimage = mysql_query("SELECT Image from ImageTbl WHERE id=$id LIMIT 1", $con);
if ($selectimage) {
$row = mysql_fetch_array($selectimage);
if ($row) { // check if the image really exists
header("Content-type:image/jpeg");
echo $row["Image"];
}
}
Use a foreach loop to loop through the requested records and echo them out independently to the img tags which your using.
That would be the best way in my opinion.
If you want to display number of different images using one script, try to add some unique hash to the script name ( for e.g. md5( microtime() ) )
$seed = md5( microtime() );
echo '<image src="Image.php' . $seed . '">';