Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Created August 26, 2010 14:33
Show Gist options
  • Save mcgivrer/551496 to your computer and use it in GitHub Desktop.
Save mcgivrer/551496 to your computer and use it in GitHub Desktop.
<?php
class ImageThumbs{
private static $_instance;
// array of size to be generated, sample : array('320x200','80x120')
private $thumbsFormats=array();
private $thumbsFileFormat="png";
public $imgTypes = array(
1 => 'GIF',
2 => 'JPG',
3 => 'PNG',
4 => 'SWF',
5 => 'PSD',
6 => 'BMP',
7 => 'TIFF(intel byte order)',
8 => 'TIFF(motorola byte order)',
9 => 'JPC',
10 => 'JP2',
11 => 'JPX',
12 => 'JB2',
13 => 'SWC',
14 => 'IFF',
15 => 'WBMP',
16 => 'XBM'
);
public function __construct($thumbsSizes){
if($thumbsSizes !=""){
$thumbsFormats = $thumbsSizes;
}
}
public function generateThumbs($image,$path){
foreach($this->thumbsFormats as $format){
//print_r($format);
$size = explode('x',$format);
$split=explode('.',basename($image));
$pathFileDest = $path."/".$format."/".$split[0].".png";
if(!file_exists($path."/".$format)){
mkdir($path."/".$format);
}
if(!file_exists($pathFileDest)){
$this->resizeImage($image, $pathFileDest, $size[0],$size[1],true);
}
}
}
public function thumbsExists($path,$format){
if(file_exists($path.$format)){
return true;
}
return false;
}
public function resizeImage($source_pic,
$destination_pic,
$max_width,
$max_height,
$force=true)
){
if(!file_exists($destination_pic) || $force){
list($width,$height,$format)=getimagesize($source_pic);
if($format==1){
$src = imagecreatefromgif($source_pic);
}elseif($format==2){
$src = imagecreatefromjpeg($source_pic);
}elseif($format==3){
$src = imagecreatefrompng($source_pic);
}
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
//echo("width:$tn_width, height:$tn_height");
$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagealphablending($tmp,true);
imagesavealpha($tmp, true);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
//calculate PNG image quality based on a 1-100% ratio
$quality=85;
$pngQuality = ($quality - 100) / 11.111111;
$pngQuality = round(abs($pngQuality));
// save image in PNG format for alpha channel.
imagepng($tmp,$destination_pic,$pngQuality);
//free temporary used memory for image manipulation.
imagedestroy($src);
imagedestroy($tmp);
}
}
public function setThumbsFormat($thumbsFormat,$fileFormat="png"){
if($thumbsFormat != ""){
$this->thumbsFormats = $thumbsFormat;
}
$this->thumbsFileFormat = $fileFormat;
}
public static function getInstance($thumbsSizes){
if(!isset(self::$_instance)){
self::$_instance=new Image($thumbsSizes);
}else{
self::$_instance->setThumbsFormat($thumbsSizes);
}
return self::$_instance;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment