how to show blob image in html? - php

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>

Related

Images from mysql does not display using php

Image is successfully fetching from database but it not be shown by the PHP. In place of image it shows a iamge thumbnail.
If I use header('Content-type:image/jpg'); it will show the only thumbnail all page contents disappear.
include 'functions/connect.php';
$user = $_SESSION['email'];
$sql = "SELECT photo FROM user WHERE email='$user'";
$run_sql = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($run_sql);
$user_photo =$row['photo'];
echo"<p><img src='$user_photo'></p>";`
You can try something like this:
<img alt="<?php echo $user_photo; ?>" src="uploads/<?php echo $user_photo; ?>" /> // Upload is folder where image was uploaded
It depends on what the field "photo" contains. If the photo field contains the location of the picture on your system, then the way you have done it should work. However, if the field contains the actual photo in binary format (having field data type as 'blob') then you'll need to do the following:
file_put_contents("image.jpg", $user_photo);
echo "<p><img src='image.jpg' ></p>";

Display second image when the 1'st image was selected

I have a list with images which I display from db like this:
<?php
$result = mysql_query("SELECT * FROM images ORDER BY position");
while ($image = mysql_fetch_array($result)){
?>
<img src="/<?php echo $image['thumb'];?>" />
php } ?>
I already created a mask where to display the big image but I have no idea how do I know which image is clicked then to display the big image inside the popup.
Fields I use from db are
image_id
image
position
thumb
You need to surround your <img> tag with a <a> link that points to another PHP script which retrieves the correct image:
Inside your while() loop:
<a href='getImage.php?id=<?php echo $image['image_id']; ?>'>
<img src="/<?php echo $image['thumb'];?>" />
</a>
getImage.php
getImage.php mainly displays an <img> tag containing the full size image selected earlier. Be sure to fill in the rest of the HTML it needs as necessary, as I have only included the <img> tag below.
$img = isset($_GET['id']) ? intval($_GET['id']) : NULL;
if ($img) {
$image_qry = mysql_query("SELECT image FROM images WHERE image_id = $img");
if ($image_qry) {
$image_data = mysql_fetch_assoc($image_qry);
// One row was returned...
if ($image_data) {
echo "<img src='{$image_data['image']}' alt='alt text...' />";
}
}
}
else {
// Invalid id, not an integer
}
<?php
$result = mysql_query("SELECT * FROM images ORDER BY position");
while ($image = mysql_fetch_array($result)){
?>
<img src="/<?php echo $image['thumb'];?>" onclick="openImage('<?php echo $image["image"];?>');" />
<?php } ?>
than write a javascript function called openImage(image_src) that will put the image in the right location
Look at this and pick something that satisfy your needs http://www.1stwebdesigner.com/css/fresh-jquery-image-gallery-display-solutions/
<script>
function showBigImage(image_id){
// show your big image here with using image_id
}
</script>
<?php
$result = mysql_query("SELECT * FROM images ORDER BY position");
while ($image = mysql_fetch_array($result)){
?>
<img src="/<?php echo $image['thumb']; ?>" onclick="showBigImage('<?php echo $image["image_id"]; ?>');" />
<?php } ?>

how to display image on web page from mysql in php

I have to display images on my php web page from mysql database where i have give the path of my images. but Its only displaying the path but not the image.
I used the following Code:
<?php
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("ek",$conn);//EK is my Database
$sql="select Pics from images";
$result=mysql_query($sql,$conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$image = $row['Pics'];
print $image;
?>
and the output I am getting is :
images/DSC01750.jpg
Some body can you please help me to actually displaying the image but not the path.
Many Thanks
If you wish to display an image in an HTML page, you can construct the image tag like this:
<img src="<?php echo $image; ?>" />
If you are attempting to obscure the location of an image and serve it via the PHP script, your image tag will look something like this:
<img src="yourscript.php" />
and you can modify your script to open and output the file:
$image = $row['Pics'];
header("Content-Type: image/jpg");
readfile( $image );
If $row['Pics'] contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. Something like this:
<img src="<?php echo $image; ?>" />
Note that "path" can mean something entirely different here. If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. Something that begins with /usr/web/something/blah/ won't be useful to the browser.
You need to wrap it in an img tag.
echo "<img src=\"$image\">";
To display several images
<?php
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("ek",$conn);//EK is my Database
$sql="select Pics from images";
$result=mysql_query($sql,$conn) or die(mysql_error());
$i=0;
$display_num =4;
while($row = mysql_fetch_assoc($result) && $i++ < $display_num){
$image = $row['Pics'];
echo "<img src=\"$image\">";
}
?>

Read multiple image php

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 . '">';

How to display image from mysql blob in PHP (joomla)

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.
I think you have 2 options:
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.
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=
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.

Categories