showImage($resize->im2); * * The class holds the original image in the variable 'im' and the new image in 'im2'. Therefore the code above will show the newly created image. * * You can get information about the image by doing the following: * print_r($objResize->findResourceDetails($objResize->resOriginalImage)); * print_r($objResize->findResourceDetails($objResize->resResizedImage)); * * This will be useful if you wish to retrieve any details about the images. * * By default the class will stop you from enlarging your images (or else they will look grainy) and if you want to do this you must turn off the protection mode by passing a 5th parameter * * $objResize = new SuperSizer('myImage.gif', 'myEnlargedImage.gif', 'P', '200', false); * */ class SuperSizer { var $strOriginalImagePath; var $strResizedImagePath; var $arrOriginalDetails; var $arrResizedDetails; var $resOriginalImage; var $resResizedImage; var $boolProtect = true; /* * * @Method: __constructor * @Parameters: 5 * @Param-1: strPath - String - The path to the image * @Param-2: strSavePath - String - The path to save the new image to * @Param-3: strType - String - The type of resize you want to perform * @Param-4: value - Number/Array - The resize dimensions * @Param-5: boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller * @Description: Calls the JLB_Pagination method so its php 4 compatible * */ function __constructor($strPath, $strSavePath, $strType = 'W', $value = '150', $Quality = '85', $boolProtect = true){ $this->SuperSizer($strPath, $strSavePath, $strType, $value); } /* * * @Method: SuperSizer * @Parameters: 5 * @Param-1: strPath - String - The path to the image * @Param-2: strSavePath - String - The path to save the new image to * @Param-3: strType - String - The type of resize you want to perform * @Param-4: value - Number/Array - The resize dimensions * @Param-5: boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller * @Description: Calls the JLB_Pagination method so its php 4 compatible * */ function SuperSizer($strPath, $strSavePath, $strType = 'W', $value = '150', $Quality = '85', $boolProtect = true){ //save the image/path details $this->strOriginalImagePath = str_replace(" ", "%20", $strPath); $this->strResizedImagePath = $strSavePath; $this->boolProtect = $boolProtect; //get the image dimensions $this->arrOriginalDetails = getimagesize($this->strOriginalImagePath); $this->arrResizedDetails = $this->arrOriginalDetails; //create an image resouce to work with $this->resOriginalImage = $this->createImage($this->strOriginalImagePath); //select the image resize type switch(strtoupper($strType)){ case 'P': $this->resizeToPercent($value); break; case 'H': $this->resizeToHeight($value); break; case 'C': $this->resizeToCustom($value); break; case 'W': default: $this->resizeToWidth($value); break; } } /* * * @Method: findResourceDetails * @Parameters: 1 * @Param-1: resImage - Resource - The image resource you want details on * @Description: Returns an array of details about the resource identifier that you pass it * */ function findResourceDetails($resImage){ //check to see what image is being requested if($resImage==$this->resResizedImage){ //return new image details return $this->arrResizedDetails; }else{ //return original image details return $this->arrOriginalDetails; } } /* * * @Method: updateNewDetails * @Parameters: 0 * @Description: Updates the width and height values of the resized details array * */ function updateNewDetails(){ $this->arrResizedDetails[0] = imagesx($this->resResizedImage); $this->arrResizedDetails[1] = imagesy($this->resResizedImage); } /* * * @Method: createImage * @Parameters: 1 * @Param-1: strImagePath - String - The path to the image * @Description: Created an image resource of the image path passed to it * */ function createImage($strImagePath){ //get the image details $arrDetails = $this->findResourceDetails($strImagePath); //choose the correct function for the image type switch($arrDetails['mime']){ case 'image/jpeg': return imagecreatefromjpeg($strImagePath); break; case 'image/png': return imagecreatefrompng($strImagePath); break; case 'image/gif': return imagecreatefromgif($strImagePath); break; } } /* * * @Method: saveImage * @Parameters: 1 * @Param-1: numQuality - Number - The quality to save the image at * @Description: Saves the resize image * */ function saveImage($numQuality = 85){ switch($this->arrResizedDetails['mime']){ case 'image/jpeg': imagejpeg($this->resResizedImage, $this->strResizedImagePath, $numQuality); break; case 'image/png': // imagepng = [0-9] (not [0-100]) imagepng($this->resResizedImage, $this->strResizedImagePath, 7); break; case 'image/gif': imagegif($this->resResizedImage, $this->strResizedImagePath); break; } } /* * * @Method: showImage * @Parameters: 1 * @Param-1: resImage - Resource - The resource of the image you want to display * @Description: Displays the image resouce on the screen * */ function showImage($resImage){ //get the image details $arrDetails = $this->findResourceDetails($resImage); //set the correct header for the image we are displaying header("Content-type: ".$arrDetails['mime']); switch($arrDetails['mime']){ case 'image/jpeg': return imagejpeg($resImage); break; case 'image/png': return imagepng($resImage); break; case 'image/gif': return imagegif($resImage); break; } } /* * * @Method: destroyImage * @Parameters: 1 * @Param-1: resImage - Resource - The image resource you want to destroy * @Description: Destroys the image resource and so cleans things up * */ function destroyImage($resImage){ imagedestroy($resImage); } /* * * @Method: _resize * @Parameters: 2 * @Param-1: numWidth - Number - The width of the image in pixels * @Param-2: numHeight - Number - The height of the image in pixes * @Description: Resizes the image by creatin a new canvas and copying the image over onto it. DONT CALL THIS METHOD DIRECTLY - USE THE METHODS BELOW * */ function _resize($numWidth, $numHeight){ //check for image protection if($this->_imageProtect($numWidth, $numHeight)){ if($this->arrOriginalDetails['mime']=='image/jpeg'){ //JPG image $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight); }else if($this->arrOriginalDetails['mime']=='image/gif'){ //GIF image $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight); imagealphablending($this->resResizedImage, false); imagesavealpha($this->resResizedImage,true); $transparent = imagecolorallocatealpha($this->resResizedImage, 255, 255, 255, 127); imagefilledrectangle($this->resResizedImage, 0, 0, $numWidth, $numHeight, $transparent); imagecolortransparent($this->resResizedImage, $transparent); }else if($this->arrOriginalDetails['mime']=='image/png'){ //PNG image $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight); imagecolortransparent($this->resResizedImage, imagecolorallocate($this->resResizedImage, 0, 0, 0)); // Turn off transparency blending (temporarily) imagealphablending($this->resResizedImage, false); // Create a new transparent color for image $color = imagecolorallocatealpha($this->resResizedImage, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($this->resResizedImage, 0, 0, $color); // Restore transparency blending imagesavealpha($this->resResizedImage, true); } //update the image size details $this->updateNewDetails(); //do the actual image resize imagecopyresized($this->resResizedImage, $this->resOriginalImage, 0, 0, 0, 0, $numWidth, $numHeight, $this->arrOriginalDetails[0], $this->arrOriginalDetails[1]); //saves the image $this->saveImage(); } } /* * * @Method: _imageProtect * @Parameters: 2 * @Param-1: numWidth - Number - The width of the image in pixels * @Param-2: numHeight - Number - The height of the image in pixes * @Description: Checks to see if we should allow the resize to take place or not depending on the size the image will be resized to * */ function _imageProtect($numWidth, $numHeight){ if($this->boolProtect AND ($numWidth > $this->arrOriginalDetails[0] OR $numHeight > $this->arrOriginalDetails[1])){ return 0; } return 1; } /* * * @Method: resizeToWidth * @Parameters: 1 * @Param-1: numWidth - Number - The width to resize to in pixels * @Description: Works out the height value to go with the width value passed, then calls the resize method. * */ function resizeToWidth($numWidth){ $numHeight=(int)(($numWidth*$this->arrOriginalDetails[1])/$this->arrOriginalDetails[0]); $this->_resize($numWidth, $numHeight); } /* * * @Method: resizeToHeight * @Parameters: 1 * @Param-1: numHeight - Number - The height to resize to in pixels * @Description: Works out the width value to go with the height value passed, then calls the resize method. * */ function resizeToHeight($numHeight){ $numWidth=(int)(($numHeight*$this->arrOriginalDetails[0])/$this->arrOriginalDetails[1]); $this->_resize($numWidth, $numHeight); } /* * * @Method: resizeToPercent * @Parameters: 1 * @Param-1: numPercent - Number - The percentage you want to resize to * @Description: Works out the width and height value to go with the percent value passed, then calls the resize method. * */ function resizeToPercent($numPercent){ $numWidth = (int)(($this->arrOriginalDetails[0]/100)*$numPercent); $numHeight = (int)(($this->arrOriginalDetails[1]/100)*$numPercent); $this->_resize($numWidth, $numHeight); } /* * * @Method: resizeToCustom * @Parameters: 1 * @Param-1: size - Number/Array - Either a number of array of numbers for the width and height in pixels * @Description: Checks to see if array was passed and calls the resize method with the correct values. * */ function resizeToCustom($size){ if(!is_array($size)){ $this->_resize((int)$size, (int)$size); }else{ $this->_resize((int)$size[0], (int)$size[1]); } } /* * * @Method: recursive_rmdir * @Parameters: 1 * @Param-1: dir - path - path to the SuperSizerTemp folder * @Description: deletes it... that's it * */ function recursive_rmdir($dir) { // all subdirectories and contents: if(is_dir($dir))$dir_handle=opendir($dir); while($file=readdir($dir_handle)) { if($file!="." && $file!="..") { if(!is_dir($dir."/".$file))unlink ($dir."/".$file); else $this->recursive_rmdir($dir."/".$file); } } closedir($dir_handle); rmdir($dir); return true; } } function smarty_cms_function_supersizer($params, &$smarty) { global $gCms; $config =& $gCms->GetConfig(); //get and set the parameters $debugIT = isset($params['debugIT']) ? $params['debugIT']:false; $noOutPut = isset($params['noOutPut']) ? $params['noOutPut']:false; $path = isset($params['path']) ? $params['path']:false; $Prefix = isset($params['Prefix']) ? $params['Prefix']:''; $Suffix = isset($params['Suffix']) ? $params['Suffix']:''; $Subdir = isset($params['Subdir']) ? $params['Subdir'] : ""; $stripTags = isset($params['stripTags']) ? $params['stripTags'] : false; $cacheBuster = isset($params['cacheBuster']) ? $params['cacheBuster']:false; if($stripTags==true){ $path = preg_replace('/.*src=([\'"])((?:(?!\1).)*)\1.*/si','$2',$path); } $URL = isset($params['URL']) ? $params['URL']:''; $Assign = isset($params['Assign']) ? $params['Assign']:''; $OlDextension = substr($path, (strrpos($path,".")+1)); $extension = strtolower(substr($path, (strrpos($path,".")+1))); $filebasename = basename($path, $OlDextension); if(!isset($params['width']) && !isset($params['height']) && !isset($params['percentage'])) { $percentage = 25; }else{ $width = isset($params['width']) ? intval($params['width']) : 0; $height = isset($params['height']) ? intval($params['height']) : 0; $percentage = isset($params['percentage']) ? intval($params['percentage']) : 0; } if(isset($Subdir)){ if ((substr($Subdir, 0,1))=="/"){substr_replace($Subdir, '', 0, 1);} if ((substr($Subdir, -1))!="/"){$Subdir.="/";} } $rootUrl = isset($params['rootUrl']) ? $params['rootUrl'] : $config['uploads_url']; $filename=$config['uploads_path']."/SuperSizerTmp/".$Subdir.$Prefix.$filebasename."-w".$width."-h".$height."-p".$percentage.$Suffix.".".$extension; $fileLINK=$rootUrl."/SuperSizerTmp/".$Subdir.$Prefix.$filebasename."-w".$width."-h".$height."-p".$percentage.$Suffix.".".$extension; $Protect= isset($params['Protect']) ? $params['Protect'] : true; if(file_exists($filename)){ if($debugIT==true){ unlink ($filename); //smarty_cms_function_supersizer($params, &$smarty); }elseif(filemtime($filename) < filemtime($path)){ unlink ($filename); } } if($_POST['clearcache']==1){ unlink ($config['uploads_path']."/SuperSizerTmp/"); } if(!file_exists($filename)){ if(!file_exists($config['uploads_path']."/SuperSizerTmp/".$Subdir)){ mkdir($config['uploads_path']."/SuperSizerTmp/".$Subdir, 0777, true); } $Quality = isset($params['Quality']) ? intval($params['Quality']) : 85; if($width>0 || $height>0) { if ($width>0 && $height==0){ $objResize = new SuperSizer($path, $filename, 'W', $width, $Quality, $Protect); }elseif($height>0 && $width==0){ $objResize = new SuperSizer($path, $filename, 'H', $height, $Quality, $Protect); }else{ $objResize = new SuperSizer($path, $filename, 'C', array($width, $height), $Quality, $Protect); } }else{ $objResize = new SuperSizer($path, $filename, 'P', $percentage, $Quality, $Protect); } if($debugIT==true){ echo '

Original Image

';
			print_r($objResize->findResourceDetails($objResize->resOriginalImage));
			echo '

Resized Image

';
			print_r($objResize->findResourceDetails($objResize->resResizedImage)); 
			echo '
'; error_reporting(E_ALL); } } if($noOutPut==false){ if($cacheBuster==true){ $fileLINK=$fileLINK."?".filemtime($filename); } if($URL!=true){ $class = isset($params['class']) ? ' class="'.$params['class'].'" ':''; $alt = isset($params['alt']) ? ' alt="'.$params['alt'].'" ':''; $id = isset($params['id']) ? ' id="'.$params['id'].'" ':''; $title = isset($params['title']) ? ' title="'.$params['title'].'" ':''; if($Assign!=''){ $smarty->assign($Assign,""); }else{ echo ""; } }else{ if($Assign!=''){ $smarty->assign($Assign,$fileLINK); }else{ echo $fileLINK; } } } } function smarty_cms_help_function_supersizer() { if(isset($_POST['clearcache'])&&$_POST['clearcache']==1){ global $gCms; $config =& $gCms->GetConfig(); $objResize = new SuperSizer(); $dirpath=$config['uploads_path']."/SuperSizerTmp/"; echo "

Cleared $dirpath

"; $objResize->recursive_rmdir($dirpath); } echo'
'; ?>

What does this tag do?

How do I use this tag?

Just insert the tag into your template/page like in one of the examples here:
1. {supersizer path='uploads/images/test.jpg' percentage='25'}
2. {supersizer path='uploads/images/test.jpg' width='320'} (With aspect ratio)

3. {supersizer path='uploads/images/test.jpg' height='240'} (With aspect ratio)

4. {supersizer path='uploads/images/test.jpg' width='150' height='140'} (Aspect ratio depends on given width, height)

5. {supersizer path="$LogoSub" width='150' rootUrl="http://media.domain.org/uploads"}

5. {supersizer path="$LogoSub" width='150' Quality=55 Protect=false}

5. {supersizer path="$LogoSub" width='150' class='LogoImg' alt="$Name" rootUrl="http://media.domain.org/uploads" Quality=55}

What parameters does it take?



Author: jeremyBass <jeremybass@cableone.net>
Website: CorbensProducts.com
Support more mods like this:


Version: 1.6, 11.20.2009

Author: jeremyBass <jeremybass@cableone.net>

Version: 1.6, 11.20.2009