File indexing completed on 2025-01-26 05:29:15

0001 <?php
0002 
0003 namespace Intervention\Image\Imagick;
0004 
0005 use Intervention\Image\Image;
0006 
0007 class Font extends \Intervention\Image\AbstractFont
0008 {
0009     /**
0010      * Draws font to given image at given position
0011      *
0012      * @param  Image   $image
0013      * @param  int     $posx
0014      * @param  int     $posy
0015      * @return void
0016      */
0017     public function applyToImage(Image $image, $posx = 0, $posy = 0)
0018     {
0019         // build draw object
0020         $draw = new \ImagickDraw();
0021         $draw->setStrokeAntialias(true);
0022         $draw->setTextAntialias(true);
0023 
0024         // set font file
0025         if ($this->hasApplicableFontFile()) {
0026             $draw->setFont($this->file);
0027         } else {
0028             throw new \Intervention\Image\Exception\RuntimeException(
0029                 "Font file must be provided to apply text to image."
0030             );
0031         }
0032 
0033         // parse text color
0034         $color = new Color($this->color);
0035 
0036         $draw->setFontSize($this->size);
0037         $draw->setFillColor($color->getPixel());
0038 
0039         // align horizontal
0040         switch (strtolower($this->align)) {
0041             case 'center':
0042                 $align = \Imagick::ALIGN_CENTER;
0043                 break;
0044 
0045             case 'right':
0046                 $align = \Imagick::ALIGN_RIGHT;
0047                 break;
0048 
0049             default:
0050                 $align = \Imagick::ALIGN_LEFT;
0051                 break;
0052         }
0053 
0054         $draw->setTextAlignment($align);
0055 
0056         // align vertical
0057         if (strtolower($this->valign) != 'bottom') {
0058 
0059             // calculate box size
0060             $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text);
0061 
0062             // corrections on y-position
0063             switch (strtolower($this->valign)) {
0064                 case 'center':
0065                 case 'middle':
0066                 $posy = $posy + $dimensions['textHeight'] * 0.65 / 2;
0067                 break;
0068 
0069                 case 'top':
0070                 $posy = $posy + $dimensions['textHeight'] * 0.65;
0071                 break;
0072             }
0073         }
0074 
0075         // apply to image
0076         $image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * (-1), $this->text);
0077     }
0078     
0079     /**
0080      * Calculates bounding box of current font setting
0081      *
0082      * @return array
0083      */
0084     public function getBoxSize()
0085     {
0086         $box = [];
0087 
0088         // build draw object
0089         $draw = new \ImagickDraw();
0090         $draw->setStrokeAntialias(true);
0091         $draw->setTextAntialias(true);
0092 
0093         // set font file
0094         if ($this->hasApplicableFontFile()) {
0095             $draw->setFont($this->file);
0096         } else {
0097             throw new \Intervention\Image\Exception\RuntimeException(
0098                 "Font file must be provided to apply text to image."
0099             );
0100         }
0101 
0102         $draw->setFontSize($this->size);
0103 
0104         $dimensions = (new \Imagick())->queryFontMetrics($draw, $this->text);
0105 
0106         if (strlen($this->text) == 0) {
0107             // no text -> no boxsize
0108             $box['width'] = 0;
0109             $box['height'] = 0;
0110         } else {
0111             // get boxsize
0112             $box['width'] = intval(abs($dimensions['textWidth']));
0113             $box['height'] = intval(abs($dimensions['textHeight']));
0114         }
0115 
0116         return $box;
0117     }
0118 }