0

i have following php code which display company logo

     <?php
     $key=$_GET['key'];
     /// key process ///

     ////////////////////
     ?>
     <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

3
  • What does temp.php do? What do you get when you call the image directly in your browser? Commented Mar 2, 2010 at 18:29
  • That's because it is setting the img tag to read <img src="<img src=images/logo.jpg" />" width="438" height="100" />" which is of course, invalid html. Commented Mar 2, 2010 at 18:32
  • @Salsa no, it's not: it's setting the image tag as <img src="../temp.php?key=123"... exactly as posted - but then trying to load "<img src=images/logo.jpg" />" (which it reads from the Uri) as binary image data. Commented Mar 2, 2010 at 19:09

6 Answers 6

5

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.
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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);

Comments

2

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.

Comments

0

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 ?

Comments

0

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="" > .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.