php image not showing
i have following php code which display company logo
<?php
$key=$_GET['key'];
/// key process ///
/////开发者_StackOverflow///////////////
?>
<img src=images/logo.jpg" />
when i run this php file this show logo.jpg but when i call this from
<img src="../temp.php?key=123" width="438" height="100" />
it didn't show image
actually what i want to track this image.
Thanks
If you want to use something like this :
Then temp.php
should actually :
- send HTTP-headers for some image content-type
- and send the actual content of the image.
- and not any HTML code
This is because the browser expects that the URL pointed to by the src
attribute of the <img>
tag actually corresponds to a valid image -- and not some HTML text.
For instance, temp.php
could contain something like this :
<?php
$key = $_GET['key'];
// TODO : security check on $key !!!
$file = 'images/logo-' . $key . '.jpg';
header('Content-type: image/jpeg');
readfile($file);
?>
As a couple of sidenotes :
- You must send the right
Content-type
HTTP header ; which means you must know if your image is a gif, jpeg, png, ... - You must check if
$_GET['key']
is correct, to not send the content of an un-wanted file ! - There should be absolutly no white-space character before or after the
<?php ... ?>
tags : the only output must be the content of the image.
To make this work, in your temp.php
, you need to read and output the actual image data (preceded by header('Content-type: image/jpeg');
.
An HTML file containing an image tag can't be used as the src
for another image tag.
Instead of your PHP script outputting an additional img
tag, you would need to have it read the binary image data and output that, along with the appropriate Content-Type headers.
temp.php should be something like:
header('Content-Type: image/jpeg');
echo file_get_contents($the_path_to_image);
Well, the PHP script is returning HTML output, not an image. If you want your temp.php file to return an image that can be used within an image element, you need to use PHP-GD to create an image, import/copy the image to it, then output the image in JPEG with proper image headers.
Is it just me or... If the code is indeed what you have
<img src=images/logo.jpg" />
should be
<img src = "images/logo.jpg">
It has no opening quote ?
It is because the folder path for img
tag should not start with "/" to locate the Images or any relevant folder in which Images are there. It should be like this: <img src="Images/today_picks_1.jpg" alt="" >
.
精彩评论