Advertisement
Guest User

st

a guest
Aug 23rd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2.  
  3. //convert PNG to base64
  4. public function carImgProxy() {
  5.         header('Access-Control-Max-Age:' . 5 * 60 * 1000);
  6.         header("Access-Control-Allow-Origin: *");
  7.         header('Access-Control-Request-Method: *');
  8.         header('Access-Control-Allow-Methods: OPTIONS, GET');
  9.         header('Access-Control-Allow-Headers: *');
  10.         header("Content-Type: application/javascript");
  11.  
  12.         // Url params |  $this->qs === $_GET
  13.         $url = $this->qs['url']; // user IMG URL input
  14.         $callback = $this->qs['callback']; // not in use atm
  15.  
  16.  
  17.         $imageDet = getimagesize($url); //get img details
  18.         if(!$imageDet) die();   //will be false if it's not an img?
  19.         if(empty($imageDet[0]) || empty($imageDet[1])) die();   //check height and width
  20.  
  21.  
  22.         // Retrieve file details
  23.         $file_details = $this->get_url_details($url, 1, $callback);
  24.  
  25.         if (!in_array($file_details["mime_type"], array("image/jpg", "image/jpeg", "image/png")))  {
  26.             print "error:Application error";
  27.         } else  {
  28.             $re_encoded_image = sprintf(
  29.                 'data:%s;base64,%s', $file_details["mime_type"], base64_encode($file_details["data"])
  30.             );
  31.  
  32.             $this->noheader = true;
  33.             $this->nofooter = true;
  34.             $response = $this->renderView("pages/car-designer/proxy.php", ["re_encoded_image" => $re_encoded_image]);
  35.  
  36.             return $response;
  37.         }
  38.  
  39.  
  40.     }
  41.  
  42.     function get_url_details($url, $attempt = 1, $callback = "")
  43.     {
  44.         $pathinfo = pathinfo($url);
  45.  
  46.         $max_attempts = 10;
  47.  
  48.         $ch = curl_init($url);
  49.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  50.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  51.         curl_setopt($ch, CURLOPT_HEADER, 0);
  52.         curl_setopt($ch, CURLOPT_NOBODY, 0);
  53.         //curl_setopt($ch, CURLOPT_PROXY, 'username:password@host:port');
  54.         $data = curl_exec($ch);
  55.         $error = curl_error($ch);
  56.  
  57.         $mime_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  58.  
  59.         if (!in_array($mime_type, array("image/jpg", "image/jpeg", "image/png")) && $max_attempts != $attempt)
  60.         {
  61.            return false;
  62.            //return get_url_details($url, $attempt++, $callback);
  63.         }
  64.  
  65.         return array(
  66.             "pathinfo" => $pathinfo,
  67.             "error" => $error,
  68.             "data" => $data,
  69.             "mime_type" => $mime_type
  70.         );
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement