Forum Moderators: coopster

Message Too Old, No Replies

php image file

not generating, but displaying a jpg file

         

sebbothebutcher

5:08 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



hi!
on my homepage i've got a gallery of images, and i want all those images to be displayed by a file (e.g. image.php) that is actually an image depending on the parameters you pass to that file.
a solution that works, but unfortunately not with every file is as simple as this example:

<?php
include($_GET["image"]);
?>

but it won't work if the image contains certain illegal chars (php will print an error)...
another thing i've tried, was also a very primitive one:
looping through the image file and displaying it line for line, but this wouldn't work either (surprise, surprise!)

do you have any suggestions how something like this works professionally?

thanks in advance!

ps: the php file DOES NOT GENERATE the image!
it's simply a wrapper that can be used to prevent displaying the image if its referer is not wanted, for example, ...

jusdrum

6:57 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



Here's an example of how you would do it:

$Image = $_GET['Image']; // you can strip out bad characters here

if (!file_exists($Image))
{
// Do something to show a "no image" error or something
}

list(,,$ImageType) = getImageSize($Image); // get image type

switch($ImageType) // Determine Content-Type for header
{
case 1:
$ContentType = "image/gif";
break;
case 2:
$ContentType = "image/jpeg";
break;
case 3:
$ContentType = "image/png";
break;
}

header("Content-Type: $ContentType"); // send header

@readfile($Image); // output file

sebbothebutcher

7:27 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



thanks jusdrum!
that was exactly the thing i needed! (forgot about that header thing and stuff!)