[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/ -> Drawing.php (source)

   1  <?php
   2  /**
   3   * PHPExcel
   4   *
   5   * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25   * @version    ##VERSION##, ##DATE##
  26   */
  27  
  28  
  29  /**
  30   * PHPExcel_Shared_Drawing
  31   *
  32   * @category   PHPExcel
  33   * @package    PHPExcel_Shared
  34   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  35   */
  36  class PHPExcel_Shared_Drawing
  37  {
  38      /**
  39       * Convert pixels to EMU
  40       *
  41       * @param     int $pValue    Value in pixels
  42       * @return     int            Value in EMU
  43       */
  44      public static function pixelsToEMU($pValue = 0)
  45      {
  46          return round($pValue * 9525);
  47      }
  48  
  49      /**
  50       * Convert EMU to pixels
  51       *
  52       * @param     int $pValue    Value in EMU
  53       * @return     int            Value in pixels
  54       */
  55      public static function EMUToPixels($pValue = 0)
  56      {
  57          if ($pValue != 0) {
  58              return round($pValue / 9525);
  59          } else {
  60              return 0;
  61          }
  62      }
  63  
  64      /**
  65       * Convert pixels to column width. Exact algorithm not known.
  66       * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
  67       * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
  68       *
  69       * @param     int $pValue    Value in pixels
  70       * @param     PHPExcel_Style_Font $pDefaultFont    Default font of the workbook
  71       * @return     int            Value in cell dimension
  72       */
  73      public static function pixelsToCellDimension($pValue = 0, PHPExcel_Style_Font $pDefaultFont)
  74      {
  75          // Font name and size
  76          $name = $pDefaultFont->getName();
  77          $size = $pDefaultFont->getSize();
  78  
  79          if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  80              // Exact width can be determined
  81              $colWidth = $pValue * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'] / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'];
  82          } else {
  83              // We don't have data for this particular font and size, use approximation by
  84              // extrapolating from Calibri 11
  85              $colWidth = $pValue * 11 * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / 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 = 0, PHPExcel_Style_Font $pDefaultFont)
  99      {
 100          // Font name and size
 101          $name = $pDefaultFont->getName();
 102          $size = $pDefaultFont->getSize();
 103  
 104          if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
 105              // Exact width can be determined
 106              $colWidth = $pValue * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'] / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'];
 107          } else {
 108              // We don't have data for this particular font and size, use approximation by
 109              // extrapolating from Calibri 11
 110              $colWidth = $pValue * $size * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'] / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / 11;
 111          }
 112  
 113          // Round pixels to closest integer
 114          $colWidth = (int) round($colWidth);
 115  
 116          return $colWidth;
 117      }
 118  
 119      /**
 120       * Convert pixels to points
 121       *
 122       * @param     int $pValue    Value in pixels
 123       * @return     int            Value in points
 124       */
 125      public static function pixelsToPoints($pValue = 0)
 126      {
 127          return $pValue * 0.67777777;
 128      }
 129  
 130      /**
 131       * Convert points to pixels
 132       *
 133       * @param     int $pValue    Value in points
 134       * @return     int            Value in pixels
 135       */
 136      public static function pointsToPixels($pValue = 0)
 137      {
 138          if ($pValue != 0) {
 139              return (int) ceil($pValue * 1.333333333);
 140          } else {
 141              return 0;
 142          }
 143      }
 144  
 145      /**
 146       * Convert degrees to angle
 147       *
 148       * @param     int $pValue    Degrees
 149       * @return     int            Angle
 150       */
 151      public static function degreesToAngle($pValue = 0)
 152      {
 153          return (int)round($pValue * 60000);
 154      }
 155  
 156      /**
 157       * Convert angle to degrees
 158       *
 159       * @param     int $pValue    Angle
 160       * @return     int            Degrees
 161       */
 162      public static function angleToDegrees($pValue = 0)
 163      {
 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  
 187          $temp = unpack("H*", $read);
 188          $hex = $temp[1];
 189          $header = substr($hex, 0, 108);
 190  
 191          //    Process the header
 192          //    Structure: http://www.fastgraph.com/help/bmp_header_format.html
 193          if (substr($header, 0, 4)=="424d") {
 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              //    Calculate line-ending and padding
 230              if ($x >= $width) {
 231                  // If padding needed, ignore image-padding
 232                  // Shift i to the ending of the current 32-bit-block
 233                  if ($usePadding) {
 234                      $i += $width%4;
 235                  }
 236  
 237                  //    Reset horizontal position
 238                  $x = 0;
 239  
 240                  //    Raise the height-position (bottom-up)
 241                  $y++;
 242  
 243                  //    Reached the image-height? Break the for-loop
 244                  if ($y > $height) {
 245                      break;
 246                  }
 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  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1