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

Documentation generated on Mon, 25 Aug 2008 13:30:23 +0200 by phpDocumentor 1.4.1