/** * Set properties for text drawing on the image. * * @param $fontFile string path to the font file on server * @param $size int font size to use * @param $color string font color to use in hex format * * @return null */ public function setFontProperties($fontFile, $size, $color) { if (empty($this->_palette)) { $this->_palette = new \Imagine\Image\Palette\RGB(); } $this->_font = $this->_instance->font($fontFile, $size, $this->_palette->color($color)); }
public function getImagineImage(ImagineInterface $imagine, FontCollection $fontCollection, $width, $height) { $fontPath = $fontCollection->getFontById($this->font)->getPath(); if ($this->getAbsoluteSize() !== null) { $fontSize = $this->getAbsoluteSize(); } elseif ($this->getRelativeSize() !== null) { $fontSize = (int) $this->getRelativeSize() / 100 * $height; } else { throw new \LogicException('Either relative or absolute watermark size must be set!'); } if (true || !class_exists('ImagickDraw')) { // Fall back to ugly image. $palette = new \Imagine\Image\Palette\RGB(); $font = $imagine->font($fontPath, $fontSize, $palette->color('#000')); $box = $font->box($this->getText()); $watermarkImage = $imagine->create($box, $palette->color('#FFF')); $watermarkImage->draw()->text($this->text, $font, new \Imagine\Image\Point(0, 0)); } else { // CURRENTLY DISABLED. // Use nicer Imagick implementation. // Untested! // @todo Test and implement it! $draw = new \ImagickDraw(); $draw->setFont($fontPath); $draw->setFontSize($fontSize); $draw->setStrokeAntialias(true); //try with and without $draw->setTextAntialias(true); //try with and without $draw->setFillColor('#fff'); $textOnly = new \Imagick(); $textOnly->newImage(1400, 400, "transparent"); //transparent canvas $textOnly->annotateImage($draw, 0, 0, 0, $this->text); //Create stroke $draw->setFillColor('#000'); //same as stroke color $draw->setStrokeColor('#000'); $draw->setStrokeWidth(8); $strokeImage = new \Imagick(); $strokeImage->newImage(1400, 400, "transparent"); $strokeImage->annotateImage($draw, 0, 0, 0, $this->text); //Composite text over stroke $strokeImage->compositeImage($textOnly, \Imagick::COMPOSITE_OVER, 0, 0, \Imagick::CHANNEL_ALPHA); $strokeImage->trimImage(0); //cut transparent border $watermarkImage = $imagine->load($strokeImage->getImageBlob()); //$strokeImage->resizeImage(300,0, \Imagick::FILTER_CATROM, 0.9, false); //resize to final size } return $watermarkImage; }
/** * * @param int $fontSize * @param int $angle * @param string $fontFace * @param string $string * @param int $width * @return array */ protected static function wrap(ImagineInterface $imagine, $fontSize, $angle, $fontFace, $string, $width) { static $palette; if (null === $palette) { $palette = new RGB(); } // str 'Op' used to calculate linespace $font = $imagine->font($fontFace, $fontSize, $palette->color("000000", 0)); $testbox = $font->box("0p", $angle); $height = $testbox->getHeight(); $testbox = $font->box("M", $angle); // 1 em $dy = $testbox->getHeight(); $toth = 0; $ret = []; foreach (explode("\n", $string) as $lig) { if ($lig == '') { $ret[] = ['w' => 0, 'h' => $dy, 't' => '']; $toth += $dy; } else { $twords = []; $iword = -1; $lastc = ''; $length = strlen($lig); for ($i = 0; $i < $length; $i++) { $c = $lig[$i]; if ($iword == -1 || ctype_space($c) && !ctype_space($lastc)) { $twords[++$iword] = [$part = 0 => '', 1 => '']; } if (!ctype_space($c) && $part == 0) { $part++; } $twords[$iword][$part] .= $lastc = $c; } if ($iword >= 0 && $twords[0][1] != '') { $buff = ''; $lastw = $lasth = 0; foreach ($twords as $i => $wrd) { $test = $buff . $wrd[0] . $wrd[1]; $testbox = $font->box($test, $angle); $w = $testbox->getWidth(); $h = $testbox->getHeight(); if ($i > 0 && $testbox->getWidth() > $width) { $ret[] = ['w' => $lastw, 'h' => $lasth, 't' => $buff]; $toth += $lasth; $buff = $wrd[1]; } else { $buff = $test; } $lastw = $w; $lasth = $h; } if ($buff != '') { $ret[] = ['w' => $lastw, 'h' => $lasth, 't' => $buff]; $toth += $lasth; } } } } return ['toth' => $toth, 'l' => $ret, 'h' => $height, 'dy' => $dy]; }
/** * Return full font path * * @param ImagineInterface $imagine * @param string $font * @param integer $size * @param string $color * * @return string */ protected function setFont(ImagineInterface $imagine, $font, $size, $color = 'ffffff') { return $imagine->font($this->getResourceDir() . '/fonts/' . $font, $size, new Color($color)); }