Source for file OCSP_IMAGE.phpclass

Documentation is available at OCSP_IMAGE.phpclass

  1. <?php
  2. /**
  3.   * @project    Open CSP-Management
  4.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  5.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  6.   *
  7.   * @package    common
  8.   * @category   image
  9.   *
  10.   ***/
  11.  
  12.     // ---------------------------------------------------------
  13.     // requirements
  14.     // ---------------------------------------------------------
  15.     
  16.     pcf_require_class('OCSP_FILE',"common/");
  17.     
  18.  
  19. /**
  20.   * @project    Open CSP-Management
  21.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  22.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  23.   *
  24.   * @package    common
  25.   * @category   image
  26.   *
  27.   ***/
  28. class OCSP_IMAGE extends OCSP_FILE {
  29.  
  30.     // ---------------------------------------------------------------------------
  31.     // constants
  32.     // ---------------------------------------------------------------------------
  33.     
  34.     /**
  35.      * @constant string CLASS_SRC_FILE
  36.      */
  37.     const CLASS_SRC_FILE = __FILE__;
  38.  
  39.     
  40.     
  41.    /*** Attributes: ***/
  42.  
  43.     /**
  44.       * @var int $width 
  45.       * @access protected
  46.       ***/
  47.     protected $width;
  48.  
  49.     /**
  50.       * @var int $height 
  51.       * @access protected
  52.       ***/
  53.     protected $height;
  54.  
  55.     /**
  56.      * minimal width when resizing
  57.      * 
  58.      * @var int $minWidth 
  59.      */
  60.     protected $minWidth = 1;
  61.  
  62.     /**
  63.      * minimal width when resizing
  64.      * 
  65.      * @var int $minHeight 
  66.      */
  67.     protected $minHeight = 1;
  68.     
  69.     
  70.     /**
  71.       * @var int $type 
  72.       * @access protected
  73.       ***/
  74.     protected $type;
  75.  
  76.     /**
  77.       * @var resource $myImgRes 
  78.       * @access protected
  79.       ***/
  80.     protected $myImgRes=NULL;
  81.  
  82.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83.     // constructor init, and attribut getting / settings
  84.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85.  
  86.     /**
  87.       * sets the image file to $file
  88.       * if $file does not exists
  89.       * $OCSP_CONF['PROJECTPATH'].$file and
  90.       * $_SERVER['DOCUMENT_ROOT'] is also checked
  91.       *
  92.       * @param string $file 
  93.       * @param bool debug
  94.       *
  95.       * @return bool 
  96.       *
  97.       * @access public
  98.       ***/
  99.     public function setFile($file
  100.     {
  101.         if (parent::setFile($file&& $this->isImage())
  102.         {
  103.             $this->width=$this->imagesize[0];
  104.             $this->height=$this->imagesize[1];
  105.             $this->type=$this->imagesize[2];
  106.             return True;            
  107.         }
  108.  
  109.         return False;
  110.     }
  111.     
  112.     
  113.     /**
  114.      * @return int 
  115.      */
  116.     function getImgType()
  117.     {
  118.         return $this->type;
  119.     }
  120.     
  121.     /**
  122.       * @return int 
  123.       * @access public
  124.       ***/
  125.     public function getWidth{
  126.         return intval($this->width);
  127.     // end of member function getWidth
  128.  
  129.     
  130.     /**
  131.       * @return int 
  132.       * @access public
  133.       ***/
  134.     public function getHeight{
  135.         return intval($this->height);
  136.     // end of member function getHeight
  137.  
  138.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139.  
  140.     /**
  141.      * sets the minimum width for resize
  142.      *
  143.      * @param int $width 
  144.      */
  145.     public function setMinWidth($width)
  146.     {
  147.         if (intval($width))
  148.         {
  149.             $this->minWidth=intval($width);
  150.         }
  151.     }
  152.     
  153.     /**
  154.      * sets the minimum height for resize
  155.      *
  156.      * @param int $height 
  157.      */
  158.     public function setMinHeight($height)
  159.     {
  160.         if (intval($height))
  161.         {
  162.             $this->minHeight = intval($height);
  163.         }
  164.     }
  165.     
  166.     
  167.     /**
  168.       * calculates the width and height the image must have to
  169.       * fit into the $with and $height
  170.       *
  171.       * @param int $width 
  172.       * @param int $height 
  173.       * @param boolean $enlarge 
  174.       *
  175.       * @access public
  176.       *
  177.       * @return boolean 
  178.       * 
  179.       * @version pk-07-11-27 (0 values of $width and $height)
  180.       * @version pk-07-11-27 (return value)
  181.       ***/
  182.     public function getFitSize(&$width&$height$enlarge=False,$debug=False
  183.     {
  184.         $i_maxWidth =(intval($width)  intval($width)  $this->getWidth());
  185.         $i_maxHeight=(intval($heightintval($height$this->getHeight());
  186.  
  187.         $width  =$this->getWidth();
  188.         $height =$this->getHeight();
  189.         
  190.         if ($debugechoDebugLine(__FILE__,__LINE__,"max$i_maxWidth x $i_maxHeight orignial$width x $height<br/>");
  191.         
  192.         $b_ret=True;
  193.         
  194.         while ($width $i_maxWidth{
  195.             $width floor($width 0.99);
  196.             if ($height $this->minHeight)
  197.             {
  198.                 $height floor($height 0.99);
  199.             }
  200.             $b_ret=False;
  201.         }
  202.         
  203.         if ($debugechoDebugLine(__FILE__,__LINE__,"max$i_maxWidth x $i_maxHeight new1$width x $height<br/>");
  204.         
  205.         
  206.         while ($height $i_maxHeight{
  207.             if ($width $this->minWidth)
  208.             {
  209.                 $width floor($width 0.99);
  210.             }
  211.             $height floor($height 0.99);
  212.             $b_ret=False;
  213.         }
  214.  
  215.         if ($debugechoDebugLine(__FILE__,__LINE__,"max$i_maxWidth x $i_maxHeight new2$width x $height<br/>");
  216.         
  217.         if ($enlarge{
  218.             while ($width $i_maxWidth{
  219.                 $width floor($width 1.01);
  220.                 $height floor($height 1.01);
  221.                 $b_ret=False;
  222.             }
  223.             while ($height $i_maxHeight{
  224.                 $width floor($width 1.01);
  225.                 $height floor($height 1.01);
  226.                 $b_ret=False;
  227.             }
  228.             if ($debugechoDebugLine(__FILE__,__LINE__,"max$i_maxWidth x $i_maxHeight new3$width x $height<br/>");
  229.         }
  230.         
  231.         return $b_ret;
  232.     }
  233.  
  234.     /**
  235.      * resize the image and overwrites the original location
  236.      *
  237.      * @param int $width 
  238.      * @param int $height 
  239.      * @param boolean $debug 
  240.      * 
  241.      * @return boolean 
  242.      */
  243.     public function resize($width,$height,$debug=False)
  244.     {
  245.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_IMAGE::resize({$width},{$height})");
  246.         
  247.         if ($this->res_create())
  248.         {
  249.             $this->res_resize(intval($width),intval($height));
  250.             return $this->res_saveAs($this->getFilePath());
  251.         }
  252.         return False;        
  253.     }
  254.     
  255.     
  256.     /**
  257.       * returns the mimetype of the image
  258.       *
  259.       * @param int $aType (if !intval this->type is used)
  260.       * @return string 
  261.       *
  262.       * @todo implementation for other types then gif,jpeg and png
  263.       *
  264.       ***/
  265.     public function getMimeType($aType=0{
  266.         if (!intval($aType)) $aType=$this->type;
  267.         switch($aType{
  268.             case 1// gif
  269.                 return "image/gif";
  270.             case 2// jpeg
  271.                 return "image/jpeg";
  272.             case 3// png
  273.                 return "image/png";
  274.             default// create an empty image
  275.                 return "image/jpeg";
  276.         }
  277.     }
  278.  
  279.     /**
  280.       * returns the Fileextension of the image type
  281.       *
  282.       * @param int $aType (if !intval this->type is used)
  283.       * @return string 
  284.       *
  285.       * @todo implementation for other types then gif,jpeg and png
  286.       *
  287.       ***/
  288.     public function getTypeFileExt($aType=0{
  289.         if (!intval($aType)) $aType=$this->type;
  290.         switch($aType{
  291.             case 1// gif
  292.                 return "gif";
  293.             case 2// jpeg
  294.                 return "jpg";
  295.             case 3// png
  296.                 return "png";
  297.             default
  298.                 return "";
  299.         }
  300.     }
  301.     
  302.     
  303.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  304.     // resource methods
  305.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  306.  
  307.     /**
  308.       * creates the image from the source (if file exists)
  309.       *
  310.       * @returns boolean
  311.       *
  312.       * @access public
  313.       *
  314.       * @todo implementation for other types then gif,jpeg and png
  315.       * @todo gd-check
  316.       *
  317.       ***/
  318.     public function res_create({
  319.         switch($this->type{
  320.             case 1// gif
  321.                 $this->myImgRes=imagecreatefromgif($this->myFilePath);
  322.                 break;
  323.             case 2// jpeg
  324.                 $this->myImgRes=imagecreatefromjpeg($this->myFilePath);
  325.                 break;
  326.             case 3// png
  327.                 $this->myImgRes=imagecreatefrompng($this->myFilePath);
  328.                 break;
  329.             default// create an empty image
  330.                 return False;
  331.         }
  332.         if (!empty($this->myImgRes)) {
  333.             return True;
  334.         else {
  335.             return False;
  336.         }
  337.     }
  338.  
  339.     /**
  340.       * resizes the resource to width and height
  341.       *
  342.       * @param int $width 
  343.       * @param int $height 
  344.       *
  345.       * @return boolean 
  346.       *
  347.       ***/
  348.     public function res_resize($width,$height{
  349.         if (empty($this->myImgRes&& (!$this->res_create())) {
  350.             return False;
  351.         }
  352.  
  353.         if (intval($width1)  $width  =$this->getWidth();
  354.         if (intval($height1$height =$this->getHeight();
  355.  
  356.         if (($width==$this->getWidth()) && ($height==$this->getHeight())) {
  357.             return True// nothing to do
  358.         }
  359.  
  360.         if (intval(str_replace(".","",phpversion())) >= 422{
  361.             $r_newImg imagecreatetruecolor($width,$height);
  362.         else {
  363.             $r_newImg imagecreate($width,$height);
  364.         }
  365.         imagecopyresized($r_newImg$this->myImgRes0000$width,$heightimagesx($this->myImgRes)imagesy($this->myImgRes));
  366.         $this->myImgRes=$r_newImg;
  367.         
  368.         $this->width=$width;
  369.         $this->height=$height;
  370.         
  371.         return True;
  372.     }
  373.  
  374.     /**
  375.       * returns the resource for output
  376.       *
  377.       * @param int $asType 0=same as image
  378.       *
  379.       * @returns boolean
  380.       *
  381.       ***/
  382.     public function res_get($asType=0)  {
  383.         $i_type=(intval($asType$asType $this->type);
  384.         switch($i_type{
  385.             case 1:
  386.                 return imagegif($this->myImgRes);
  387.             case 3:
  388.                 return imagepng($this->myImgRes);
  389.             case 2:
  390.                 return imagejpeg($this->myImgRes);
  391.         }
  392.     }
  393.  
  394.     /**
  395.       * saves the resource
  396.       *
  397.       * @param string $fileName (empty will overwrite the image)
  398.       * @param int $asType 0=same as image
  399.       * @param bool $testType (if true and imagexxx fails the next one is tried)
  400.       *
  401.       * @returns int (type number)
  402.       *
  403.       ***/
  404.     public function res_saveAs($fileName="",$asType=0,$testType=False)  
  405.         if (empty($fileName)) $fileName=dirname($this->myFilePath_OCSP_DIRSEP_ basename($this->myFileName);       
  406.         $i_type=(intval($asType$asType $this->type);
  407.         
  408.         if (empty($this->myImgRes&& (!$this->res_create())) {
  409.             return False;
  410.         }
  411.                 
  412.         switch($i_type{
  413.             case 1// gif
  414.                 if (imagegif($this->myImgRes,$fileName))
  415.                 {
  416.                     return 1;
  417.                 else if (!$testType{
  418.                     return False;
  419.                 }
  420.             case 3// png
  421.                 if (imagepng($this->myImgRes,$fileName))
  422.                 {
  423.                     return 3;
  424.                 else if (!$testType{
  425.                     return False;
  426.                 }
  427.             case 2// jpg
  428.                 if (imagejpeg($this->myImgRes,$fileName))
  429.                 {
  430.                     return 2;
  431.                 else {
  432.                     return False;
  433.                 }
  434.         }
  435.         return False;
  436.     }    
  437.     
  438.  
  439.     /**
  440.       * frees myImgRes
  441.       *
  442.       **/
  443.  
  444.     public function res_free(
  445.     {
  446.         $this->myImgRes=NULL;
  447.     }
  448.  
  449.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  450.     // output methods
  451.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  452.     
  453.     /**
  454.      * echos the image with headers
  455.      * 
  456.      * @param int $width 
  457.      * @param int $height 
  458.      * @param boolean $useCache 
  459.      * @param boolean $enlarge 
  460.      * @param boolean $debug 
  461.      * 
  462.      * @return boolean if success
  463.      */
  464.     public function output($width=0,$height=0,$useCache=True,$enlarge=False,$debug=False)
  465.     {           
  466.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_IMAGE::output({$width},{$height}");
  467.  
  468.         if (empty($this->myFilePath))
  469.         {
  470.             return False;
  471.         }
  472.         
  473.         if (!$debug)
  474.         {
  475.             header("Content-Type: ".$this->getMimeType());
  476.             header("Cache-Control: public");
  477.         }
  478.         
  479.         if ($this->getFitSize($width,$height,$enlarge,$debug))
  480.         {
  481.             $fp=fopen($this->myFilePath"rb");
  482.             fpassthru($fp);
  483.             fclose($fp)
  484.             return True;
  485.         else {
  486.             if ($useCache)
  487.             {
  488.                 $str_cacheFile $this->getCacheFilePath($width,$height,True,$debug)
  489.                 if ($debugechoDebugLine(__FILE__,__LINE__,"cachefile: " $str_cacheFile);
  490.                 if (!self::isFile($str_cacheFile))
  491.                 {
  492.                     $this->generateCacheFile($width,$height,$enlarge,$debug);
  493.                     $this->res_get();
  494.                     $this->res_free();
  495.                     return True;
  496.                 else {
  497.                     $fp=fopen($str_cacheFile"rb");
  498.                     fpassthru($fp);
  499.                     fclose($fp);
  500.                     return True
  501.                 }
  502.                 
  503.             else {
  504.                 $this->res_create();
  505.                 $this->res_resize($width,$height);
  506.                 $this->res_get();
  507.                 $this->res_free();
  508.                 return True;                
  509.             }    
  510.         }
  511.                 
  512.         return False;
  513.     }
  514.         
  515.     
  516.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  517.     // cache methods
  518.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  519.     
  520.     /**
  521.      * @param int $width 
  522.      * @param int $height 
  523.      * @param boolean $autoCreate 
  524.      * @param boolean $debug 
  525.      * 
  526.      * @return string 
  527.      */
  528.     protected function getCacheFilePath($width,$height,$autoCreate=True,$debug=False)
  529.     {
  530.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_IMAGE::getCaccheFilePath");
  531.         $str_cDir=str_replace($_SERVER['DOCUMENT_ROOT'],"__DR__",dirname($this->myFilePath));
  532.         $str_cDir=str_replace(__OCSP_PROJECTPATH__,"__PP__",$str_cDir);
  533.         
  534.         $str_cDir =  OCSP_CONF::getInstance()->getValue('IMGCACHEPATH').$str_cDir;
  535.                        
  536.         if (pcf_checkDir($str_cDir,$autoCreate,0777,$debug))
  537.         {
  538.             return $str_cDir _OCSP_DIRSEP_ "cache_" $width "x" $height "_" basename($this->myFileName);            
  539.         }
  540.         return;        
  541.     }
  542.     
  543.     /**
  544.      * generates a cahce file if the image does not fit otherwithe returns false
  545.      * 
  546.      * NOTE does not free the resource
  547.      * 
  548.      * @param int $width 
  549.      * @param int $height 
  550.      * @param boolean $enlarge 
  551.      * @param boolean $debug 
  552.      * 
  553.      * @return boolean 
  554.      */
  555.     protected function generateCacheFile($width,$height,$enlarge=False,$debug=False)
  556.     {
  557.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_IMAGE::generateCacheFile({$width},{$height})");
  558.         
  559.         if (!$this->getFitSize($width,$height,$enlarge,$debug))
  560.         {
  561.             $str_cacheFile $this->getCacheFilePath($width,$height,True,$debug)
  562.             if ($debugechoDebugLine(__FILE__,__LINE__,"CacheFile: " $str_cacheFile);
  563.             $this->res_create();
  564.             $this->res_resize($width,$height);
  565.             $this->res_saveAs($str_cacheFile);
  566.             return True;            
  567.         else {
  568.             return False;
  569.         }
  570.     }
  571.     
  572.     /**
  573.      * removes the cache files
  574.      *
  575.      * @param boolean $debug 
  576.      */
  577.     public function clearCache($debug=False)
  578.     {
  579.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_IMAGE::clearCache");
  580.         $str_cDir=str_replace($_SERVER['DOCUMENT_ROOT'],"__DR__",dirname($this->myFilePath));
  581.         $str_cDir=str_replace(__OCSP_PROJECTPATH__,"__PP__",$str_cDir);
  582.         
  583.         $str_cDir =  OCSP_CONF::getInstance()->getValue('IMGCACHEPATH').$str_cDir;
  584.         
  585.         if ($arr_files OCSP_DIRECTORY::getDirList($str_cDir,"FL"))
  586.         {
  587.             foreach($arr_files as $str_file)
  588.             {
  589.                 $str_pattern='/^cache_\d+x\d+' basename($this->myFileName'/';
  590.                 if (preg_match($str_pattern,$str_file))
  591.                 {
  592.                     unlink(OCSP_FILE::checkFilePath($str_cDir._OCSP_DIRSEP_.$str_file));
  593.                 }
  594.             }
  595.         }
  596.         
  597.     }
  598.     
  599.     // ---------------------------------------------------------------------------
  600.     // file operations
  601.     // ---------------------------------------------------------------------------    
  602.     
  603.     /**
  604.      * deletes the file
  605.      *
  606.      */
  607.     public function delete()
  608.     {
  609.         $this->clearCache();
  610.         parent::delete();
  611.     }    
  612.     
  613. // end of OCSP_IMAGE
  614. ?>

Documentation generated on Thu, 08 Jan 2009 17:46:53 +0100 by phpDocumentor 1.4.0a2