Advertisement
terorama

PHP / Image Loader

Apr 12th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. //--------------------------------------
  4.  if (isset($_GET['url'])) {
  5.  
  6.       $ch = curl_init();
  7.       //curl_setopt($ch, CURLOPT_HEADER,1);
  8.  
  9.       curl_setopt($ch, CURLOPT_URL, $_GET['url']);
  10.       curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  11.       curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
  12.       curl_setopt($ch, CURLOPT_USERAGENT,
  13.        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
  14.  
  15.  
  16.       curl_setopt($ch,CURLOPT_ENCODING, 1);
  17.       curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  18.  
  19.       $teststr = curl_exec($ch);
  20.  
  21.      if(curl_exec($ch) === false)
  22.        {
  23.        echo 'Ошибка curl: ' . curl_error($ch);
  24.       }
  25.  
  26.       curl_close($ch);
  27.       header('content-type: image/jpeg');
  28.       $seconds_to_cache = 3600*24*60;
  29.       $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
  30.       header("Expires: $ts");
  31.       header("Pragma: cache");
  32.       header("Cache-Control: max-age=$seconds_to_cache");
  33.  
  34.       echo $teststr;
  35.       exit();
  36.  
  37.    }
  38.  
  39. //--------------------------------------
  40.   if (isset($_GET['thumb'])) {
  41.  
  42.       $url = $_GET['thumb'];
  43.  
  44.       //$time_prev = microtime(true);
  45.  
  46.       $ch=curl_init();                  
  47.       curl_setopt($ch, CURLOPT_URL, $url);
  48.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  49.       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  50.                            
  51.       curl_setopt($ch, CURLOPT_USERAGENT,
  52.       'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)'.
  53.                                   ' Chrome/20.0.1132.57 Safari/536.11');
  54.                                  
  55.       $data=curl_exec($ch);
  56.       curl_close($ch);
  57.  
  58.       /*$time= microtime(true)-$time_prev;
  59.       echo sprintf("time: %f",$time).'<br/>';
  60.       $time_prev = microtime(true);*/
  61.  
  62.       list($w,$h)=getimagesize($url);
  63.  
  64.       /*$time= microtime(true)-$time_prev;
  65.       echo sprintf("time size: %f",$time).'<br/>';
  66.       $time_prev = microtime(true);*/
  67.  
  68.       if (isset($_GET['nw'])) {
  69.        
  70.          $nw = $_GET['nw'];
  71.          $nh = $h*($nw/$w);
  72.       }
  73.        else
  74.  
  75.          if (isset($_GET['nh'])) {
  76.  
  77.             $nh = $_GET['nh'];
  78.             $nw = $w*($nh/$h);
  79.  
  80.          } else {
  81.  
  82.                $nw=300;
  83.                $nh=$h*($nw/$w);
  84.            }
  85.            
  86.       $thumb=imagecreatetruecolor($nw,$nh);
  87.       $source=imagecreatefromjpeg('data://image/jpeg;base64,'.base64_encode($data));
  88.       imagecopyresampled($thumb,$source,0,0,0,0,$nw,$nh,$w,$h);
  89.  
  90.       ob_start ();
  91.  
  92.       imagejpeg ($thumb);
  93.       $image_data = ob_get_contents ();
  94.       ob_end_clean ();
  95.  
  96.            
  97.       imagedestroy($source);
  98.       imagedestroy($thumb);
  99.  
  100.       /*$time= microtime(true)-$time_prev;
  101.       echo sprintf("time resample: %f",$time).'<br/>';
  102.       $time_prev = microtime(true);*/
  103.  
  104.       //$outstr='data:img/jpeg;base64,'.base64_encode($image_data);
  105.  
  106.       header('content-type: image/jpeg');
  107.       $seconds_to_cache = 3600*24*60;
  108.       $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
  109.       header("Expires: $ts");
  110.       header("Pragma: cache");
  111.       header("Cache-Control: max-age=$seconds_to_cache");
  112.  
  113.       echo $image_data;
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement