Beispiel #1
0
 public function drawText($text, $x = 0, $y = 0, array $options = null)
 {
     if (isset($options)) {
         $this->setOptions($options);
     }
     $colorRgb = Utility::htmlToRgb($this->options['fontColor']);
     $color = 'rgb(' . implode(', ', $colorRgb) . ')';
     $attributes['x'] = $x;
     $attributes['y'] = $y;
     $attributes['fill'] = $color;
     $attributes['font-size'] = $this->options['fontSize'] * 1.2;
     switch ($this->options['textAlign']) {
         case 'center':
             $textAnchor = 'middle';
             break;
         case 'right':
             $textAnchor = 'end';
             break;
         case 'left':
         default:
             $textAnchor = 'start';
             break;
     }
     switch ($this->options['textValign']) {
         case 'top':
             $attributes['y'] += $this->options['fontSize'];
             break;
         case 'middle':
             $attributes['y'] += $this->options['fontSize'] / 2;
             break;
         case 'bottom':
         default:
             $attributes['y'] += 0;
             break;
     }
     $attributes['style'] = 'text-anchor: ' . $textAnchor;
     $attributes['transform'] = 'rotate(' . -$this->options['textOrientation'] . ', ' . $x . ', ' . $y . ')';
     $this->appendRootElement('text', $attributes, html_entity_decode($text, ENT_COMPAT | ENT_HTML5, 'UTF-8'));
 }
Beispiel #2
0
 /**
  * Draw text string.
  * 
  * @param string $text Text for drawing
  * @param int $x x-coordinate
  * @param int $y y-coordinate
  * @param array $options
  * @throws Exception\RuntimeException
  */
 public function drawText($text, $x = 0, $y = 0, array $options = null)
 {
     $this->setOptions($options);
     $colorRgb = Utility::htmlToRgb($this->optionFontColor);
     $color = $this->allocateColor($this->Resource, $colorRgb['r'], $colorRgb['g'], $colorRgb['b']);
     if ($this->optionFontName == null) {
         $this->optionFontName = 3;
     }
     if (is_numeric($this->optionFontName)) {
         if ($this->optionTextOrientation) {
             throw new Exception\RuntimeException('No orientation possible with GD internal font.');
         }
         $fontWidth = imagefontwidth($this->optionFontName);
         $fontHeight = imagefontheight($this->optionFontName);
         switch ($this->optionTextAlign) {
             case 'left':
                 $positionX = $x;
                 break;
             case 'center':
                 $positionX = $x - ceil($fontWidth * strlen($text) / 2);
                 break;
             case 'right':
                 $positionX = $x - $fontWidth * strlen($text);
                 break;
         }
         switch ($this->optionTextValign) {
             case 'top':
                 $positionY = $y;
                 break;
             case 'middle':
                 $positionY = $y - $fontHeight / 2;
                 break;
             case 'bottom':
                 $positionY = $y - $fontHeight + 1;
                 break;
         }
         imagestring($this->Resource, $this->optionFontName, $positionX, $positionY, $text, $color);
     } else {
         if (!function_exists('imagettfbbox')) {
             throw new Exception\RuntimeException('A font was provided, but this instance of PHP does not have TTF (FreeType) support');
         }
         $box = imagettfbbox($this->optionFontSize, 0, $this->optionFontName, $text);
         switch ($this->optionTextAlign) {
             case 'center':
                 $width = ($box[2] - $box[0]) / 2;
                 break;
             case 'right':
                 $width = $box[2] - $box[0];
                 break;
             case 'left':
             default:
                 $width = 0;
                 break;
         }
         switch ($this->optionTextValign) {
             case 'top':
                 $height = $box[1] - $box[7];
                 break;
             case 'middle':
                 $height = ($box[1] - $box[7]) / 2;
                 break;
             case 'bottom':
             default:
                 $height = 0;
                 break;
         }
         imagettftext($this->Resource, $this->optionFontSize, $this->optionTextOrientation, $x - $width * cos(pi() * $this->optionTextOrientation / 180) + $height * sin(pi() * $this->optionTextOrientation / 180), $y + $height * cos(pi() * $this->optionTextOrientation / 180) + $width * sin(pi() * $this->optionTextOrientation / 180), $color, $this->optionFontName, $text);
     }
 }