Print an image in html using session variable from php - php

i have this code trying to print an image in an html using
session variable from a php.Here is my code:
</head>
<body>
<?php
session_id(1);
session_start();
echo $_SESSION['phname'];
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
here:
session_id(1);
session_start();
echo $_SESSION['phname'];
i'm checking if my variable has passed
from the php and its ok.
and here
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
i'm using php to print the image from the following source
src="../uploads/$_SESSION['phname']"
when $_SESSION['phname'] is my image's name but i'm not getting the image. Is there something wrong with my code or
is there any other way to print my image?
i try a lot of things and a lot of codes
i found on the net but nothing help me more.

You need to do your session_start before you output anything to the browser as it sends some headers.
And of course when you want to echo something in php, you need php tags and an echo statement...

<?session_start();?>
</head>
<body>
<?php
session_id(1);
echo $_SESSION['phname'];
echo "<img src=\"../uploads/". $_SESSION['phname']."\" alt=\"photo\" width=\"498\" height="720" border="0" />";
?>

Related

How to pass a variable/change html from php

So I'm using PHP to grab my profile picture from my steam profile and i want to display it on the page is there any way to pass the variable containing the picture to html or edit my html code from PHP???
here is my code
<?php
$pic_path = file_get_contents("http://steamcommunity.com/id/LocalSugarDaddy");
preg_match('/<div class="playerAvatarAutoSizerInner"><img src="(.*)" \/><\/div>/i', $pic_path, $pic);
?>
I guess you are trying to use PHP variable containing image URL and want to use in html tag. If so, then you can do below:
<?php
$your_picture_str = "http://www.domainname.com/picture_url.jpg";
?>
<div><img src="<?php echo $your_picture_str; ?>" width="100%" alt="" title="" /></div>

How to have image src a variable?

I want to keep src attribute in a variable. I am working on php and tried the following method but it doesn't display the image on html page.
Code:
<?php $path="C:/horizontal.jpg"; ?>
<image src="<?php echo $path; ?>" style="width:304px;height:228px" />
I think you can use this technique:
<?php $path="http://" . $_SERVER["SERVER_NAME"];?>
<img src="<?php echo $path.'/projectName/image.jpg'; ?>" style="width:304px;height:228px" />
Where:
$_SERVER["SERVER_NAME"]: to get the server name, for example: www.example.com.
Also you can use $_SERVER["SERVER_ADDR"], in this case you can get the address IP of your server, for example: "127.0.0.1".
I hope this information helps you.
Good Luck.
<?php $path="localhost/projectName/";?>
<img src="<?php echo $path.'image.jpg'; ?>" style="width:304px;height:228px" />

retrieve image from folder in php codeigniter

imagedisplay.php(view)
<html>
<body>
<h3>Your file was successfully uploaded!</h3>
<?php print_r($upload_data); ?> </br>
<?php $str=base_url()."images/".$upload_data['file_name'] ?> </br>
<?php $str=str_replace('http://','',$str) ?>
<?php echo $str; ?>
<img src= '$str'/> </br>
For echo $str; I got the string i need to display th image
but when i pass it to img src.... i am not able to display it on the browswer
Is there any syntactical error or am i missing anything ...pls help?
Just a small syntax problem here.
Embed PHP echo command in the HTML code, like so:
<img src="<?php echo $str; ?>"/> </br>
or embed PHP echo short tags:
<img src="<?=$str?>"/> </br>
In other words: insert the PHP output at the positions, where you need it as HTML content.

Error displaying blob image in php/html

