Image not showing - php

I have used this code for accessing image from database to web page. But after debugging there is no image in image tag.
This code prints the correct path....but fails to return correct image in image tag...
echo $_SESSION['user_image'];
How do I get the image in image tag?
plz help me.
while($row = mysql_fetch_assoc($result))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['user_level'] = $row['user_level'];
//$_SESSION['user_image'] = $row['image'];
$image=$row['image'];
$user_image= trim('C:/xampp/htdocs/source/img/' . $image);
//$user_image = imagecreatefromstring($user_image);
$_SESSION['user_image']=$user_image;
echo $_SESSION['user_image'];
}
echo "<img src='".$row['image']."'>";
echo 'Welcome, <img src= "'.$_SESSION['user_image'].'" alt="" width="50"
height="40" /> ' . $_SESSION['user_name'] . '. <br /><a href="index.php">Proceed to
the forum overview</a>.';

You are using the local path in the server.
'C:/xampp/htdocs/source/img/' . $image
Should be
'/source/img/' . $image

Related

How to display name under photo in a foreach loop

Edit: I'm using php to accomplish this.
I'm making a page that draws information from a database (name, photo path) and I want it to show the picture and then the name of who it is underneath.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> '.$fullname.' ';
}
As of now, the name is displayed next to the photo and just looks bad. How would I go about making the name appear below the photo?
While a little more code than simply adding a line break, you could wrap your image in a <figure> and add a <figcaption> after your <img>.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<figure><img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><figcaption>'.$fullname.'</figcaption></figure>';
}
To display multiple images per line, add the following to your CSS:
figure {
display: inline-block;
}
Check it out in this jsfiddle.
Try this: Add <br> to create a line break
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> <br>'.$fullname.' ';
}
There are different options for this depending on your needs. A simple solution is to add a line break after the image:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><br /'.$fullname.' ';
If you want more control on styling, you can wrap the name in a div or span and assign a class to style it further with CSS:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><div class="imagename">'.$fullname.'</div>';
Of course you could also wrap the entire image and text in a div, with or without the full name in its own div within there for further styling flexibility.

Displaying image in php

Currently I'm trying to display an image from the database i created, using phpmyadmin to test my script. The database contains images in png and jpg. But the output are just nonsense characters because the browser cannot render the image. Can some one tell me where I get wrong?
I google the problem and I can see their pattern and mine are similar so I don't really under stand where I get wrong. I can get the name of the image in database, but not the image.
this is my script:
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
What format is your imaged stored as?
Raw bytes?
Base-64?
or a URL?
If it's a URL then just use the attribute 'src' of the html img element.
If it's Base-64 then use
<img src="data:image/jpg;base64,THE BASE 64 code here" />
If you have raw bytes, either convert them to Base-64 with PHP
$base64segment = base64_encode($data);
or you can call a custom url within the src attribute that will lead to a PHP script where you can create an image on the fly from the bytes by sending a custom header followed by the bytes:
<?php
header('Content-type: image/jpeg');
header('Content-Length: '.strlen($data));
echo $data;
?>
The disadvantage of the Base-64 based approach is that the data takes more space than the original binary segment because the encoding is done using 64 different signs instead of 256.
With this:
<img src="<?php echo $row["image"]; ?>
You are setting the source of the image. This should be either an URL or a path from your server root. What I usually did was save the file location and name (so for example "/images/imagename.png") in the database.
If you used MIME type or BLOB in the database, this code will not work.
Have you tried this?
$base64 = 'data:image/PNG;base64,' . base64_encode($row['image']);
echo "<img src=$base64 />" ;
In your code...
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo data:image/PNG;base64,'. base64_encode($row["image"]); ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
$row["image"] = "data:image/png;base64,".base64_encode($row["image"]); //the trick!
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You need to store image MIME type in database, then you can display image like this:
echo $row["image"];
header("Content-Type: ".$row['mime_type']);
For example, PNG's MIME type is image/png;

How to echo picture using php?

