Jump to content

How to make a php page act like an image?


acroporas

Recommended Posts

I want to hide the real location and file name of the images used on my site.

instead of
[code]<img src="/picture.jpg">[/code]

I would use.
[code]<img src="/image.php">[/code]

Right now I have something like the following for image.php:
[code]<? header("Location: http://www.server.com/picture.jpg"); ?>[/code]

It works in that picture shows up as it on the page when I use. <img src="/image.php">

But if you enter http://www.server.com/image.php into the address bar, rather than just showing the picture, the address is automatically changed to the pictures real address (http://www.server.com/picture.jpg)

Is there a way to make image.php stay in the address bar, and just show the picture as if image.php was actually a .jpg file? 
Link to comment
https://forums.phpfreaks.com/topic/27664-how-to-make-a-php-page-act-like-an-image/
Share on other sites

You could try something like this...

[code]
<?php

  header('Content-type: image/jpeg');
  readfile('picture.jpg');

?>
[/code]

You can imagine how passing GET (or POST) variables to such a script could enable to you to select from your various images...

EDIT: Tested and Works.  :)
Thanks, yes, that does exactly what I wanted it to.  And yes, I was planning on using the same script to display all images, I was just trying to get it to work with one image first.  Here is the full code I will use.

[code]
Two numeric variables are passed via GET.  $Gallery and $Image

<?
$count=0;
foreach( Glob("*") as $poten )
  {
if ( is_dir($poten) )
  {
$count += 1;
$folders[$count] = $poten;
  }
  }
sort($folders);

if ( is_dir($folders[$Gallery]) )
  {
chdir($folders[$Gallery]);

$dir=Glob("{*.jpg,*.JPG}",GLOB_BRACE);
sort($dir);

if ( is_file("$dir[$Image]") )
  {
header('Content-type: image/jpeg');
readfile("http://server.com/images/$folders[$Gallery]/$dir[$Image]");
  }
else
  {
header('Content-type: image/jpeg');
readfile("http://server.com/images/badimage.jpg");
  }
  }
else
  {
header('Content-type: image/jpeg');
readfile("http://server.com/images/badimage.jpg");
  }

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.