Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2012 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4020431 to your computer and use it in GitHub Desktop.
Save anonymous/4020431 to your computer and use it in GitHub Desktop.
Resizer on PHP
public static function Crop($source_image, $destination, $tn_w = 300, $tn_h =220, $quality = 100, $wmsource = false) {
#find out what type of image this is
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
$src_ratio = $src_w/$src_h;
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
if ($tn_w/$tn_h > $src_ratio) {
$new_h = $tn_w/$src_ratio;
$new_w = $tn_w;
} else {
$new_w = $tn_h*$src_ratio;
$new_h = $tn_h;
}
$x_mid = $new_w/2;
$y_mid = $new_h/2;
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($tn_w/2)), ($y_mid-($tn_h/2)), $tn_w, $tn_h, $tn_w, $tn_h);
#if we need to add a watermark
if($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
}
if(Imagejpeg($final,$destination,$quality)) {
return true;
}
return false;
}
public static function CropAll($path = 'public/file/img')
{
$filetypes = array('jpg','png');
//Iterator for recursive scan
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $filename => $file)
{
$pathinfo = pathinfo($file);
// get the extension
$extension = $pathinfo['extension'];
// proceed only if it matches with the extension we provide
if(basename(dirname($file))!='small' && basename(dirname($file))!='thumb' && basename(dirname($file))!='medium')
{
if(dirname($file)!='small' && dirname($file)!='thumb' && dirname($file)!='medium')
{
if(strcasecmp($filetypes[0],$extension) == 0 || strcasecmp($filetypes[1],$extension) == 0)
{
$current_name = $pathinfo['filename'];
$destination = dirname($file).DS.'thumb'.DS.substr($current_name, 0,-6).'.'.$extension;
$thumb_path = dirname($file).DS.'thumb';
if(is_dir($thumb_path) ==false){
mkdir($thumb_path);
}
static::Crop($file,$destination,$tn_w = 300,$tn_h =200,$quality = 100,$wmsource = false);
echo $destination;
echo "$file resized to $destination";
echo '</br>';
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment