Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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