I am trying to display image from a blob field of a MySQL table. Looks like I have some sort of error in the following line. As soon as I put "header("Content-type: image/jpeg")" things get messed up and instead of displaying webpage, all source code of the page is displayed.
Please let me know how to correct.
<div class="image" align="left">
<a href="<?php header("Content-type: image/jpeg"); echo $rec['image']; ?>">
<img src="<?php echo $rec['image']; ?>" width="150" border="0"/>
</a>
</div><!-- image -->
You normally don't put the actual image contents in the src= attribute of the image tag. Instead, you point to the URL of an image file.
(There are ways to include the image source directly in the HTML, but it doesn't work consistantly with all browsers, and you still won't have your <a> link working properly.
Instead, the best way to do this is to create a separate PHP file to serve the image.
Your HTML:
<div class="image" align="left">
<img src="myimage.php?key=<?php echo($key) ?>" width="150" border="0"/>
</div><!-- image -->
myimage.php:
<?php
header("Content-type: image/jpeg");
$key = $_GET['key'];
// todo: load image for $key from database
echo $rec['image'];
You're trying to put the image data inline inside the content. The only feasible way to do this is via a Data URI data URI. Something like:
<img src="data:image/jpeg;base64,<?= base64_encode($rec['image']) ?>" width="150" border="0" />
However, what you probably want to do is put it into a separate script. So your HTML would be:
<img src="showimage.php?id=XXX" width="150" border="0" />
And your showimage.php script would be:
<?php
// Get $rec from database based on the $_GET['id']
header('Content-Type: image/jpeg');
echo $rec['image'];
?>
I've done something like that retrieving blob from my database in another way that you may find useful, here is the code example.. see if it suits your needs and if you needed anymore help let me know.
while ($row = mysql_fetch_array($hc_query2)) {
$title = $row['title'];
$text = $row['text'];
$image = $row ['image'];
$output ='<div class="HCInstance"><img src="data:image/jpeg;base64,' . base64_encode($image) . '" alt="High Council" width="100px" height="100px"/>
<div class="HCHeader"><h2>'.$title.'</h2></div><br/><div class="HCDetails"><p>'.$text.'</p></div></div>';
echo $output;
}

Can't get image to display in dynamic pages

I can't seem to get my image to display properly. Previously, I have used the following code snippet and it worked perfectly.
catalog.php (worked perfectly):
<p class="image">
<a href="synopsis.php?id=<?php echo $row['id']; ?>">
<img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
</a>
</p>
synopsis.php (not displaying image at all):
<?php
$id = $_GET['id'];
...?>
<p class="image">
<img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>" width="250" height="400" />
<?php echo $row['synopsis']; ?>
</p>
where getimage.php:
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['dvdimage_path']);
?>
Any idea why can't I display this image?
EDIT 1:
So after debugging, I got an error message:
Undefined index: id in C:\xampp\htdocs\synopsis.php on line 106
so i went to add the following code into the php code just before echo $row['id']:
<p>getImage.php?id=<?php error_reporting(0); echo $row['id']; ?></p>
However,
the paragraph i got was just getImage.php?id=.
Then, i went into synopsis.php -> <img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>
and changed that into:
<img border="0" class="floatleft" src="getImage.php?id=2">
Again, same problem happens, where i can't get the specific image out.
I suspect something is wrong with my getimage.php file. However, this getimage.php file has been working fine for other pages when i use the snippet.
My requirements are very simple:
In catalog.php, i populate images and text from dvd database using a while loop. Then, each of these images has got their specific primary ID. when i click the the images, they will go to the link: synopsis.php?id="primaryid" Then, using this "primaryid" i should be able use getimage.php?"primaryid" to generate an image on synopsis.php page.
EDIT 2:
actually, i made a syntax error somewhere. So this line:
<img border="0" class="floatleft" src="getImage.php?id=2">
is working perfectly, this means the fault lies in somewhere that i cant echo 'id' out correctly.
EDIT 3:
I have included the links to the relevant source code:
catalog.php
synopsis.php
getimage.php
sortmenu.css
style.css
database in xml format
Questions to ask yourself:
Is it really required that you use a php script to mimic the image? If not, just use the image path.
Is there any output before the header(); function in the getimage.php file? Even just a space before the
Is the image actually a JPEG?
Are there any errors coming up when you go to getimage.php?id=ID in your browser?
In synopsis.php you are getting the id from the querystring but then trying to use a database value. I cant see all of your code so im posting solutions to cover two scenarios
src="getImage.php?id=<?php echo $id; ?>"
or
src="getImage.php?id=<?php echo $row[$id]; ?>"

Categories