Mitchell Nelson
Mitchell Nelson

Reputation: 63

Using PHP to send a certain image

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.

Upvotes: 4

Views: 10342

Answers (4)

Flash Thunder
Flash Thunder

Reputation: 12036

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.

Upvotes: 9

TecBrat
TecBrat

Reputation: 3719

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

Upvotes: 2

brianmearns
brianmearns

Reputation: 9967

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.

Upvotes: 2

Cobra_Fast
Cobra_Fast

Reputation: 16061

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.

Upvotes: 2

Related Questions