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

Source for file BaseDrawing.php

Documentation is available at BaseDrawing.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Worksheet
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31.  
  32. /** PHPExcel_Worksheet */
  33. require_once 'PHPExcel/Worksheet.php';
  34.  
  35. /** PHPExcel_Worksheet_Drawing_Shadow */
  36. require_once 'PHPExcel/Worksheet/Drawing/Shadow.php';
  37.  
  38. /**
  39.  * PHPExcel_Worksheet_BaseDrawing
  40.  *
  41.  * @category   PHPExcel
  42.  * @package    PHPExcel_Worksheet
  43.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  44.  */
  45. class PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  46. {        
  47.     /**
  48.      * Image counter
  49.      *
  50.      * @var int 
  51.      */
  52.     private static $_imageCounter 0;
  53.     
  54.     /**
  55.      * Image index
  56.      *
  57.      * @var int 
  58.      */
  59.     private static $_imageIndex 0;
  60.     
  61.     /**
  62.      * Name
  63.      *
  64.      * @var string 
  65.      */
  66.     protected $_name;
  67.     
  68.     /**
  69.      * Description
  70.      *
  71.      * @var string 
  72.      */
  73.     protected $_description;
  74.     
  75.     /**
  76.      * Worksheet
  77.      *
  78.      * @var PHPExcel_Worksheet 
  79.      */
  80.     protected $_worksheet;
  81.     
  82.     /**
  83.      * Coordinates
  84.      *
  85.      * @var string 
  86.      */
  87.     protected $_coordinates;
  88.     
  89.     /**
  90.      * Offset X
  91.      *
  92.      * @var int 
  93.      */
  94.     protected $_offsetX;
  95.     
  96.     /**
  97.      * Offset Y
  98.      *
  99.      * @var int 
  100.      */
  101.     protected $_offsetY;
  102.     
  103.     /**
  104.      * Width
  105.      *
  106.      * @var int 
  107.      */
  108.     protected $_width;
  109.     
  110.     /**
  111.      * Height
  112.      *
  113.      * @var int 
  114.      */
  115.     protected $_height;
  116.     
  117.     /**
  118.      * Proportional resize
  119.      *
  120.      * @var boolean 
  121.      */
  122.     protected $_resizeProportional;
  123.     
  124.     /**
  125.      * Rotation
  126.      *
  127.      * @var int 
  128.      */
  129.     protected $_rotation;
  130.     
  131.     /**
  132.      * Shadow
  133.      *
  134.      * @var PHPExcel_Worksheet_Drawing_Shadow 
  135.      */
  136.     protected $_shadow;    
  137.     
  138.     /**
  139.      * Create a new PHPExcel_Worksheet_BaseDrawing
  140.      */
  141.     public function __construct()
  142.     {
  143.         // Initialise values
  144.         $this->_name                = '';
  145.         $this->_description            = '';
  146.         $this->_worksheet            = null;
  147.         $this->_coordinates            = 'A1';
  148.         $this->_offsetX                = 0;
  149.         $this->_offsetY                = 0;
  150.         $this->_width                = 0;
  151.         $this->_height                = 0;
  152.         $this->_resizeProportional    = true;
  153.         $this->_rotation            = 0;
  154.         $this->_shadow                = new PHPExcel_Worksheet_Drawing_Shadow();
  155.         
  156.         // Set image index
  157.         self::$_imageCounter++;
  158.         $this->_imageIndex             = self::$_imageCounter;
  159.     }
  160.     
  161.     /**
  162.      * Get image index
  163.      *
  164.      * @return int 
  165.      */
  166.     public function getImageIndex({
  167.         return $this->_imageIndex;
  168.     }
  169.        
  170.     /**
  171.      * Get Name
  172.      *
  173.      * @return string 
  174.      */
  175.     public function getName({
  176.         return $this->_name;
  177.     }
  178.     
  179.     /**
  180.      * Set Name
  181.      *
  182.      * @param string $pValue 
  183.      */
  184.     public function setName($pValue ''{
  185.         $this->_name = $pValue;
  186.     }
  187.     
  188.     /**
  189.      * Get Description
  190.      *
  191.      * @return string 
  192.      */
  193.     public function getDescription({
  194.         return $this->_description;
  195.     }
  196.     
  197.     /**
  198.      * Set Description
  199.      *
  200.      * @param string $pValue 
  201.      */
  202.     public function setDescription($pValue ''{
  203.         $this->_description = $pValue;
  204.     }
  205.  
  206.     /**
  207.      * Get Worksheet
  208.      *
  209.      * @return PHPExcel_Worksheet 
  210.      */
  211.     public function getWorksheet({
  212.         return $this->_worksheet;
  213.     }
  214.     
  215.     /**
  216.      * Set Worksheet
  217.      *
  218.      * @param     PHPExcel_Worksheet     $pValue 
  219.      * @param     bool                $pOverrideOld    If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  220.      * @throws     Exception
  221.      */
  222.     public function setWorksheet(PHPExcel_Worksheet $pValue null$pOverrideOld false{
  223.         if (is_null($this->_worksheet)) {
  224.             // Add drawing to PHPExcel_Worksheet
  225.             $this->_worksheet = $pValue;
  226.             $this->_worksheet->getCell($this->_coordinates);
  227.             $this->_worksheet->getDrawingCollection()->append($this);
  228.         else {
  229.             if ($pOverrideOld{
  230.                 // Remove drawing from old PHPExcel_Worksheet
  231.                 $iterator $this->_worksheet->getDrawingCollection()->getIterator();
  232.                     
  233.                 while ($iterator->valid()) {
  234.                     if ($iterator->current()->getHashCode(== $this->getHashCode()) {
  235.                         $this->_worksheet->getDrawingCollection()->offsetUnset$iterator->key() );
  236.                         $this->_worksheet = null;
  237.                         break;
  238.                     }
  239.                 }
  240.                     
  241.                 // Set new PHPExcel_Worksheet
  242.                 $this->setWorksheet($pValue);
  243.             else {
  244.                 throw new Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
  245.             }
  246.         }
  247.     }
  248.     
  249.     /**
  250.      * Get Coordinates
  251.      *
  252.      * @return string 
  253.      */
  254.     public function getCoordinates({
  255.         return $this->_coordinates;
  256.     }    
  257.     
  258.     /**
  259.      * Set Coordinates
  260.      *
  261.      * @param string $pValue 
  262.      */
  263.     public function setCoordinates($pValue 'A1'{
  264.         $this->_coordinates = $pValue;
  265.     }
  266.     
  267.     /**
  268.      * Get OffsetX
  269.      *
  270.      * @return int 
  271.      */
  272.     public function getOffsetX({
  273.         return $this->_offsetX;
  274.     }
  275.     
  276.     /**
  277.      * Set OffsetX
  278.      *
  279.      * @param int $pValue 
  280.      */
  281.     public function setOffsetX($pValue 0{
  282.         $this->_offsetX = $pValue;
  283.     }
  284.     
  285.     /**
  286.      * Get OffsetY
  287.      *
  288.      * @return int 
  289.      */
  290.     public function getOffsetY({
  291.         return $this->_offsetY;
  292.     }
  293.     
  294.     /**
  295.      * Set OffsetY
  296.      *
  297.      * @param int $pValue 
  298.      */
  299.     public function setOffsetY($pValue 0{
  300.         $this->_offsetY = $pValue;
  301.     }
  302.     
  303.     /**
  304.      * Get Width
  305.      *
  306.      * @return int 
  307.      */
  308.     public function getWidth({
  309.         return $this->_width;
  310.     }
  311.     
  312.     /**
  313.      * Set Width
  314.      *
  315.      * @param int $pValue 
  316.      */
  317.     public function setWidth($pValue 0{
  318.         // Resize proportional?
  319.         if ($this->_resizeProportional && $pValue != 0{
  320.             $ratio $this->_height / $this->_width;            
  321.             $this->_height = round($ratio $pValue);
  322.         }
  323.         
  324.         // Set width
  325.         $this->_width = $pValue;
  326.     }
  327.     
  328.     /**
  329.      * Get Height
  330.      *
  331.      * @return int 
  332.      */
  333.     public function getHeight({
  334.         return $this->_height;
  335.     }
  336.     
  337.     /**
  338.      * Set Height
  339.      *
  340.      * @param int $pValue 
  341.      */
  342.     public function setHeight($pValue 0{
  343.         // Resize proportional?
  344.         if ($this->_resizeProportional && $pValue != 0{
  345.             $ratio $this->_width / $this->_height;           
  346.             $this->_width = round($ratio $pValue);
  347.         }
  348.         
  349.         // Set height
  350.         $this->_height = $pValue;
  351.     }
  352.     
  353.     /**
  354.      * Set width and height with proportional resize
  355.      * @author Vincent@luo MSN:kele_100@hotmail.com
  356.      * @param int $width 
  357.      * @param int $height 
  358.      * @example $objDrawing->setResizeProportional(true);
  359.      * @example $objDrawing->setWidthAndHeight(160,120);
  360.      */
  361.     public function setWidthAndHeight($width 0$height 0{
  362.         $xratio $width $this->_width;
  363.         $yratio $height $this->_height;
  364.         if ($this->_resizeProportional && !($width == || $height == 0)) {
  365.             if (($xratio $this->_height$height{
  366.                 $this->_height = ceil($xratio $this->_height);
  367.                 $this->_width  = $width;
  368.             else {
  369.                 $this->_width    = ceil($yratio $this->_width);
  370.                 $this->_height    = $height;
  371.             }
  372.         }
  373.     }
  374.     
  375.     /**
  376.      * Get ResizeProportional
  377.      *
  378.      * @return boolean 
  379.      */
  380.     public function getResizeProportional({
  381.         return $this->_resizeProportional;
  382.     }
  383.     
  384.     /**
  385.      * Set ResizeProportional
  386.      *
  387.      * @param boolean $pValue 
  388.      */
  389.     public function setResizeProportional($pValue true{
  390.         $this->_resizeProportional = $pValue;
  391.     }
  392.     
  393.     /**
  394.      * Get Rotation
  395.      *
  396.      * @return int 
  397.      */
  398.     public function getRotation({
  399.         return $this->_rotation;
  400.     }
  401.     
  402.     /**
  403.      * Set Rotation
  404.      *
  405.      * @param int $pValue 
  406.      */
  407.     public function setRotation($pValue 0{
  408.         $this->_rotation = $pValue;
  409.     }
  410.     
  411.     /**
  412.      * Get Shadow
  413.      *
  414.      * @return PHPExcel_Worksheet_Drawing_Shadow 
  415.      */
  416.     public function getShadow({
  417.         return $this->_shadow;
  418.     }
  419.     
  420.     /**
  421.      * Set Shadow
  422.      *
  423.      * @param     PHPExcel_Worksheet_Drawing_Shadow $pValue 
  424.      * @throws     Exception
  425.      */
  426.     public function setShadow(PHPExcel_Worksheet_Drawing_Shadow $pValue null{
  427.            $this->_shadow = $pValue;
  428.     }
  429.  
  430.     /**
  431.      * Get hash code
  432.      *
  433.      * @return string    Hash code
  434.      */    
  435.     public function getHashCode({
  436.         return md5(
  437.               $this->_name
  438.             . $this->_description
  439.             . $this->_worksheet->getHashCode()
  440.             . $this->_coordinates
  441.             . $this->_offsetX
  442.             . $this->_offsetY
  443.             . $this->_width
  444.             . $this->_height
  445.             . $this->_rotation
  446.             . $this->_shadow->getHashCode()
  447.             . __CLASS__
  448.         );
  449.     }
  450.     
  451.     /**
  452.      * Hash index
  453.      *
  454.      * @var string 
  455.      */
  456.     private $_hashIndex;
  457.     
  458.     /**
  459.      * Get hash index
  460.      * 
  461.      * Note that this index may vary during script execution! Only reliable moment is
  462.      * while doing a write of a workbook and when changes are not allowed.
  463.      *
  464.      * @return string    Hash index
  465.      */
  466.     public function getHashIndex({
  467.         return $this->_hashIndex;
  468.     }
  469.     
  470.     /**
  471.      * Set hash index
  472.      * 
  473.      * Note that this index may vary during script execution! Only reliable moment is
  474.      * while doing a write of a workbook and when changes are not allowed.
  475.      *
  476.      * @param string    $value    Hash index
  477.      */
  478.     public function setHashIndex($value{
  479.         $this->_hashIndex = $value;
  480.     }
  481.         
  482.     /**
  483.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  484.      */
  485.     public function __clone({
  486.         $vars get_object_vars($this);
  487.         foreach ($vars as $key => $value{
  488.             if (is_object($value)) {
  489.                 $this->$key clone $value;
  490.             else {
  491.                 $this->$key $value;
  492.             }
  493.         }
  494.     }
  495. }

Documentation generated on Mon, 05 Jan 2009 20:36:25 +0100 by phpDocumentor 1.4.1