Please help me with this guys,
I have this code:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
echo '<center><img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}
else
{
echo "photo not uploaded";
}
the problem here is when i echo it and use its tmpname i am having an error in this section
<img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" //....
the userfile is underlined in red. Is it wrong to use its tempname in echoing it?
help me please...
Change your echo statement like this,
echo '<center><img src="admin/gallery/'.{$_FILES['userfile']['name']}.'" width="100" height="100"/>'.'</center>';
Also your if checks showing incorrectly.
May be like this,
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']")
{
.....
}
try with proper quoting and if()
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/".$_FILES['userfile']['name']))
{
echo '<center><img src="admin/gallery/'.$_FILES['userfile']['name'].'" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}else
echo "photo not uploaded";
}
you just have a problems with your ' and "
i would do it like this
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
this should do it
total source is
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
}
else
{
echo "photo not uploaded";
}
Try this. You can use php in html attributes and tags
<img src="admin/gallery/<?php {$_FILES['userfile']['name']} ?>" width="100" />
You should use:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
And
echo "<img src='admin/gallery/{$_FILES['userfile']['name']}' width='100'...";
or better close php with an ?>then
<img src='admin/gallery/<?php echo $_FILES['userfile']['name'];?>' width='100'...

How to call image url from directory using php

I want to display the image and the image url of a directory
I found this code, but there is a dot before the filename.
<?php
$files = glob("./images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
print "<textarea>";
print "http://" .$_SERVER['SERVER_NAME'].$image;
print "</textarea>"."<br />";
echo '<img src="'.$image .'" />'."<br /><br />";
}
?>
the result will be like this :
<textarea>http://domain.com./images/filename.png<textarea>
<img src="./images/filename.png" />
please help to fix this. thanks.
I solved the problem by using the following:
<?php
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
echo '<img src="'.$image .'" />'."<br /><br />";
print "<textarea>";
print "http://" .$_SERVER['SERVER_NAME'].$image;
print "</textarea>"."<br /><br /><br />";
}
?>

Default avatar pic always showing in Pal requests

im trying to develop a social networking application and im having some trouble showing a user's selected avatar. Every users can upload pictures and there is a field called avatar and when this is set to 1, it is the users avatar pic. Only one pic can be an avatar.
My PHP code:
<div class="interactContainers" id="pal_requests">
<?php
$pal_requests = "SELECT * FROM pal_requests WHERE mem2='$user_id' ORDER BY pal_request_id ASC LIMIT 50";
$pal_request_query = mysql_query($pal_requests) or die(mysql_error());
$pal_request_num_rows = mysql_num_rows($pal_request_query);
if($pal_request_num_rows < 1)
{
echo ' You have no Pal requests at this time.';
exit();
}
else
{
while($row = mysql_fetch_array($pal_request_query))
{
$request_id = $row["pal_request_id"];
$mem1 = $row["mem1"];
$query_user = "SELECT user_first_name, user_last_name, picture_thumb_url FROM users LEFT JOIN picture ON users.user_id = picture.user_id
AND picture.avatar=1 WHERE users.user_id='$mem1' LIMIT 1";
$user_info = mysql_query($query_user, $connections) or die(mysql_error());
}
if(!empty($row["picture_thumb_url"]))
{
$avatar = '<img src="/NNL/User_Images/$row["picture_thumb_url"]" width="50" height="50" border="0"/>';
}
else
{
$avatar = '<img src="/NNL/Style/Images/default_avatar.png" width="50" height="50" border="0"/>';
}
}
?>
<?php
while ($row = mysql_fetch_array($user_info)){ $requesterFirstName = $row["user_first_name"]; $requesterLastName = $row["user_last_name"]; }
{
$thumbnail_user = $row["picture_thumb_url"] != '' ? $row["picture_thumb_url"] : '../Style/Images/default_avatar.png';
echo '<hr />
<table width="100%" cellpadding="5">
<tr>
<td width="17%" align="left"><div style="overflow:hidden; height:50px;">'. $avatar .'</div></td>
<td width="83%"><a class="ordinary_text_12_blue "href="user_view.php?user_id2=' . $mem1 . '">'. $requesterFirstName .' '. $requesterLastName .'</a> wants to be your Pal<br /><br />
<span id="req' . $request_id . '">
<a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:acceptFriendRequest(' . $request_id . ');" >Accept</a>
OR
<a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:denyFriendRequest(' . $request_id . ');" >Deny</a>
</span></td>
</tr>
</table>';
}
?>
</div>
The default avatar is being called all the time even though a user has selected their own avatar. Can someone tel me what im doing wrong?
This Block of code should be inside the body of the second while loop. In first while loop the variable $row["picture_thumb_url"] is never set.
if(!empty($row["picture_thumb_url"]))
{
$avatar = '<img src="/NNL/User_Images/$row["picture_thumb_url"]" width="50" height="50" border="0"/>';
}
else
{
$avatar = '<img src="/NNL/Style/Images/default_avatar.png" width="50" height="50" border="0"/>';
}

Categories