4

I want to have a PNG picture, but when accessing it, it runs a PHP script, the PHP script should decide what picture to send (using some if statements and whatever). Then the PHP script should read the image file from somewhere on my web server and output it.

Here is the issue, if I get a .png file, and put PHP code in it, it won't work, however, if I use the .php extension, it works, and I can even embed the image into other websites, and the PHP can decide what image to send, but if I want to view that image directly (copy it's URL into my address bar) it doesn't work, it gives me the images plain contents (random jibberish).

Anyone know what to do?

Also This is my first question on Stack Overflow - please tell me if I am doing something wrong.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 2
    *if I get a .png file, and put PHP code in it, it won't work* -- I don't understand. What do you mean? – Amal Murali Aug 15 '13 at 12:31
  • Use a proper content-type header. – Aneri Aug 15 '13 at 12:33
  • Are you asking for a solution to show images with PHP or are you describing an issue with a system already in place? If you have code then please show it. – MonkeyZeus Aug 15 '13 at 12:34
  • _please tell me if I am doing something wrong_ Post some of the code you've already tried, and try some Google searches too. You might find an answer faster than asking a new question. – TecBrat Aug 15 '13 at 12:39

4 Answers4

9

You need to send Content-Type headers.

For png:

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

For others change png to jpg or gif or bmp or whatever.

Please note that header() function must be used before anything is written to output.

potashin
  • 44,205
  • 11
  • 83
  • 107
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
2

First, make sure you have your image image.png somewhere accessible to php.

Then create a php script image.php:

<?php
header('Content-Type: image/png');
readfile('image.png');

The script now acts like it was a PNG image.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
2

Simplest version I know...

<?php
header('Content-Type: image/png');
if(whatever)
  {
    $image=your_image_select_function();
  }
// as suggested by sh1ftst0rm with correction of unmatched quotes.
header('Content-Disposition: inline; filename="'.$your_name_variable.'"');
readfile($image);
?>

Then, you treat it like an image file. That is, if this is "pngmaker.php" then, in your HTML document, you do

<img src="pngmaker.php">

You can even do

<img src="pngmaker.php/?id=123&user=me">
TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • As Cobra mentioned, `$image` must be accessible to the PHP script. You may need to make that `'/path/to/images/'.$image` – TecBrat Aug 15 '13 at 12:44
2

It sounds like you know how to send the image, your issue is that you want the URL to look like it's a PNG image.

There are a couple of things you can do. First, if your web server supports URL rewriting (like Apache's mod_rewrite module), you can use a rewrite rule so that the user access the script as something like http://example.com/generated_image.png but your server will translate/rewrite this URL to point directly to your PHP script, so something like /var/www/image_generator.php.

Another option would be to actually name your script "generated_image.png" but force your webserver to treat it like a PHP script. For instance, in Apache you could try something like:

<Location /generated_image.png>
    ForceType application/x-httpd-php
</Location>

As a final note, if you're not actually worried about the URL, but worried about the file name that is used if the user decides to save it to disk, you can simply use the Content-Disposition HTTP header in your response. In PHP it would look something like this:

<?php
header("Content-Disposition: inline; filename="generated_image.png");
?>

With that, it doesn't matter what the URL is, if the user saves the image through their web browser, the web browser should offer "generated_image.png" as the default filename.

brianmearns
  • 9,581
  • 10
  • 52
  • 79
  • Eh, I just re-read the question more carefully, and it sounds like I'm answering the wrong thing. I'll leave it for now because it might still be useful. If anyone wants me to remove it, I will. – brianmearns Aug 15 '13 at 12:49
  • This won't help. Threating .png as php script via rewrite will leave content-type the same, so the browser still wont threat it as an image. At least most of browsers. – Flash Thunder Aug 15 '13 at 13:02
  • I know. If you look at my comment above yours, I misunderstood the question, but left it here in case it is useful anyway. – brianmearns Aug 15 '13 at 13:31