display image using php - php

I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".
I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers
<?php
session_start();
$username = $_SESSION['username'];
$con = mysql_connect('localhost','root','password');
mysql_select_db("db");
$profileimage = mysql_query("
SELECT * FROM images WHERE username='$username'
");
$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];
?>
<html>
<img src = "$showimage">
</html>

First off, HTML doesn't know what "$showimage" means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.
So if the value for $showimage is "/images/foo.jpg" you would need something like:
<img src="<?php echo $showimage; ?>" />
which would give you
<img src="/images/foo.jpg" />
Now, switching things to mysqli is as simple as replacing mysql with mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.

Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:
<html>
<img src ="<?php echo $showimage; ?>" />
</html>

<?php
$db = mysqli_connect("localhost:3306","root","","databasename");
$sql = "SELECT * FROM table_name ";
$sth = $db->query($sql);
while($result=mysqli_fetch_array($sth)){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
}
?>
Works for me

Related

I can't display images from database MySQL

I can't display images from database.
I have folder called images next to index.php where I keep images.
I suppose I have wrongly defined folder path with images or sth else.
<?php
$connection = new mysqli("localhost", "root", "", "crud");
$sql = "SELECT * FROM test";
$res = $connection->query($sql);
if(#$res->num_rows > 0)
{
while($row = $res->fetch_assoc())
{
?>
<img src="<?php "C:/xampp/htdocs/img_mysql/images/".$row['image'] ?>" style="width:170px;height:120px" />
<?php
}
}
?>
I put picture with how look my website when I try display images.
2 Issues:
You are not outputing anything.
You are using a path to the file on your computer rather than of the
server itself.
Fix it like this:
<img src="<?php echo "/images/".$row['image'] ?>" style="width:170px;height:120px" />
You shouldn't use a domain in your frontend pathes for the simple reason that in production you will have a different domain. If you really want to for some reason, a fine solution will be using a constant, something like this:
[In a file included from all pages]:
define("IMAGES_PATH", "http://localhost/images/");
Then
<img src="<?php echo IMAGES_PATH . $row['image'] ?>" style="width:170px;height:120px" />

how do i echo an image in another page based on its id?

How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)

Joomla - posting pictures

I am trying to post pictures in a joomla page. I am completely new in php and joomla.
This is what I have in a joomla page:
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<p><img src="images/test.php?id=8" alt="powered by" />
</p>
<p>Done!</p>
this is test.php:
<?php
$link=...... //connection to database
if(isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$query=...
$result=mysqli_query($link,$query);
while($row=mysqli_fetch_array($result)) {
$imageData = $row['photo'];
}
header("content-type: image/png");
echo $imageData;
}
?>
This is just for a simple 1 photo print, I eventually want to print multiple photos, and have like a slideshow.
Is this possible using joomla?
I also tried several other ways, but I had no luck. Is it possible to have ?
I just need a way through loop through images that are in a database, and print them out. Is there a good approach to this than what I am taking? Do I have to install plugins?
This is a completely wrong approach.
You have to develop a custom module to handle your slideshow task. You could start here.
As for your current code you have to convert it to:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__helloworld'))
->where('id = '. $db->Quote($params));
$db->setQuery($query);
$results = $db->loadObjectList();
print_r($results);
?>
Please check the full list of joomla database functions.
This part will help you how you could display your results.
Good Luck!

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

How can I retrieve HTML/PHP code stored in a mySQL table and not have the PHP commented out?

I am storing in a mySQL table the HTML/PHP content of individual slides to be displayed on a single page.
Here is an example of HTML/PHP code stored in the mySQL table:
<p>Welcome <?php echo $userData['fname']; ?>!</p>
<p>You made it to the first slide!</p>
I retrieve the content of the slides in PHP with the following code:
<?php
$fetchedPageSlideData = mysql_query("SELECT * FROM pageSlides WHERE pageID = $pageID ORDER BY 'order' DESC") or die(mysql_error());
while ($pageSlideData = mysql_fetch_array($fetchedPageSlideData)) {
$pageSlideContent = $pageSlideData['content']; ?>
<div><?php echo $pageSlideContent; ?></div>
<?php }
?>
All of the HTML of the content displays correctly, but the PHP is inserted as follows:
<!--?php echo $userData['fname']; ?-->
So the PHP is commented out and doesn't display.
How can I retrieve the HTML/PHP code and have the PHP not commented out?
It might be a better idea to use placeholder strings in the DB data. Executing arbitrary php code from a DB can be dangerous. PHP is Evil
Look into PHP function eval(): http://php.net/manual/en/function.eval.php
Dropping in and out of the PHP interpreter makes your code rather difficult to read. Consider:
<?php
$f = mysql_query(
"SELECT *
FROM pageSlides
WHERE pageID = $pageID
ORDER BY 'order' DESC"
) or die(mysql_error());
while ($d = mysql_fetch_array($f)) {
print "<div>" . $d['content'] . "</div>\n";
}
Regardless there is no implicit nor explicit mechanism here which would inject the comment tags you've presented. However it may be the browser trying to make sense of the unescaped html code and <?php ... ?> tags.
Try:
print "<div>" . htmlentities($d['content']) . "</div>\n";
As a side note, you might consider using
print "<div>" . highlight_string($d['content']) . "</div>\n";
Or do you mean that you actually want to run the code stored in the database - if so, you're asking for a world of pain. Eval is not evil - but you really must know what you're doing to avoid getting bitten by it.

Categories