/** * Applies scheduled transformation to ImageInterface instance * Returns processed ImageInterface instance * * @param \Imagine\Image\ImageInterface $image * * @return \Imagine\Image\ImageInterface */ function apply(ImageInterface $image) { // We reduce the usage of methods on the image to dramatically increase the performance of this algorithm. // Really... We need that performance... // Therefore we first build a matrix, that holds the colors of the image. $width = $image->getSize()->getWidth(); $height = $image->getSize()->getHeight(); $byteData = new Matrix($width, $height); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $byteData->setElementAt($x, $y, $image->getColorAt(new Point($x, $y))); } } $dHeight = (int) floor(($this->matrix->getHeight() - 1) / 2); $dWidth = (int) floor(($this->matrix->getWidth() - 1) / 2); for ($y = $dHeight; $y < $height - $dHeight; $y++) { for ($x = $dWidth; $x < $width - $dWidth; $x++) { $sumRed = 0; $sumGreen = 0; $sumBlue = 0; // calculate new color for ($boxX = $x - $dWidth, $matrixX = 0; $boxX <= $x + $dWidth; $boxX++, $matrixX++) { for ($boxY = $y - $dHeight, $matrixY = 0; $boxY <= $y + $dHeight; $boxY++, $matrixY++) { $sumRed = $sumRed + $this->matrix->getElementAt($matrixX, $matrixY) * $byteData->getElementAt($boxX, $boxY)->getRed(); $sumGreen = $sumGreen + $this->matrix->getElementAt($matrixX, $matrixY) * $byteData->getElementAt($boxX, $boxY)->getGreen(); $sumBlue = $sumBlue + $this->matrix->getElementAt($matrixX, $matrixY) * $byteData->getElementAt($boxX, $boxY)->getBlue(); } } // set new color - has to be between 0 and 255! $image->draw()->dot(new Point($x, $y), new Color(array('red' => max(0, min(255, $sumRed)), 'green' => max(0, min(255, $sumGreen)), 'blue' => max(0, min(255, $sumBlue))))); } } return $image; }
/** * Write text on an image * * @param $text * @param $x * @param $y * @param int $angle * * @return null * @throws Exception */ public function writeText($text, $x, $y, $angle = 0) { if (empty($this->_font)) { throw new Exception(Craft::t("No font properties have been set. Call Image::setFontProperties() first.")); } $point = new \Imagine\Image\Point($x, $y); $this->_image->draw()->text($text, $this->_font, $point, $angle); }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { $size = $image->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $draw = $image->draw(); // Draw top and bottom lines $draw->line(new Point(0, 0), new Point($width - 1, 0), $this->color, $this->height)->line(new Point($width - 1, $height - 1), new Point(0, $height - 1), $this->color, $this->height); // Draw sides $draw->line(new Point(0, 0), new Point(0, $height - 1), $this->color, $this->width)->line(new Point($width - 1, 0), new Point($width - 1, $height - 1), $this->color, $this->width); return $image; }
/** * Draws a text string on an existing image. * @param string $text the text to write to the image * @param string $fontFile the file path or path alias * @param array $start the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates. * @param array $fontOptions the font options. The following options may be specified: * * - color: The font color. Defaults to "fff". * - size: The font size. Defaults to 12. * - angle: The angle to use to write the text. Defaults to 0. * * @return static * @throws InvalidParamException if `$fontOptions` is invalid */ public function text($text, $fontFile, array $start = [0, 0], array $fontOptions = []) { if (!isset($start[0], $start[1])) { throw new InvalidParamException('$start must be an array of two elements.'); } $fontSize = ArrayHelper::getValue($fontOptions, 'size', 12); $fontColor = ArrayHelper::getValue($fontOptions, 'color', 'fff'); $fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0); $font = static::getImagine()->font(Yii::getAlias($fontFile), $fontSize, new Color($fontColor)); $this->image->draw()->text($text, $font, new Point($start[0], $start[1]), $fontAngle); return $this; }
/** * Draw centered text in current image * * @param string $text * @param string $color * * @return void */ protected function drawCenteredText($text, $color) { $width = $this->image->getSize()->getWidth(); $height = $this->image->getSize()->getHeight(); $fontColor = $this->image->palette()->color('#' . $color); $fontSize = 48; $widthFactor = $width > 160 ? 0.8 : 0.9; $heightFactor = $height > 160 ? 0.8 : 0.9; do { $font = $this->getImagineService()->font(__DIR__ . '/../../../data/font/Roboto-Regular.ttf', $fontSize, $fontColor); $fontBox = $font->box($text); $fontSize = round($fontSize * 0.8); } while ($fontSize > 5 && ($width * $widthFactor < $fontBox->getWidth() || $height * $heightFactor < $fontBox->getHeight())); $pointX = max(0, floor(($width - $fontBox->getWidth()) / 2)); $pointY = max(0, floor(($height - $fontBox->getHeight()) / 2)); $this->image->draw()->text($text, $font, new Point($pointX, $pointY)); }
/** * @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load() */ public function load(ImageInterface $image, array $options = array()) { $size = $image->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $alpha = $options['opacity']; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $point = new Point($x, $y); $color = $image->getColorAt($point); $dR = $color->getRed(); $dG = $color->getGreen(); $dB = $color->getBlue(); $image->draw()->dot($point, new Color(array($dR, $dG, $dB), $alpha)); } } return $image; }
private function richDrawText($text, ImageInterface $image, $wordSpacing, ImagineFont $font, Point $point) { $words = preg_split('/\\s+/', $text); $wordSpacing = $wordSpacing + $this->getWidthOfSpaceChar($font); foreach ($words as $word) { if ($word !== '') { $box = $font->box($word); $image->draw()->text($word, $font, $point); $point = new Point($point->getX() + $box->getWidth() + $wordSpacing, $point->getY()); } } }
/** * Draws a text string on an existing image. * @param Imagine\Image\ImageInterface $Image the image * @param string $text the text to write to the image * @param string $fontFile the file path or path alias * @param array $start the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates. * @param array $fontOptions the font options. The following options may be specified: * * - color: The font color. Defaults to "fff". * - size: The font size. Defaults to 12. * - angle: The angle to use to write the text. Defaults to 0. * * @return \Imagine\Image\ImageInterface * @throws \yii\base\InvalidParamException if `$fontOptions` is invalid */ public static function text($Image, $text, $fontFile, array $start = [0, 0], array $fontOptions = []) { if (!isset($start[0], $start[1])) { throw new \yii\base\InvalidParamException('$start must be an array of two elements.'); } $fontSize = ArrayHelper::getValue($fontOptions, 'size', 12); $fontColor = ArrayHelper::getValue($fontOptions, 'color', '#ffffff'); $fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0); $Palette = new Imagine\Image\Palette\RGB(); /** @var Imagine\Image\AbstractFont $font */ $font = static::getImagine()->font(\Yii::getAlias($fontFile), $fontSize, $Palette->color($fontColor)); $Image->draw()->text($text, $font, new Imagine\Image\Point($start[0], $start[1]), $fontAngle); return $Image; }
/** * @param \Imagine\Image\ImageInterface $image * @param array $commandOptions * @return \Imagine\Image\ImageInterface */ protected function drawCommand(\Imagine\Image\ImageInterface $image, array $commandOptions) { $drawer = $image->draw(); foreach ($commandOptions as $drawCommandName => $drawCommandOptions) { if ($drawCommandName === 'ellipse') { $drawer = $this->drawEllipse($drawer, $drawCommandOptions); } elseif ($drawCommandName === 'text') { $drawer = $this->drawText($drawer, $drawCommandOptions); } else { throw new \InvalidArgumentException('Invalid draw command "' . $drawCommandName . '"', 1316613593); } } return $image; }