original_string: public function renderToImage(\NMC\ImageWithText\Image $image)
      {
          // Allocate words to lines
          $this->distributeText();
  
          // Calculate maximum potential line width (close enough) in pixels
          $maxWidthString = implode('', array_fill(0, $this->width, 'x'));
          $maxWidthBoundingBox = imagettfbbox($this->size, 0, $this->font, $maxWidthString);
          $maxLineWidth = abs($maxWidthBoundingBox[0] - $maxWidthBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
  
          // Calculate each line width in pixels for alignment purposes
          for ($j = 0; $j < count($this->lines); $j++) {
              // Fetch line
              $line =& $this->lines[$j];
  
              // Remove unused lines
              if (empty($line['words'])) {
                  unset($this->lines[$j]);
                  continue;
              }
  
              // Calculate width
              $lineText = implode(' ', $line['words']);
              $lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $lineText);
              $line['width'] = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
              $line['text'] = $lineText;
          }
  
          // Calculate line offsets
          for ($i = 0; $i < count($this->lines); $i++) {
              // Fetch line
              if (array_key_exists($i, $this->lines)) {
                  $line =& $this->lines[$i];
  
                  // Calculate line width in pixels
                  $lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $line['text']);
                  $lineWidth = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
  
                  // Calculate line X,Y offsets in pixels
                  switch ($this->align) {
                      case 'left':
                          $offsetX = $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                      case 'center':
                          $imageWidth = $image->getWidth();
                          $offsetX = (($maxLineWidth - $lineWidth) / 2) + $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                      case 'right':
                          $imageWidth = $image->getWidth();
                          $offsetX = $imageWidth - $line['width'] - $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                  }
  
                  // Render text onto image
                  $image->getImage()->text($line['text'], $offsetX, $offsetY, $this->size, $this->color, 0, $this->font);
              }
          }
      }
code: public function renderToImage(\NMC\ImageWithText\Image $image)
      {
          // Allocate words to lines
          $this->distributeText();
  
          // Calculate maximum potential line width (close enough) in pixels
          $maxWidthString = implode('', array_fill(0, $this->width, 'x'));
          $maxWidthBoundingBox = imagettfbbox($this->size, 0, $this->font, $maxWidthString);
          $maxLineWidth = abs($maxWidthBoundingBox[0] - $maxWidthBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
  
          // Calculate each line width in pixels for alignment purposes
          for ($j = 0; $j < count($this->lines); $j++) {
              // Fetch line
              $line =& $this->lines[$j];
  
              // Remove unused lines
              if (empty($line['words'])) {
                  unset($this->lines[$j]);
                  continue;
              }
  
              // Calculate width
              $lineText = implode(' ', $line['words']);
              $lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $lineText);
              $line['width'] = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
              $line['text'] = $lineText;
          }
  
          // Calculate line offsets
          for ($i = 0; $i < count($this->lines); $i++) {
              // Fetch line
              if (array_key_exists($i, $this->lines)) {
                  $line =& $this->lines[$i];
  
                  // Calculate line width in pixels
                  $lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $line['text']);
                  $lineWidth = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)
  
                  // Calculate line X,Y offsets in pixels
                  switch ($this->align) {
                      case 'left':
                          $offsetX = $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                      case 'center':
                          $imageWidth = $image->getWidth();
                          $offsetX = (($maxLineWidth - $lineWidth) / 2) + $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                      case 'right':
                          $imageWidth = $image->getWidth();
                          $offsetX = $imageWidth - $line['width'] - $this->startX;
                          $offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
                          break;
                  }
  
                  // Render text onto image
                  $image->getImage()->text($line['text'], $offsetX, $offsetY, $this->size, $this->color, 0, $this->font);
              }
          }
      }
code_tokens: public function renderToImage ( \ NMC \ ImageWithText \ Image $ image ) { // Allocate words to lines $ this -> distributeText ( ) ; // Calculate maximum potential line width (close enough) in pixels $ maxWidthString = implode ( '' , array_fill ( 0 , $ this -> width , 'x' ) ) ; $ maxWidthBoundingBox = imagettfbbox ( $ this -> size , 0 , $ this -> font , $ maxWidthString ) ; $ maxLineWidth = abs ( $ maxWidthBoundingBox [ 0 ] - $ maxWidthBoundingBox [ 2 ] ) ; // (lower left corner, X position) - (lower right corner, X position) // Calculate each line width in pixels for alignment purposes for ( $ j = 0 ; $ j < count ( $ this -> lines ) ; $ j ++ ) { // Fetch line $ line = & $ this -> lines [ $ j ] ; // Remove unused lines if ( empty ( $ line [ 'words' ] ) ) { unset ( $ this -> lines [ $ j ] ) ; continue ; } // Calculate width $ lineText = implode ( ' ' , $ line [ 'words' ] ) ; $ lineBoundingBox = imagettfbbox ( $ this -> size , 0 , $ this -> font , $ lineText ) ; $ line [ 'width' ] = abs ( $ lineBoundingBox [ 0 ] - $ lineBoundingBox [ 2 ] ) ; // (lower left corner, X position) - (lower right corner, X position) $ line [ 'text' ] = $ lineText ; } // Calculate line offsets for ( $ i = 0 ; $ i < count ( $ this -> lines ) ; $ i ++ ) { // Fetch line if ( array_key_exists ( $ i , $ this -> lines ) ) { $ line = & $ this -> lines [ $ i ] ; // Calculate line width in pixels $ lineBoundingBox = imagettfbbox ( $ this -> size , 0 , $ this -> font , $ line [ 'text' ] ) ; $ lineWidth = abs ( $ lineBoundingBox [ 0 ] - $ lineBoundingBox [ 2 ] ) ; // (lower left corner, X position) - (lower right corner, X position) // Calculate line X,Y offsets in pixels switch ( $ this -> align ) { case 'left' : $ offsetX = $ this -> startX ; $ offsetY = $ this -> startY + $ this -> lineHeight + ( $ this -> lineHeight * $ i ) ; break ; case 'center' : $ imageWidth = $ image -> getWidth ( ) ; $ offsetX = ( ( $ maxLineWidth - $ lineWidth ) / 2 ) + $ this -> startX ; $ offsetY = $ this -> startY + $ this -> lineHeight + ( $ this -> lineHeight * $ i ) ; break ; case 'right' : $ imageWidth = $ image -> getWidth ( ) ; $ offsetX = $ imageWidth - $ line [ 'width' ] - $ this -> startX ; $ offsetY = $ this -> startY + $ this -> lineHeight + ( $ this -> lineHeight * $ i ) ; break ; } // Render text onto image $ image -> getImage ( ) -> text ( $ line [ 'text' ] , $ offsetX , $ offsetY , $ this -> size , $ this -> color , 0 , $ this -> font ) ; } } }
docstring: Render text on image
  @param \Intervention\Image\Image $image The image on which the text will be rendered
  @api
docstring_tokens: Render text on image