-1

as most of you probably know, we can serve images from PHP using constructs like this:

RewriteRule ^images/([a-zA-Z0-9]+)\.jpg script.php?image=$1

And then in PHP:

header('Content-Type: image/png');
$image=imagecreatefromjpeg($pathComputedSomewhereElse);
imagejpeg($image);

That's dead simple, but that's not my problem - i simply don't want to confuse you.

My question is:

How can I, if it is possible, serve such image directly, using PHP only to fetch image path? I want Apache to do the work, not PHP reading file and outputting data as binary stream. Prototype markup would be as follows [same .htaccess]:

header('Content-Type: image/png');
header('Location: '.$pathComputedSomewhereElse);
Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • Is the client allowed to know the `$pathComputedSomewhereElse`, or does it have to be secret? Is this a path on the same server? – Pekka Oct 27 '10 at 09:51
  • This is a path on the same server, but for SEO reasons we want to to hide exact image path and serve only "nice" one. – Tomasz Kowalczyk Oct 27 '10 at 11:45

4 Answers4

1

If you have mod_xsendfile installed in your Apache, you can do exactly what you need without the client seeing the path they are redirected to.

header("X-Sendfile: /path/to/your/filename");
header("Content-type: image/jpeg");

See a background article here

Alternatively, if the client is allowed to see the new URL, I don't see why using a 302 header wouldn't work:

header("Location: http://example.com/path/to/your/imagefile.jpg");
die();
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • that's good idea, i'll check if that module is instaled and enabled. – Tomasz Kowalczyk Oct 27 '10 at 10:01
  • @Tomasz yeah. Alternatively, as I said, `header("Location:")` should work as well, but it will reveal the URL to the user – Pekka Oct 27 '10 at 10:03
  • unfortunately it came out later that we don't have access to client's server so we had to simple rename files and change references in code, but still thanks for helping me - accepted, sir! – Tomasz Kowalczyk Oct 30 '10 at 10:30
0
header("Content-Type: image/jpeg\n");
header("Content-Transfer-Encoding: binary");
$fp=fopen("images/$image.jpg" , "r");
if ($fp)
fpassthru($fp);

using the gdlib functions for this is overkill :-)

if you dont want to modify the file you use:

header("Content-Type: image/jpeg\n");
header("Content-Transfer-Encoding: binary");
readfile("images/$image.jpg" , "r");
DoXicK
  • 4,784
  • 24
  • 22
0

Why not try..

RewriteRule ^images/([a-zA-Z0-9]+)\.jpg /new/image/directory/$1.jpg [L]
fire
  • 21,383
  • 17
  • 79
  • 114
0

in htaccess

RewriteRule ([^.]+)\.jpg$ script.php?image=$1

$_REQUEST['image'] contains path without ".jpg", add and gather path ;)

nerkn
  • 1,867
  • 1
  • 20
  • 36