filesystem
[ class tree: filesystem ] [ index: filesystem ] [ all elements ]

Source for file ImageFiles.class.php

Documentation is available at ImageFiles.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package filesystem
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. include_onceCLASSPATH."filesystem/Files.class.php" );
  21.  
  22. /**
  23.  * A class to manage image files in a filesystemtree.
  24.  *
  25.  * @package   filesystem
  26.  * @version   0.1.35
  27.  * @author    Daniel Plücken <daniel@debakel.net>
  28.  * @license   http://www.gnu.org/copyleft/lesser.html
  29.  *             GNU Lesser General Public License
  30.  * @copyright Copyright (c) 2005 Daniel Plücken <daniel@debakel.net>
  31.  *
  32.  *  This library is free software; you can redistribute it and/or
  33.  *  modify it under the terms of the GNU Lesser General Public
  34.  *  License as published by the Free Software Foundation; either
  35.  *  version 2.1 of the License.
  36.  *
  37.  *  This library is distributed in the hope that it will be useful,
  38.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  39.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40.  *  GNU Lesser General Public License for more details.
  41.  *
  42.  *  You should have received a copy of the GNU Lesser General
  43.  *  Public License along with this library; if not, write to the
  44.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  45.  *  Boston, MA 02111-1307 USA
  46.  */
  47. class ImageFiles extends Files
  48. {
  49.   /**
  50.    * @access public
  51.    * @var    string  $kind 
  52.    */
  53.   var $kind = ""// JPG, PNG or GIF
  54.  
  55.   /**
  56.    * @access private
  57.    * @var    string  $width 
  58.    */
  59.   var $width = 0;
  60.  
  61.   /**
  62.    * @access private
  63.    * @var    string  $height 
  64.    */
  65.   var $height = 0;
  66.  
  67.   /**
  68.    * @access private
  69.    * @var    resource $handle 
  70.    */
  71.   var $handle = null;
  72.  
  73.   /**
  74.    * @access private
  75.    * @var    resource $handle 
  76.    */
  77.   var $edit_handle = null;
  78.  
  79.   /**
  80.    * The jpeg quality from 0 to 100.
  81.    *
  82.    * @access private
  83.    * @var    string $jpeg_compression 
  84.    */
  85.   var $jpeg_compression = 60;
  86.  
  87.  
  88.  
  89.   /**
  90.    * Constructor
  91.    *
  92.    * @version 1.12
  93.    * @since   0.1.0
  94.    * @author  Daniel Plücken <daniel@debakel.net>
  95.    * @access  public
  96.    * @param   string  $path_or_width 
  97.    * @param   string  $type_or_height 
  98.    */
  99.   function ImageFiles$path_or_width$type_or_height "" )
  100.   {
  101.     if !preg_match"!^\d+$!"$path_or_width ) )
  102.        $this->Files$path_or_width );
  103.  
  104.     if is_string$this->filename ) )
  105.     {
  106.         if preg_match"!^(JPG|GIF|PNG)$!i"$type_or_height ) )
  107.             $this->kind = strtoupper$type_or_height );
  108.         else
  109.         {
  110.             $tmp_str FilesystemToolkit::getFileSuffix$this->filename );
  111.  
  112.             if preg_match"!jpe?g$!i"$tmp_str ) )
  113.                $this->kind = "JPG";
  114.             else
  115.                $this->kind = strtoupper$tmp_str );
  116.         }
  117.     }
  118.   }
  119.  
  120.  
  121.  
  122.   /**
  123.    * Creates a handle to this image to be able to manipulate the image data. The
  124.    * handle will be stored in $this->handle.
  125.    *
  126.    * @version 1.0
  127.    * @since   0.1.35
  128.    * @author  Daniel Plücken <daniel@debakel.net>
  129.    * @access  public
  130.    * @return  void 
  131.    */
  132.   function createHandle()
  133.   {
  134.     switch $this->kind )
  135.     {
  136.         case "JPG":
  137.              $this->handle = imagecreatefromjpeg(
  138.                                      $this->path."/".$this->filename
  139.                                                 );
  140.              break;
  141.         case "PNG":
  142.              $this->handle = imagecreatefrompng(
  143.                                      $this->path."/".$this->filename
  144.                                                );
  145.              break;
  146.         case "GIF":
  147.              $this->handle = imagecreatefromgif(
  148.                                      $this->path."/".$this->filename
  149.                                                );
  150.              break;
  151.         default:
  152.              die "Not supported file format!" );
  153.              break;
  154.     }
  155.   }
  156.  
  157.  
  158.  
  159.   /**
  160.    * Sets the comression quality for jpg between 0 (zero) and 100 (one hundred).
  161.    *
  162.    * @version 1.0
  163.    * @since   0.1.2
  164.    * @author  Daniel Plücken <daniel@debakel.net>
  165.    * @access  public
  166.    * @param   string  $int 
  167.    * @return  void 
  168.    */
  169.   function setJPGCompression$int )
  170.   {
  171.     $int intval$int );
  172.  
  173.     if $int )
  174.        $int 0;
  175.     else
  176.     if $int 100 )
  177.        $int 100;
  178.  
  179.     $this->jpeg_compression = $int;
  180.   }
  181.  
  182.  
  183.  
  184.   /**
  185.    * Sets the comression quality for jpg between 0 (zero) and 100 (one hundred).
  186.    * This method is an alias for the method setJPGCompression();
  187.    *
  188.    * @version 1.0
  189.    * @since   0.1.2
  190.    * @author  Daniel Plücken <daniel@debakel.net>
  191.    * @access  public
  192.    * @param   string  $int 
  193.    * @return  void 
  194.    */
  195.   function setJPEGCompression$int )
  196.   $this->setJPGCompression$int )}
  197.  
  198.  
  199.  
  200.   /**
  201.    * Scales the image to the given width and keeps the aspect ratio. Trys to
  202.    * preserve transparency info for GIFs & PNGs.
  203.    *
  204.    * @version 1.34
  205.    * @since   0.1.0
  206.    * @author  Daniel Plücken <daniel@debakel.net>
  207.    * @author  Rob <?>
  208.    * @see     http://www.php.net/manual/en/function.imagecolortransparent.php#78379
  209.    *
  210.    * @access  public
  211.    * @param   string  $to_width 
  212.    * @param   string  $smooth 
  213.    *
  214.    * @return  boolean 
  215.    */
  216.   function proportionalWidthScale$to_width$smooth true )
  217.   {
  218.     if $this->handle == null )
  219.        $this->createHandle();
  220.  
  221.     $act_width  $this->getWidth();
  222.     $act_height $this->getHeight();
  223.  
  224.     $dbl_aspect_ratio $act_height $act_width;
  225.     $to_height $to_width $dbl_aspect_ratio;
  226.  
  227.     $this->edit_handle = imagecreatetruecolor$to_width$to_height );
  228.  
  229.     // Transparency only available for GIFs & PNGs
  230.     // (Try to preserve transparency info)
  231.     if $this->kind == "PNG" || $this->kind == "GIF" )
  232.     {
  233.        $trnprt_indx imagecolortransparent$this->handle );
  234.  
  235.        // If we have a specific transparent color
  236.        if $trnprt_indx >= )
  237.        {
  238.             // Get the original image's transparent color's RGB values
  239.             $trnprt_color imagecolorsforindex$this->handle$trnprt_indx );
  240.  
  241.             // Allocate the same color in the new image resource
  242.             $trnprt_indx imagecolorallocate(
  243.                                         $this->edit_handle,
  244.                                         $trnprt_color['red'],
  245.                                         $trnprt_color['green'],
  246.                                         $trnprt_color['blue']
  247.                                              );
  248.  
  249.             // Completely fill the background of the new image with allocated
  250.             // color.
  251.             imagefill$this->edit_handle00$trnprt_indx );
  252.  
  253.             // Set the background color for new image to transparent
  254.             imagecolortransparent$this->edit_handle$trnprt_indx );
  255.  
  256.        }
  257.        // Always make a transparent background color for PNGs that don't have
  258.        // one allocated already
  259.        else
  260.        if $this->kind == "PNG" )
  261.        {
  262.             // Turn off transparency blending (temporarily)
  263.             imagealphablending$this->edit_handlefalse );
  264.  
  265.             // Create a new transparent color for image
  266.             $color imagecolorallocatealpha$this->edit_handle000127 );
  267.  
  268.             // Completely fill the background of the new image with allocated
  269.             // color.
  270.             imagefill$this->edit_handle00$color );
  271.  
  272.             // Restore transparency blending
  273.             imagesavealpha$this->edit_handletrue );
  274.        }
  275.     }
  276.  
  277.     if $smooth )
  278.     {
  279.        $ret imagecopyresampled (
  280.                    $this->edit_handle$this->handle,
  281.                    0000,
  282.                    $to_width$to_height$act_width$act_height
  283.                                  );
  284.        $this->handle = $this->edit_handle;
  285.        $edit_done true;
  286.  
  287.        return $ret;
  288.     }
  289.     else
  290.     {
  291.        $ret imagecopyresized(
  292.                    $this->edit_handle$this->handle,
  293.                    0000,
  294.                    $to_width$to_height$act_width$act_height
  295.                    );
  296.                    $this->handle = $this->edit_handle;
  297.                    $edit_done true;
  298.  
  299.                    return $ret;
  300.     }
  301.   }
  302.  
  303.  
  304.  
  305.   /**
  306.    * Scales the image to the given height and keeps the aspect ratio. Trys to
  307.    * preserve transparency info for GIFs & PNGs.
  308.    *
  309.    * @version 1.22
  310.    * @since   0.1.3
  311.    * @author  Daniel Plücken <daniel@debakel.net>
  312.    * @author  Rob <?>
  313.    * @see     http://www.php.net/manual/en/function.imagecolortransparent.php#78379
  314.    *
  315.    * @access  public
  316.    * @param   string  $to_height 
  317.    * @param   string  $smooth 
  318.    *
  319.    * @return  boolean 
  320.    */
  321.   function proportionalHeightScale$to_height$smooth true )
  322.   {
  323.     if $this->handle == null )
  324.        $this->createHandle();
  325.  
  326.     $act_width  $this->getWidth();
  327.     $act_height $this->getHeight();
  328.  
  329.     $dbl_aspect_ratio $act_width $act_height;
  330.     $to_width $to_height $dbl_aspect_ratio;
  331.  
  332.     $this->edit_handle = imagecreatetruecolor$to_width$to_height );
  333.  
  334.     // Transparency only available for GIFs & PNGs
  335.     // (Try to preserve transparency info)
  336.     if $this->kind == "PNG" || $this->kind == "GIF" )
  337.     {
  338.        $trnprt_indx imagecolortransparent$this->handle );
  339.  
  340.        // If we have a specific transparent color
  341.        if $trnprt_indx >= )
  342.        {
  343.             // Get the original image's transparent color's RGB values
  344.             $trnprt_color imagecolorsforindex$this->handle$trnprt_indx );
  345.  
  346.             // Allocate the same color in the new image resource
  347.             $trnprt_indx imagecolorallocate(
  348.                                         $this->edit_handle,
  349.                                         $trnprt_color['red'],
  350.                                         $trnprt_color['green'],
  351.                                         $trnprt_color['blue']
  352.                                              );
  353.  
  354.             // Completely fill the background of the new image with allocated
  355.             // color.
  356.             imagefill$this->edit_handle00$trnprt_indx );
  357.  
  358.             // Set the background color for new image to transparent
  359.             imagecolortransparent$this->edit_handle$trnprt_indx );
  360.  
  361.        }
  362.        // Always make a transparent background color for PNGs that don't have
  363.        // one allocated already
  364.        else
  365.        if $this->kind == "PNG" )
  366.        {
  367.             // Turn off transparency blending (temporarily)
  368.             imagealphablending$this->edit_handlefalse );
  369.  
  370.             // Create a new transparent color for image
  371.             $color imagecolorallocatealpha$this->edit_handle000127 );
  372.  
  373.             // Completely fill the background of the new image with allocated
  374.             // color.
  375.             imagefill$this->edit_handle00$color );
  376.  
  377.             // Restore transparency blending
  378.             imagesavealpha$this->edit_handletrue );
  379.        }
  380.     }
  381.  
  382.     if $smooth )
  383.     {
  384.        $ret imagecopyresampled (
  385.                    $this->edit_handle$this->handle,
  386.                    0000,
  387.                    $to_width$to_height$act_width$act_height
  388.                                  );
  389.        $this->handle = $this->edit_handle;
  390.        $edit_done true;
  391.  
  392.        return $ret;
  393.     }
  394.     else
  395.     {
  396.        $ret imagecopyresized(
  397.                    $this->edit_handle$this->handle,
  398.                    0000,
  399.                    $to_width$to_height$act_width$act_height
  400.                               );
  401.        $this->handle = $this->edit_handle;
  402.        $edit_done true;
  403.  
  404.        return $ret;
  405.     }
  406.   }
  407.  
  408.  
  409.  
  410.   /**
  411.    * Returns the height of the image.
  412.    *
  413.    * @version 1.1
  414.    * @since   0.1.32
  415.    * @author  Daniel Plücken <daniel@debakel.net>
  416.    * @access  public
  417.    * @return  integer 
  418.    */
  419.   function getHeight()
  420.   {
  421.     if $this->handle != null )
  422.        return imagesy$this->handle );
  423.     else
  424.     {
  425.        $tmp_arr getimagesize$this->path."/".$this->filename );
  426.        return $tmp_arr[1];
  427.     }
  428.   }
  429.  
  430.  
  431.  
  432.   /**
  433.    * Returns the width of the image.
  434.    *
  435.    * @version 1.1
  436.    * @since   0.1.32
  437.    * @author  Daniel Plücken <daniel@debakel.net>
  438.    * @access  public
  439.    * @return  integer 
  440.    */
  441.   function getWidth()
  442.   {
  443.     if $this->handle != null )
  444.        return imagesx$this->handle );
  445.     else
  446.     {
  447.        $tmp_arr getimagesize$this->path."/".$this->filename );
  448.        return $tmp_arr[0];
  449.     }
  450.   }
  451.  
  452.  
  453.  
  454.   /**
  455.    * Returns the content of this image file object.
  456.    *
  457.    * @version 1.11
  458.    * @since   0.1.0
  459.    * @author  Daniel Plücken <daniel@debakel.net>
  460.    * @access  public
  461.    * @return  string 
  462.    */
  463.   function getContent()
  464.   {
  465.     if (
  466.          empty$this->edit_handle )
  467.       && !empty$this->path )
  468.       && !empty$this->filename )
  469.        )
  470.        return FilesystemToolkit
  471.             ::loadFileContent$this->path."/".$this->filename );
  472.  
  473.     ob_start();
  474.     ifempty$this->edit_handle ) )
  475.       $tmp_h =$this->handle;
  476.     else
  477.       $tmp_h =$this->edit_handle;
  478.  
  479.     switch$this->kind )
  480.     {
  481.       case "JPG":
  482.            imagejpeg$tmp_h''$this->jpeg_compression );
  483.            break;
  484.       case "GIF":
  485.            imagegif$tmp_h );
  486.            break;
  487.       case "PNG":
  488.            imagepng$tmp_h );
  489.            break;
  490.     }
  491.     $cont ob_get_contents();
  492.     ob_end_clean();
  493.  
  494.     return $cont;
  495.   }
  496. }
  497. ?>

Documentation generated on Thu, 05 Jun 2008 19:13:32 +0200 by phpDocumentor 1.4.1