#native_company# #native_desc#
#native_cta#

Dynapic

By Riccardo Pasquini
on August 9, 2002

Version: 1.0a

Type: Class

Category: Graphics

License: GNU General Public License

Description: class to create thumbnails on the fly

<?
//Images Wrapper for thumbs and other stuffs, require GDLib
//Riccardo Pasquini, 04/04/2001, v0.1
//[email protected]

/*
Finally the class review, 09/08/2002, v1.0a
this source is of course under GPL, and ill be glad if u notify me any change

Documentation (im sorry for my english)

*public properties*
none

*public methods*
DynaPic(string pic, string format)		->		class constructor
***parameters
	string pic		->		full pic name, including the path (absolute or relative)
	string format	->		the format of the input file, be sure that GDLib handle it!
	
destroy()								->		free the resources

bool create()							->		initialize the handling of the pic, thats not required after the constructor
												but could be required after some method calls like setPicName($pic), i.e.
												
setPicName(string pic)					->		change the handled pic, the class require a call to create() after this
***parameters
	string pic		->		full pic name, including the path (absolute or relative)

string getPicName()						->		returns the name of the handled pic

setThumbDimension(float pctg)			->		initialize the dimensions of the default thumbnail
***parameters
	float pctg		->		float number (0<pctg<=1) which specifies the resize factor of the default thumb, i.e. pctg=1 means that 
							the thumb is like the original picture, pctg=0.5 means that the thumb is half of the original, and so on...

getThumbDimension(float &w,float &h)	->		return as referenced parameters the default dimension on the thumbnail

setPicFormat(string format)				->		set a new output format for the loaded pic, require a call to create() after this
***parameters
	string format	->		the format of the input file, be sure that GDLib handle it!

string getPicFormat()					->		returns the format of the output

thumb()									->		output the default thumbnail

full()									->		output the original image

view(float width, float height)			->		output a thumbnail, the dimension is specified in the parameters of the method
***parameters
	float width		->		width of the output pic
	float height	->		height of the output pic
*/
class DynaPic
{
      var $m_sPicName;		//file name with path
      var $m_sFormat;		//file format, check m_asAllowedFormat
      var $m_hPic;			//image handle
      var $m_bHandled;		//flag to check if the pic is well loaded
      var $m_nThumbX;		//width of the thumbnail
      var $m_nThumbY;		//height of the thumbnail
      
      //possible pic formats
      var $m_asAllowedFormat 	= array("GIF","PNG","JPEG");
      //and related headers
      var $m_aHeaders			= array("GIF"=>"Content-type: image/gif","PNG"=>"Content-type: image/png","JPEG"=>"Content-type: image/jpeg");
      
      function DynaPic()
      {
      		die("Constructor requires parameters: string pic_path, string ");
      }

      function DynaPic($pic,$format)
      {
      		 $this->m_sFormat = strtoupper($format);
             //check if valid format
             if(!in_array($this->m_sFormat,$this->m_asAllowedFormat))
             {
                  die("Invalid format: ".stripslashes(htmlentities($this->m_sFormat)));
             }

             //initialize
             $this->m_sPicName=$pic;

             //thumb default resize 50%
             $this->setThumbDimension(0.5);
             if(!$this->create())
             	die("Unable to create ".stripslashes(htmlentities($this->m_sPicName))." as ".stripslashes(htmlentities($this->m_sFormat)));
      }

      function destroy()
      {
             if($this->m_bHandled)
             {
                 imagedestroy($this->m_hPic);
                 $this->m_bHandled=false;
             }
      }

      function create()
      {
             $this->destroy();

             eval("$this->m_hPic=@imagecreatefrom".$this->m_sFormat."('".$this->m_sPicName."');");

             //check if init succeded
             if(!$this->m_hPic)
             {
                  $this->m_bHandled=false;
                  return false;
             }
             else
             {
                  $this->m_bHandled=true;
                  return true;
             }
      }

      function setPicName($pic)
      {
             $this->m_sPicName=$pic;
             $this->m_bHandled=false;
      }

      function getPicName()
      {
             return $this->m_sPicName;
      }

      function setThumbDimension($pctg)
      {
      		 if($pctg<=0 || $pctg>1)
      		 	die("Not a valid resize factor");
      		 	
             $size = GetImageSize($this->m_sPicName);

             $this->m_nThumbX=$size[0]*$pctg;
             $this->m_nThumbY=$size[1]*$pctg;
      }

      function getThumbDimension(&$w,&$h)
      {
             $w=$this->m_nThumbX;
             $h=$this->m_nThumbY;
      }

      function setPicFormat($format)
      {
             //check if valid format
             if(!in_array($format,$this->m_asAllowedFormat))
             {
                  die("Invalid format: ".stripslashes(htmlentities($this->m_sFormat)));
             }      	
             else
             {
                  $this->m_sFormat=strtoupper($format);
                  $this->m_bHandled=false;
             }
      }

      function getPicFormat()
      {
             return $this->m_sFormat;
      }

      //display functions
      function thumb()
      {
            if($this->m_bHandled)
            {
                $size = GetImageSize($this->m_sPicName);
                $picTmp=imagecreate($this->m_nThumbX,$this->m_nThumbY);
                imagecopyresized($picTmp, $this->m_hPic, 0, 0, 0, 0, $this->m_nThumbX, $this->m_nThumbY, $size[0], $size[1]);
                header($this->m_aHeaders[$this->m_sFormat]);
                switch($this->m_sFormat)
                {
                         case 'JPEG':
                               imagejpeg($picTmp);
                               break;
                         case 'GIF':
                               imagegif($picTmp);
                               break;
                         case 'PNG':
                               imagepng($picTmp);
                               break;
                }
                imagedestroy($picTmp);
            }
      }

      function full()
      {
            if($this->m_bHandled)
            {
                header($this->m_aHeaders[$this->m_sFormat]);
                switch($this->m_sFormat)
                {
                         case 'JPEG':
                               imagejpeg($this->m_hPic);
                               break;
                         case 'GIF':
                               imagegif($this->m_hPic);
                               break;
                         case 'PNG':
                               imagepng($this->m_hPic);
                               break;
                }
            }
      }

      function view($width, $height)
      {
            if($this->m_bHandled)
            {
                $size = GetImageSize($this->m_sPicName);
                $picTmp=imagecreate($width,$height);
                imagecopyresized($picTmp, $this->m_hPic, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
                header($this->m_aHeaders[$this->m_sFormat]);
                switch($this->m_sFormat)
                {
                         case 'JPEG':
                               imagejpeg($picTmp);
                               break;
                         case 'GIF':
                               imagegif($picTmp);
                               break;
                         case 'PNG':
                               imagepng($picTmp);
                               break;
                }
                imagedestroy($picTmp);
            }
      }
}

/*
sample use, view folder content in thumbs

<script1.php>
$handle=opendir($dirname);
while ($file = readdir($handle))
{
	if($file=='.'||$file=='..' || is_dir($dirname.$file))
		continue;
		
	//i suppose that only gif images r in that directory
	echo "<img border="0" src="thumb.php?pic=".urlencode($dirname.$file)."&zoom=25&ext=gif"><br>n";
}
closedir($handle);

<thumb.php>
include("dynapic.class.inc.php");
$hThumb = new DynaPic(urldecode($HTTP_GET_VARS["pic"]),$HTTP_GET_VARS["ext"]);
$hThumb->setThumbDimension($HTTP_GET_VARS["zoom"]/100);
$hThumb->thumb();
$hThumb->destroy();
//*/
?>