0

Is it possible to load a image using:

<img src="image.php?image_id=1">

?

for image.php:

$image_id = $_GET['image_id']; 
echo "image".$image_id.".png"; 
3
  • Don't you mean src? I just corrected it. Commented May 17, 2011 at 8:56
  • answer to your question is yes Commented May 17, 2011 at 8:57
  • Are you sure you want to use server's resources to read then echo an image? I wouldn't be so sure about that... Commented May 17, 2011 at 9:34

6 Answers 6

5

you must return a valid image, use file_get_contents or readfile for get the conent of image then output to browser

 header("Content-Type:image/png");

    $image_id = $_GET['image_id']; 

    if(is_file($file = "image".$image_id.".png") ||  is_file($file = "no_image.png"))
        readfile($file); 
Sign up to request clarification or add additional context in comments.

3 Comments

Don't serve up images with a text/html content-type.
output still needs the headers
Or, if this is not to hide the true image location, or if the image is not created on the fly, one could easily use the Location header to redirect to the image. (If it's accessable through http)
0

If you have the png contents in the database, follow the other answers. If all you need is the id from the database, simply <img src="image<?=$image_id?>.png">

Comments

0

No, this is not possible that way. You would have to write the contents of the image file to the output stream instead.

Comments

0

Is it possible to load a image use: <img src="image.php?image_id=1">

Yes. URLs are URLs. It is content-type that determines content, not any characters in the URL itself.

$image_id = $_GET['image_id']; echo "image".$image_id.".png";

Not like that. You either have to read the image file in and then output it with a suitable content-type, or redirect (with a location header) to a static image.

Comments

0

You have to change the header content-type and print the contents of the image file. Here is an example for your image.php file:

$image_id = $_GET['image_id']; 
$filename = "image".$image_id.".png"; 
header('Content-Type: image/png');
print file_get_contents($filename);

Comments

0

one liner....

echo (file_exists($_GET['image_id'])) ? header("Content-Type:image/png").readfile($_GET['image_id']).die() : false;

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.