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);
If you have
mod_xsendfileinstalled in your Apache, you can do exactly what you need without the client seeing the path they are redirected to.See a background article here
Alternatively, if the client is allowed to see the new URL, I don’t see why using a
302header wouldn’t work: