tags:

views:

75

answers:

4

I want a PHP to be able to send 1 of 3 images, depending on a $_GET[] parameter. I have the images as three separate PNGs right now, and would like the PHP script to have those embedded in it, then return the specified image. So, I want one PHP script instead of 3 images. Is this possible? I don't need to create special images on the fly, just print out one of those. Thanks!

+1  A: 

Yes, it's possible.

Do this:

  1. Write a php script deciding which image to output.
  2. Set the appropriate headers with header() function (I.e. Content-type)
  3. Open the file, read it and send it to the output stream.

Remember that you will probably lose any benefits from browser's cache with this approach...

Pablo Santa Cruz
Wouldn't that still require having the images as seperate files? Is there any way to embed them in the script?
Leticia Meyer
why is he going to loose browser cache? Images are still located by unique URL with GET parameters in it.
doc
Why would you want to embed an image into a script?
Pablo Santa Cruz
Using "data:" uris you can make it easy.
LatinSuD
I'm doing a contest where I have to make a 1-page website, no external files. I have a couple images i have to have, so that's why I was wondering if I could embed them. Is it possible to embed them?
Leticia Meyer
@Leticia Meyer cool contest! Good luck
doc
+4  A: 

If your images are in files, use PHP's readfile() function, and send a content-type header before outputting it:

<?php
$imagePaths = array(
    '1' => 'file1.png',
    '2' => 'file2.png',
    '3' => 'file3.png',
);

$type = $_GET['img'];

if ( isset($imagePaths[$type]) ) {
    $imagePath = $imagePaths[$type];
    header('Content-Type: image/png');
    readfile($imagePath);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

EDIT:
You could also embed your images in the script by encoding them e.g. as Base64, then embed them as strings in PHP, then decode it there with base64_decode to deliver them:

<?php
$imageData = array(
    '1' => '...', // Base64-encoded data as string
    ...
);

$type = $_GET['img'];

if ( isset($imageData[$type]) ) {
    header('Content-Type: image/png');
    echo base64_decode($imageData[$type]);
} else {
    header('HTTP/1.1 404 File Not Found');
    echo 'File not found.';
}
?>

You could also use PHP to encode the image on the command line. Just execute this PHP script in the command line (php script.php image1.png image2.png image3.png > output.php) and save its output, and incorporate it into your script:

<?php
$imageData = array();

foreach ($argv as $index => $imagePath)
    $imageData[(string)($index + 1)] = base64_encode(file_get_contents($imagePath));

echo '$imageData = '.var_export($imageData, true).';';
?>
Archimedix
Thanks so much Archimedix!! I'll take a look at that, it's just what I need!
Leticia Meyer
That worked! Thanks!
Leticia Meyer
A: 

A fast but not really secure nor elegant way to do it...

<?php
switch($_GET['type']){
 case "1":
    $image = "image1.png";
    break;
 case "2":
    $image = "image2.png";
    break;
 case "3":
    $image = "image3.png";
    break;
}
header('Content-Type: image/png');
readfile($image);
?>

the moral of the story: use header() and readfile() =D

pleasedontbelong
+2  A: 

I have not tested the following, but it should work. Use this script to get PHP code that will contain the image data.

To get the image:

$image = base64_encode(file_get_contents("image.png"));
// The string $image now contains your image data.

Get this (potentially big) string in your code where you want the image, for each image. Print it and copy it then paste it. Import it as a text file over the web. That's up to you.

Then, to print the image (only the image as if the PHP script were the image), do:

header("content-type: image/png");
print base64_decode($image);

Of course, you would put each image data in an array or something like that.

Let me know if it works.

eje211
It works. First I have tried it with newdoc syntax and it also worked, but this solution is better.
doc
Thanks, doc. I'm glad it worked.
eje211