Skip to content

Instantly share code, notes, and snippets.

@gastonsoto
Last active December 18, 2015 01:09
Show Gist options
  • Save gastonsoto/5702192 to your computer and use it in GitHub Desktop.
Save gastonsoto/5702192 to your computer and use it in GitHub Desktop.
Download an image from a given URL and create a thumbnail.
<?php
class Download_Images {
var $source;
var $save_to;
var $set_extension;
var $quality;
function download($method = 'curl')
{
$info = @GetImageSize($this->source);
$mime = $info['mime'];
$type = substr(strrchr($mime, '/'), 1);
switch ($type)
{
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
$quality = isSet($this->quality) ? $this->quality : 100;
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
$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);
if($this->set_name){
$new_name = $this->set_name.'.'.$new_image_ext;
}else{
$new_name = basename(substr($this->source, 0, -$strlen)).'_'.time().'.'.$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);
}
}
$this->create_thumbnail($new_name,$this->save_to,$this->save_to,550);
return array("name" => $new_name, "save" => $save_image);
}
function LoadImageCURL($save_to)
{
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");
$options = array(
CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 60
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
function create_thumbnail($fname,$pathToImages,$pathToThumbs, $thumbWidth)
{
$info = pathinfo($pathToImages . $fname);
list($width, $height, $type, $attr) = getimagesize($pathToImages . $fname);
$file_type = strtolower($file_dimensions['mime']);
if ( strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg' || strtolower($info['extension']) == 'png' || strtolower($info['extension']) == 'gif' )
{
if (strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg'){
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
}elseif (strtolower($info['extension']) == 'png'){
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$convert = 1;
}elseif(strtolower($info['extension']) == 'gif'){
$img = imagecreatefromgif( "{$pathToImages}{$fname}" );
}else{
$img = imagecreatefromstring( "{$pathToImages}{$fname}" );
}
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
if ($convert == 1) {
imagealphablending($tmp_img, false);
imagesavealpha($tmp_img,true);
$transparent = imagecolorallocatealpha($tmp_img, 255, 255, 255, 127);
imagefilledrectangle($tmp_img, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagepng( $tmp_img, "{$pathToThumbs}{$fname}");
} else {
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}", 100 );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment