Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //convert PNG to base64
- public function carImgProxy() {
- header('Access-Control-Max-Age:' . 5 * 60 * 1000);
- header("Access-Control-Allow-Origin: *");
- header('Access-Control-Request-Method: *');
- header('Access-Control-Allow-Methods: OPTIONS, GET');
- header('Access-Control-Allow-Headers: *');
- header("Content-Type: application/javascript");
- // Url params | $this->qs === $_GET
- $url = $this->qs['url']; // user IMG URL input
- $callback = $this->qs['callback']; // not in use atm
- $imageDet = getimagesize($url); //get img details
- if(!$imageDet) die(); //will be false if it's not an img?
- if(empty($imageDet[0]) || empty($imageDet[1])) die(); //check height and width
- // Retrieve file details
- $file_details = $this->get_url_details($url, 1, $callback);
- if (!in_array($file_details["mime_type"], array("image/jpg", "image/jpeg", "image/png"))) {
- print "error:Application error";
- } else {
- $re_encoded_image = sprintf(
- 'data:%s;base64,%s', $file_details["mime_type"], base64_encode($file_details["data"])
- );
- $this->noheader = true;
- $this->nofooter = true;
- $response = $this->renderView("pages/car-designer/proxy.php", ["re_encoded_image" => $re_encoded_image]);
- return $response;
- }
- }
- function get_url_details($url, $attempt = 1, $callback = "")
- {
- $pathinfo = pathinfo($url);
- $max_attempts = 10;
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);
- //curl_setopt($ch, CURLOPT_PROXY, 'username:password@host:port');
- $data = curl_exec($ch);
- $error = curl_error($ch);
- $mime_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
- if (!in_array($mime_type, array("image/jpg", "image/jpeg", "image/png")) && $max_attempts != $attempt)
- {
- return false;
- //return get_url_details($url, $attempt++, $callback);
- }
- return array(
- "pathinfo" => $pathinfo,
- "error" => $error,
- "data" => $data,
- "mime_type" => $mime_type
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement