How view image from database by using php - php

I am using localhost. I insert image in database blob. I am trying for show image. But I am fail. Image does not show. Database have three field id,name,image. Trying this code.
uimage.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$image=mysql_query("SELECT * FROM image WHERE id=1");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
and using this code in index.php page:
echo "Image Uploaded.<p />Your image:<p /><img src=uimage.php>";
But image did not show. Please help me.
My Full Code here: http://codepad.org/hJ8qucml

The easiest type of handling images in PHP-based websites is to save the images with the its id as name in a separated folder and just saving the id in database!
i really recommend you this type of working with images!

These are quick examples and you should validate any data being passed that is being used in a query. Check out Mysqli.
Using inline base64 encoding:
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$result=mysql_query("SELECT name, myimg FROM image WHERE id=1");
$image=mysql_fetch_row($result);
$imagesrc=$image[1];
echo '<img src="data:image/jpeg;base64,'.base64_encode($imagesrc).'"/>';
Using PHP file to call the image:
<img src="image.php?id=<?php echo $id; ?>" />
image.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$result=mysql_query("SELECT name, myimg FROM image WHERE id=1");
$image=mysql_fetch_row($result);
$imagesrc=$image[1];
header('Content-Type: image/jpeg');
echo $imagesrc;
?>

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 lightbox image retrieved from longblob database

I'm using the lightbox plugin: Fancybox
I wanted to apply lightbox effect on image gallery php file. The image is stored in longblob format instead of folder. Following is the code I did to read image.
<?php
$img_slct=mysql_query("SELECT * FROM news_gallery WHERE news_id='$page[news_id]' ORDER BY position ASC");
while($img=mysql_fetch_array($img_slct))
{ ?>
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
<?php } ?>
load_image.php
$id=$_GET['id'];
$is_file=false;
if(!empty($id))
{
$file=mysql_query("SELECT id FROM news_files WHERE id='$id'");
if(mysql_num_rows($file)>0)
{
$is_file=true;
$file=mysql_fetch_array($file);
}
}
if($is_file)
display_file_news($id);
Everything is displayed correctly but that is an issue when using lightbox to display the image retrieved from longblob database.
<a rel="example_group" href="load_image.php?id=<?php echo $img['file_id']; ?>">
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
</a>
I have no idea how to read the longblob database using lightbox. So I simply put like this --> href="load_image.php?id=<?php echo $img['file_id']; ?>"
I know that is wrong, but can you help me? Please...Let me know if you need more info, because I can't insert all my code at here. Thank you.
Following are my database tables.
news_files table
news_gallery table

how to show blob image in html?

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>

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\">";
}
?>

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