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

Source for file Drawing.php

Documentation is available at Drawing.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 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_Shared
  23.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Shared_Drawing
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared
  34.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Convert pixels to EMU
  39.      *
  40.      * @param     int $pValue    Value in pixels
  41.      * @return     int            Value in EMU
  42.      */
  43.     public static function pixelsToEMU($pValue 0{
  44.         return round($pValue 9525);
  45.     }
  46.  
  47.     /**
  48.      * Convert EMU to pixels
  49.      *
  50.      * @param     int $pValue    Value in EMU
  51.      * @return     int            Value in pixels
  52.      */
  53.     public static function EMUToPixels($pValue 0{
  54.         if ($pValue != 0{
  55.             return round($pValue 9525);
  56.         else {
  57.             return 0;
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Convert pixels to column width. Exact algorithm not known.
  63.      * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
  64.      * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
  65.      *
  66.      * @param     int $pValue    Value in pixels
  67.      * @param     PHPExcel_Style_Font $pDefaultFont    Default font of the workbook
  68.      * @return     int            Value in cell dimension
  69.      */
  70.     public static function pixelsToCellDimension($pValue 0PHPExcel_Style_Font $pDefaultFont{
  71.         // Font name and size
  72.         $name $pDefaultFont->getName();
  73.         $size $pDefaultFont->getSize();
  74.  
  75.         if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  76.             // Exact width can be determined
  77.             $colWidth $pValue
  78.                 * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width']
  79.                 / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'];
  80.         else {
  81.             // We don't have data for this particular font and size, use approximation by
  82.             // extrapolating from Calibri 11
  83.             $colWidth $pValue 11
  84.                 * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width']
  85.                 / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'$size;
  86.         }
  87.  
  88.         return $colWidth;
  89.     }
  90.  
  91.     /**
  92.      * Convert column width from (intrinsic) Excel units to pixels
  93.      *
  94.      * @param     float    $pValue        Value in cell dimension
  95.      * @param     PHPExcel_Style_Font $pDefaultFont    Default font of the workbook
  96.      * @return     int        Value in pixels
  97.      */
  98.     public static function cellDimensionToPixels($pValue 0PHPExcel_Style_Font $pDefaultFont{
  99.         // Font name and size
  100.         $name $pDefaultFont->getName();
  101.         $size $pDefaultFont->getSize();
  102.  
  103.         if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  104.             // Exact width can be determined
  105.             $colWidth $pValue
  106.                 * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px']
  107.                 / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'];
  108.  
  109.         else {
  110.             // We don't have data for this particular font and size, use approximation by
  111.             // extrapolating from Calibri 11
  112.             $colWidth $pValue $size
  113.                 * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px']
  114.                 / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'11;
  115.         }
  116.  
  117.         // Round pixels to closest integer
  118.         $colWidth = (int) round($colWidth);
  119.  
  120.         return $colWidth;
  121.     }
  122.  
  123.     /**
  124.      * Convert pixels to points
  125.      *
  126.      * @param     int $pValue    Value in pixels
  127.      * @return     int            Value in points
  128.      */
  129.     public static function pixelsToPoints($pValue 0{
  130.         return $pValue 0.67777777;
  131.     }
  132.  
  133.     /**
  134.      * Convert points to pixels
  135.      *
  136.      * @param     int $pValue    Value in points
  137.      * @return     int            Value in pixels
  138.      */
  139.     public static function pointsToPixels($pValue 0{
  140.         if ($pValue != 0{
  141.             return (int) ceil($pValue 1.333333333);
  142.         else {
  143.             return 0;
  144.         }
  145.     }
  146.  
  147.     /**
  148.      * Convert degrees to angle
  149.      *
  150.      * @param     int $pValue    Degrees
  151.      * @return     int            Angle
  152.      */
  153.     public static function degreesToAngle($pValue 0{
  154.         return (int)round($pValue 60000);
  155.     }
  156.  
  157.     /**
  158.      * Convert angle to degrees
  159.      *
  160.      * @param     int $pValue    Angle
  161.      * @return     int            Degrees
  162.      */
  163.     public static function angleToDegrees($pValue 0{
  164.         if ($pValue != 0{
  165.             return round($pValue 60000);
  166.         else {
  167.             return 0;
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Create a new image from file. By alexander at alexauto dot nl
  173.      *
  174.      * @link http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214
  175.      * @param string $filename Path to Windows DIB (BMP) image
  176.      * @return resource 
  177.      */
  178.     public static function imagecreatefrombmp($p_sFile)
  179.     {
  180.         //    Load the image into a string
  181.         $file    =    fopen($p_sFile,"rb");
  182.         $read    =    fread($file,10);
  183.         while(!feof($file)&&($read<>""))
  184.             $read    .=    fread($file,1024);
  185.        
  186.         $temp    =    unpack("H*",$read);
  187.         $hex    =    $temp[1];
  188.         $header    =    substr($hex,0,108);
  189.        
  190.         //    Process the header
  191.         //    Structure: http://www.fastgraph.com/help/bmp_header_format.html
  192.         if (substr($header,0,4)=="424d")
  193.         {
  194.             //    Cut it in parts of 2 bytes
  195.             $header_parts    =    str_split($header,2);
  196.            
  197.             //    Get the width        4 bytes
  198.             $width            =    hexdec($header_parts[19].$header_parts[18]);
  199.            
  200.             //    Get the height        4 bytes
  201.             $height            =    hexdec($header_parts[23].$header_parts[22]);
  202.            
  203.             //    Unset the header params
  204.             unset($header_parts);
  205.         }
  206.        
  207.         //    Define starting X and Y
  208.         $x                =    0;
  209.         $y                =    1;
  210.        
  211.         //    Create newimage
  212.         $image            =    imagecreatetruecolor($width,$height);
  213.        
  214.         //    Grab the body from the image
  215.         $body            =    substr($hex,108);
  216.  
  217.         //    Calculate if padding at the end-line is needed
  218.         //    Divided by two to keep overview.
  219.         //    1 byte = 2 HEX-chars
  220.         $body_size        =    (strlen($body)/2);
  221.         $header_size    =    ($width*$height);
  222.  
  223.         //    Use end-line padding? Only when needed
  224.         $usePadding        =    ($body_size>($header_size*3)+4);
  225.        
  226.         //    Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
  227.         //    Calculate the next DWORD-position in the body
  228.         for ($i=0;$i<$body_size;$i+=3)
  229.         {
  230.             //    Calculate line-ending and padding
  231.             if ($x>=$width)
  232.             {
  233.                 //    If padding needed, ignore image-padding
  234.                 //    Shift i to the ending of the current 32-bit-block
  235.                 if ($usePadding)
  236.                     $i    +=    $width%4;
  237.                
  238.                 //    Reset horizontal position
  239.                 $x    =    0;
  240.                
  241.                 //    Raise the height-position (bottom-up)
  242.                 $y++;
  243.                
  244.                 //    Reached the image-height? Break the for-loop
  245.                 if ($y>$height)
  246.                     break;
  247.             }
  248.            
  249.             //    Calculation of the RGB-pixel (defined as BGR in image-data)
  250.             //    Define $i_pos as absolute position in the body
  251.             $i_pos    =    $i*2;
  252.             $r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]);
  253.             $g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]);
  254.             $b        =    hexdec($body[$i_pos].$body[$i_pos+1]);
  255.            
  256.             //    Calculate and draw the pixel
  257.             $color    =    imagecolorallocate($image,$r,$g,$b);
  258.             imagesetpixel($image,$x,$height-$y,$color);
  259.            
  260.             //    Raise the horizontal position
  261.             $x++;
  262.         }
  263.        
  264.         //    Unset the body / free the memory
  265.         unset($body);
  266.        
  267.         //    Return image-object
  268.         return $image;
  269.     }
  270.  
  271. }

Documentation generated on Sun, 27 Feb 2011 16:30:19 -0800 by phpDocumentor 1.4.3