Background
- I am building an image search for oscobo, a privacy search engine.
- We use a Bing URL to fetch thumbnails to display to the user on the image search results page. For example: https://oscobo.com/image.php?q=london.
- We do not want to connect the client to Bing directly (as in the above example) but intend to use a reverse proxy script that uses cURL to fetch the image and then send it back to the browser.
- The script works well when we know what the file type is (e.g. jpg) and we can pick the correct content-type for the Header accordingly before we echo the image back to the browser.
- The problem we have is that the URL to Bing does not contain the filetype, it looks along the lines of http://tse3.mm.bing.net/th?id=OIP.M1a65dd11fb2b159f4fe44eba7dcfa0a6H0&pid=15.1&H=90&W=160
Question
How can we modify this script to work with the URL that does not display the file type?
Any ideas or input much appreciated.
The PHP on the page
$html = '';
$images = $results->bossresponse->images->results;
foreach ($images as $image) {
$theurl = "{$image->thumbnailurl}";
$theurl = base64_encode($theurl);
$reverseurl = "reverseproxy.php?url=$theurl";
$html .= "<a href=\"{$image->clickurl}\"> <img class=border src=\"$reverseurl\" height=\"140px\"></a>";
}
echo $html;
The reverse script reverseproxy.php
<?php
$file = base64_decode(@$_GET['url']);
$aFile = end(explode('.' , $file));
if($aFile == 'jpg' or $aFile == 'jpeg'){
header('Content-Type: image/jpeg');
}elseif($aFile == 'png'){
header('Content-Type: image/png');
}elseif($aFile == 'gif'){
header('Content-Type: image/gif');
}else{
die('not supported');
}
if($file != ''){
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: maxage=". $cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_expire).' GMT');
//The cURL stuff...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$file");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$picture = curl_exec($ch);
curl_close($ch);
//Display the image in the browser
echo $picture;
}
exit;
?>