How to capture image URL and return cached local or original remote data?

I’m trying to cache a giant and slow source of photos but on an as-needed basis.

I was thinking of using mod rewrite and php routing to capture a url like /photos/http://original.com/foo.jpg and then check my local db or hd for foo.jpg and return that or some how tell the browser to use the original http://original.com/foo.jpg meanwhile PHP downloads foo.jpg locally so the next time that photo is requested it comes from my server which is much faster than the original server.

Can anyone give advice on how to go about doing this? Would the PHP involved be just as slow as the originating slow server and not worth doing?

You could do a search locally (and in the db) and if it doesn’t exist, return the url where the image could be found, but before you return that, initiate a forking process to grab the image.

return the url where the image could be found,

How exactly would that be done in a way to tell the browser to actually pull the data from that url instead of having to read the data via my server and push it to the browser… an http redirect header maybe?

but before you return that, initiate a forking process to grab the image.

How exactly do you fork a process in php? In the past I’ve managed to get “fire and forget” http requests to another php file to work but it was often buggy.

How would you know where to get the url to begin with?

Just send that url to the broswer and you start a background process to retrieve it for the next time.

Well i was thinking of having a separate route for photos so they can be dealt with one by one instead of doing the check and the download for all scores of photos in the template/controller code before sending the html of the whole page. So the page would load with urls like /photos/00004ac3.L03 and the route code for /photos/* would be run individually for each photo. The route code would know how to turn 00004ac3.L03 into the 369 character long real URL.

Thanks. I think I have something that works now.

<?php
$_GET['rs_src'] = '0000'.dechex($_GET['rs_src']).'.L01';

$rs = 'http://ibrea.realtyserver.com/photo_server.php?btnSubmit=GetPhoto&board=bahamas&name='.$_GET['rs_src'].'&failover=portal_Blank.gif&key1='.getenv('RLSV_KEY');

//-----------------------------
//https://www.binarytides.com/php-output-content-browser-realtime-buffering/
//-----------------------------
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);
// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);
// Redirect to the target url
header("Location: $rs", true, 301);
// recommended to prevent caching of event data.
header('Cache-Control: no-cache');
echo 'some text to trigger php buffer flushing';
// Flush the system and user space buffers
ob_flush();
flush();

// Do other stuff after the redirect has been sent.
sleep(8);
$fp = fopen('./test7'.rand(10,100), 'a');
fwrite($fp, '123');
fclose($fp);
exit;
?>
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service