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

Documentation generated on Mon, 11 Jan 2010 08:06:26 +0100 by phpDocumentor 1.4.1