2 function curl_redirect_exec($ch) {
3 curl_setopt($ch, CURLOPT_HEADER, TRUE);
4 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
5 $data = curl_exec($ch);
6 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7 if ($http_code == 301 || $http_code == 302) {
8 list($header) = explode("\r\n\r\n", $data, 2);
10 //this part has been changes from the original
11 preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
12 $url = trim(str_replace($matches[1],"",$matches[0]));
14 $url_parsed = parse_url($url);
15 if (isset($url_parsed)) {
16 curl_setopt($ch, CURLOPT_URL, $url);
17 return curl_redirect_exec($ch);
23 if(isset($_GET['imgurl']))
25 $url = $_GET['imgurl'];
26 $ch = curl_init($url);
27 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE); //301&302
28 $ret = curl_redirect_exec($ch);
29 $Httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
30 $Hsize = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
32 if($Httpcode == '200')
34 $header = substr($ret,0,$Hsize);
35 $pat = '/(Content-Type:\s?image\/\w+)/i';
36 $matchRet = preg_match_all($pat,$header,$m);
40 $ret = substr($ret,$Hsize);
46 echo 'image not found';
51 echo 'image loading error, code: '.$Httpcode;