-
-
Save dmdotin/8c336d3fb4b2f48cdd1f25278dc02df5 to your computer and use it in GitHub Desktop.
Curl get image and data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class curl_get_image{ | |
private $image_name = ""; | |
private $data = ""; | |
private $dir = ""; | |
private $details = array(); | |
public function init($dir = "/images/"){ | |
$this->dir = $dir; | |
$details['image_location'] = $this->dir; | |
$details['image_name'] = ""; | |
$details['image_type'] = ""; | |
$details['url'] = ""; | |
} | |
private function get_data($url){ | |
$ch = curl_init(); | |
$timeout = 5; | |
curl_setopt($ch,CURLOPT_URL,$url); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
private function store_image($name, $type, $data){ | |
if(!file_put_contents($this->dir.$name, $data)) | |
return false; | |
else | |
return true; | |
} | |
public function get_image($url, $rand=false){ | |
$name = end(explode("/", $url)); | |
$type = end(explode(".", $name)); | |
$rand ? $name = $this->gen_secure_id(30).".".$type : $name = $name; | |
$details = array(); | |
$details['image_location'] = $this->dir; | |
$details['image_name'] = $name; | |
$details['image_type'] = $type; | |
$details['url'] = $url; | |
$this->details = $details; | |
$return = $this->store_image($name, $type, $this->get_data($url)); | |
if($return) | |
return true; | |
else | |
return "failed to store image"; | |
} | |
private function gen_secure_id($size){ | |
$char = range("a", "z"); | |
$numb = range(1, 9); | |
$merge = array_merge($char, $numb); | |
$string = ""; | |
for($x=0; $x != $size; $x++){ | |
$n = ""; | |
$n = rand(0, count($merge) -1); | |
$string .= $merge[$n]; | |
} | |
return trim($string); | |
} | |
public function return_image_location(){ | |
return $this->details['image_location']; | |
} | |
public function return_image_name(){ | |
return $this->details['image_name']; | |
} | |
public function return_image_type(){ | |
return $this->details['image_type']; | |
} | |
public function return_image_url(){ | |
return $this->details['url']; | |
} | |
} | |
$image = new curl_get_image(); | |
$image->init("dump/"); | |
echo $image->get_image("http://si0.twimg.com/profile_images/1378674922/tf2-heavy.jpg", true); | |
echo "<br />"; | |
echo $image->return_image_location(); | |
echo "<br />"; | |
echo $image->return_image_name(); | |
echo "<br />"; | |
echo $image->return_image_type(); | |
echo "<br />"; | |
echo $image->return_image_url(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment