This a PHP Class useful if you need to download images from a remote location. You can choose between 2 methods to retrieve the image: using GD or cURL.
Here is the class (I will explain you how it works below):
<?php class GetImage { var $source; var $save_to; var $set_extension; var $quality; function download($method = 'curl') // default method: cURL { $info = @GetImageSize($this->source); $mime = $info['mime']; // What sort of image? $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; // Best Quality: 100 $quality = isSet($this->quality) ? $this->quality : 100; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; // Compression Level: from 0 (no compression) to 9 $quality = isSet($this->quality) ? $this->quality : 0; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; case 'vnd.wap.wbmp': $image_create_func = 'ImageCreateFromWBMP'; $image_save_func = 'ImageWBMP'; $new_image_ext = 'bmp'; break; case 'xbm': $image_create_func = 'ImageCreateFromXBM'; $image_save_func = 'ImageXBM'; $new_image_ext = 'xbm'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } if(isSet($this->set_extension)) { $ext = strrchr($this->source, "."); $strlen = strlen($ext); $new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext; } else { $new_name = basename($this->source); } $save_to = $this->save_to.$new_name; if($method == 'curl') { $save_image = $this->LoadImageCURL($save_to); } elseif($method == 'gd') { $img = $image_create_func($this->source); if(isSet($quality)) { $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); } } return $save_image; } function LoadImageCURL($save_to) { $ch = curl_init($this->source); $fp = fopen($save_to, "wb"); // set URL and other appropriate options $options = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough) curl_setopt_array($ch, $options); curl_exec($ch); curl_close($ch); fclose($fp); } } ?>
How it works?
After initializing the class and the necessary variables are set, download() is called. Here the GetImageSize() function is used to obtain information regarding the image. We are interested in the mime-type from which he have to extract the type of image. Based on $type we will set some variables such: $image_create_func, $image_save_func, $new_image_ext (useful if $set_extension is set to ‘true’) and even $quality for ‘JPEG’ and ‘PNG’. The first 2 variables are needed in case we will use GD to download the image.
If $this->set_extension is not set, the saved image will have exactly the same name and extension (if any) as the remote image. If it is set, then we add $new_image_ext to the file name (could be the same as the remote one):
if(isSet($this->set_extension)) { $ext = strrchr($this->source, "."); $strlen = strlen($ext); $new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext; } else { $new_name = basename($this->source); }
Set the full path where the image will be saved (filename + save folder):
$save_to = $this->save_to.$new_name;
Now, some functions would be called based on the $method value (could be ‘gd or ‘curl’):
if($method == 'curl') { $save_image = $this->LoadImageCURL($save_to); } elseif($method == 'gd') { $img = $image_create_func($this->source); if(isSet($quality)) { $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); } }
If we choose ‘gd’ the LoadImageCURL() function will be called with one argument, $save_to, representing the full path where the image would be saved.
The curl_unit() is called with the ‘url’ argument ($this->source). The session is initialized and returns a cURL handle for use with curl_setopt_array(), curl_exec() and curl_close() functions. The next thing to do is to open the remote image for writing using fopen():
$ch = curl_init($this->source); $fp = fopen($save_to, "wb");
The function curl_setopt_array() sets multiple options for a cURL transfer without repetitively calling curl_setopt():
// set URL and other appropriate options $options = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough) curl_setopt_array($ch, $options);
Now we need to grab the URL and pass it to the browser:
curl_exec($ch);
The last things to do is closing both the cURL resource (to free up system resources) and the open file pointer:
curl_close($ch); fclose($fp);
The image is now saved in the specified folder.
Now, let’s see how we can download the image using the ‘gd’ method! We’ll call the function needed to create the image from URL ($image_create_func). It’s used to return an image identifier representing the image obtained from the given URL:
// possible values: ImageCreateFromJPEG, ImageCreateFromPNG etc. $img = $image_create_func($this->source);
The next function used ($image_save_func) is used to create the image from the given image ($img). Another aspect here is the $quality variable. For JPEG images, the value ranges from 0 (worst quality, smaller file) to 100 (best quality). The default is the default IJG quality value (about 75). As for the PNG images, the range is from 0 (no compression) to 9.
if(isSet($quality)) { // possible values: ImageJPEG, ImagePNG etc. $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); }
Here’s an example of how you can use this class:
<?php include_once 'class.get.image.php'; // initialize the class $image = new GetImage; $image->source = 'http://l.yimg.com/a/i/ww/beta/y3.gif'; $image->save_to = 'images/'; // with trailing slash at the end $get = $image->download('gd'); // using GD if($get) { echo 'The image has been saved.'; } ?>
NOTE: Make sure the folder where the image will be saved is writable (chmod 0777).
Happy coding!