/** * Crop image if exceeds boundaries * * @param ImageInterface $image * @param $settings * @return ImageInterface */ private function cropImage(ImageInterface $image, $settings) { $neededSize = new Box($settings['width'], $settings['height']); $currentSize = $image->getSize(); if ($neededSize->contains($currentSize)) { return $image; } $point = new Point($currentSize->getWidth() > $neededSize->getWidth() ? round(($currentSize->getWidth() - $neededSize->getWidth()) / 2) : 0, $currentSize->getHeight() > $neededSize->getHeight() ? round(($currentSize->getHeight() - $neededSize->getHeight()) / 2) : 0); $image->crop($point, $neededSize); return $image; }
/** * 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; }
/** * {@inheritdoc} */ public function execute(ImageInterface $image, $parameters) { $maskPath = isset($parameters['image']) ? $this->fileLocator->locate($parameters['image']) : null; if (!$maskPath) { return $image; } $originalWidth = $image->getSize()->getWidth(); $originalHeight = $image->getSize()->getHeight(); $top = isset($parameters['top']) ? $parameters['top'] : 0; $left = isset($parameters['left']) ? $parameters['left'] : 0; $width = isset($parameters['width']) ? $parameters['width'] : $originalWidth; $height = isset($parameters['height']) ? $parameters['height'] : $originalHeight; // imagine will error when mask is bigger then the given image // this could happen in forceRatio true mode so we need also scale the mask if ($width > $originalWidth) { $width = $originalWidth; $height = (int) ($height / $width * $originalWidth); } if ($height > $originalHeight) { $height = $originalHeight; $width = (int) ($width / $height * $originalHeight); } // create mask $mask = $this->createMask($maskPath, $width, $height); // add mask to image $image->paste($mask, new Point($top, $left)); return $image; }
/** * Applies scheduled transformation to ImageInterface instance * Returns processed ImageInterface instance * * @param ImageInterface $image * * @return ImageInterface */ public function apply(ImageInterface $image) { for ($x = 0; $x < $image->getSize()->getWidth(); $x++) { for ($y = 0; $y < $image->getSize()->getHeight(); $y++) { call_user_func($this->callback, $image, new Point($x, $y)); } } return $image; }
/** * @param string $point * @param \Imagine\Image\ImageInterface $pasteImage * @param \Imagine\Image\ImageInterface $image * * @return integer */ protected function stringYtoInteger($point, ImageInterface $pasteImage, ImageInterface $image) { switch ($point) { case 'bottom': return (int) $image->getSize()->getHeight() - $pasteImage->getSize()->getHeight(); case 'middle': return (int) round($image->getSize()->getHeight() / 2 - $pasteImage->getSize()->getHeight() / 2); case 'top': } }
protected function calculateDimensions($width = null, $height = null) { if (empty($width)) { $width = $this->image->getSize()->getWidth(); } if (empty($height)) { $height = $this->image->getSize()->getHeight(); } $this->width = $width; $this->height = $height; }
/** * {@inheritDoc} */ public function load(ImageInterface $image, array $options = array()) { $background = $image->palette()->color(isset($options['color']) ? $options['color'] : '#fff', isset($options['transparency']) ? $options['transparency'] : null); $topLeft = new Point(0, 0); $size = $image->getSize(); if (isset($options['size'])) { list($width, $height) = $options['size']; $size = new Box($width, $height); $topLeft = new Point(($width - $image->getSize()->getWidth()) / 2, ($height - $image->getSize()->getHeight()) / 2); } $canvas = $this->imagine->create($size, $background); return $canvas->paste($image, $topLeft); }
/** * Returns true iff the cropping does not exceed the image borders. * * @param ImageInterface $image * @param $x * @param $y * @param $width * @param $height * * @return bool */ private function isInsideImage(ImageInterface $image, $x, $y, $width, $height) { if ($x < 0 || $y < 0) { return false; } if ($x + $width > $image->getSize()->getWidth()) { return false; } if ($y + $height > $image->getSize()->getHeight()) { return false; } return true; }
/** * @param ImagineImageInterface $image * * @return ImagineImageInterface */ public function updateFile(ImagineImageInterface $image) { $this->originWidth = $image->getSize()->getWidth(); $this->originHeight = $image->getSize()->getHeight(); // Resize $this->resize(); $resize = new ImagineResize(new Box($this->resizeWidth, $this->resizeHeight)); $image = $resize->apply($image); // Crop $this->crop(); $crop = new ImagineCrop(new Point($this->targetLeft, $this->targetTop), new Box($this->targetWidth, $this->targetHeight)); $image = $crop->apply($image); return $image; }
/** * {@inheritDoc} */ public function apply(ImageInterface $image) { if ($this->size) { list($width, $height) = $this->size; $size = new Box($width, $height); $topLeft = new Point(($width - $image->getSize()->getWidth()) / 2, ($height - $image->getSize()->getHeight()) / 2); } else { $topLeft = new Point(0, 0); $size = $image->getSize(); } $background = $image->palette()->color($this->color); $canvas = $this->imagine->create($size, $background); return $canvas->paste($image, $topLeft); }
/** * {@inheritDoc} */ public function load(ImageInterface $image, array $options = array()) { if (!isset($options['min'])) { throw new \InvalidArgumentException('Missing min option.'); } list($width, $height) = $options['min']; $size = $image->getSize(); $origWidth = $size->getWidth(); $origHeight = $size->getHeight(); if ($origWidth < $width || $origHeight < $height) { $widthRatio = $width / $origWidth; $heightRatio = $height / $origHeight; $ratio = $widthRatio > $heightRatio ? $widthRatio : $heightRatio; $filter = new Resize(new Box($origWidth * $ratio, $origHeight * $ratio)); return $filter->apply($image); } return $image; }
/** * {@inheritdoc} */ public function load(ImageInterface $image, array $options = array()) { $mode = ImageInterface::THUMBNAIL_OUTBOUND; if (!empty($options['mode']) && 'inset' === $options['mode']) { $mode = ImageInterface::THUMBNAIL_INSET; } if (!empty($options['filter'])) { $filter = constant('Imagine\\Image\\ImageInterface::FILTER_' . strtoupper($options['filter'])); } if (empty($filter)) { $filter = ImageInterface::FILTER_UNDEFINED; } list($width, $height) = $options['size']; $size = $image->getSize(); $origWidth = $size->getWidth(); $origHeight = $size->getHeight(); if (null === $width || null === $height) { if (null === $height) { $height = (int) ($width / $origWidth * $origHeight); } elseif (null === $width) { $width = (int) ($height / $origHeight * $origWidth); } } if ($origWidth > $width || $origHeight > $height || !empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height)) { $filter = new Thumbnail(new Box($width, $height), $mode, $filter); $image = $filter->apply($image); } return $image; }
/** * {@inheritDoc} */ public function load(ImageInterface $image, array $options = array()) { $background = new Color(isset($options['color']) ? $options['color'] : '#fff'); $topLeft = new Point(0, 0); $canvas = $this->imagine->create($image->getSize(), $background); return $canvas->paste($image, $topLeft); }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { $currentSize = $image->getSize(); $ratioCurrent = $currentSize->getHeight() / $currentSize->getWidth(); $ratioNew = $this->size->getHeight() / $this->size->getWidth(); // ratio inverse of original and thumb image $ratioInverseNew = 1 / $ratioNew; // image has to crop if ($ratioCurrent != $ratioNew) { if ($this->size->getWidth() > $this->size->getHeight()) { $cropHeight = $currentSize->getWidth() * $ratioNew; $cropWidth = $currentSize->getWidth(); if ($cropHeight > $currentSize->getHeight()) { $correction = 1 / ($cropHeight / $currentSize->getHeight()); $cropWidth *= $correction; $cropHeight *= $correction; } } else { $cropWidth = $currentSize->getHeight() * $ratioInverseNew; $cropHeight = $currentSize->getHeight(); if ($cropWidth > $currentSize->getWidth()) { $correction = 1 / ($cropWidth / $currentSize->getWidth()); $cropWidth *= $correction; $cropHeight *= $correction; } } $cropSize = new Box($cropWidth, $cropHeight); $startPoint = $this->gravity->getStartPoint($cropSize); $image = $image->crop($startPoint, $cropSize); } return $image->resize($this->size); }
/** * @return null|string */ public function imageResize() { $this->openImage(); $width = $this->imageWidth; $height = $this->imageHeight; $maxWidth = $this->imageMaxWidth; $maxHeight = $this->imageMaxHeight; if ($maxWidth === null && $maxHeight !== null) { $imageBox = $this->image->getSize()->heighten($maxHeight); } elseif ($maxHeight === null && $maxWidth !== null) { $imageBox = $this->image->getSize()->widen($maxWidth); } elseif ($maxHeight !== null && $maxWidth !== null) { $imageBox = $this->image->getSize()->heighten($maxHeight)->widen($maxWidth); } elseif ($width !== null && $height !== null) { $imageBox = new \Imagine\Image\Box($width, $height); } elseif ($width === null && $height !== null) { $imageBox = new \Imagine\Image\Box($this->image->getSize()->getWidth(), $height); } elseif ($width !== null && $height === null) { $imageBox = new \Imagine\Image\Box($width, $this->image->getSize()->getHeight()); } else { $imageBox = new \Imagine\Image\Box($this->image->getSize()->getWidth(), $this->image->getSize()->getHeight()); } $this->image = $this->image->resize($imageBox); return $this; }
protected function pasteCentered(\Imagine\Image\ImageInterface $image, \Imagine\Image\ImageInterface $original) { $originalSize = $original->getSize(); $x = $this->getPasteValue($this->size->getWidth(), $originalSize->getWidth()); $y = $this->getPasteValue($this->size->getHeight(), $originalSize->getHeight()); $pastePoint = new \Imagine\Image\Point($x, $y); $image->paste($original, $pastePoint); return $image; }
public function load(ImageInterface $image, array $options = array()) { if (empty($options)) { throw new InvalidArgumentException('Missing width option'); } $filter = new Gravity(new Box((int) $options[0], (int) $options[1]), new MiddleMiddle($image->getSize())); $image = $filter->apply($image); return $image; }
/** * {@inheritDoc} */ public function apply(ImageInterface $image) { $watermark = $this->watermark; $size = $image->getSize(); $watermarkSize = $watermark->getSize(); // If 'null': Downscale if needed if (!$this->size && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) { $this->size = 1.0; } if ($this->size) { $factor = $this->size * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight()); $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor)); $watermarkSize = $watermark->getSize(); } switch ($this->position) { case 'topleft': $x = 0; $y = 0; break; case 'top': $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; $y = 0; break; case 'topright': $x = $size->getWidth() - $watermarkSize->getWidth(); $y = 0; break; case 'left': $x = 0; $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; break; case 'center': $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; break; case 'right': $x = $size->getWidth() - $watermarkSize->getWidth(); $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; break; case 'bottomleft': $x = 0; $y = $size->getHeight() - $watermarkSize->getHeight(); break; case 'bottom': $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; $y = $size->getHeight() - $watermarkSize->getHeight(); break; case 'bottomright': $x = $size->getWidth() - $watermarkSize->getWidth(); $y = $size->getHeight() - $watermarkSize->getHeight(); break; default: throw new Exception\InvalidArgumentException(sprintf('Unknown position "%s"', $this->position)); } return $image->paste($watermark, new Point($x, $y)); }
/** * Adds a frame around of the image. Please note that the image size will increase by `$margin` x 2. * @param integer $margin the frame size to add around the image * @param string $color the frame color * @param integer $alpha the alpha value of the frame. * @return static */ public function frame($margin = 20, $color = '666', $alpha = 100) { $size = $this->image->getSize(); $pasteTo = new Point($margin, $margin); $padColor = new Color($color, $alpha); $box = new Box($size->getWidth() + ceil($margin * 2), $size->getHeight() + ceil($margin * 2)); $image = static::getImagine()->create($box, $padColor); $image->paste($this->image, $pasteTo); return $this; }
/** * {@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; }
public function apply(ImageInterface &$image, ImagineInterface $imagine) { $box = $image->getSize(); $width = Helper::percentValue($this->_width, $box->getWidth()); $height = Helper::percentValue($this->_height, $box->getHeight()); // no upscale if ($box->getWidth() <= $width && $box->getHeight() <= $height) { return; } Helper::scaleSize($width, $height, $box); $image->resize(new Box($width, $height)); }
/** * Performs resize of the image. Imagine component is used. * * @param ImageInterface $imagine * @param $resize * @param bool $crop * * @return BoxInterface */ private function performResize($imagine, $resize, $crop = true) { $box = $imagine->getSize(); list($width, $height) = Utils::getDimension($resize); $box = $box->scale(max($width / $box->getWidth(), $height / $box->getHeight())); $imagine->resize($box); if ($crop) { $point = new Point(($box->getWidth() - $width) / 2, ($box->getHeight() - $height) / 2); $imagine->crop($point, new Box($width, $height)); } return $box; }
/** * Resize image by its height. Compute the witdh proportionaly to the height. */ protected static function resizeByHeight(ImageInterface $image, Format $format) { $expectedHeight = $format->getHeight(); $size = $image->getSize(); $originalHeight = $size->getHeight(); if ($originalHeight < $expectedHeight) { $box = $size; } else { $expectedWidth = (int) ($expectedHeight * $size->getWidth() / $size->getHeight()); $box = new Box($expectedWidth, $expectedHeight); } return static::doResize($image, $box); }
public function load(ImageInterface $image, array $options = array()) { if (count($options) < 2) { throw new InvalidArgumentException('Missing width and/or height percent options'); } $size = $image->getSize(); $origWidth = $size->getWidth(); $origHeight = $size->getHeight(); list($widthPercent, $heightPercent) = $options; $targetWidth = $origWidth * $widthPercent / 100; $targetHeight = $origHeight * $heightPercent / 100; return $this->innerLoader->load($image, array('size' => array($targetWidth, $targetHeight))); }
/** * {@inheritDoc} */ public function load(ImageInterface $image, array $options = array()) { if (!isset($options['size'])) { return $image; } $width = $image->getSize()->getWidth(); $height = $image->getSize()->getHeight(); // By default we focus the center of the image $focusX = $width / 2; $focusY = $height / 2; $options['focusPoint'] = ["x" => 0, "y" => 0]; if (isset($options['object'])) { if (!$options['object'] instanceof Image) { throw new \InvalidArgumentException(); } $focusX = $options['focusPoint']["x"]; $focusY = $options['focusPoint']["y"]; } $x = $this->computeCoordinate($focusX, $width, $options['size'][0]); $y = $this->computeInversedCoordinate($focusY, $height, $options['size'][1]); return parent::load($image, array_merge($options, ['start' => [$x, $y]])); }
/** * @inheritdoc * @param \Imagine\Image\ImageInterface $image * @return \Imagine\Image\ImageInterface|\Imagine\Image\ManipulatorInterface */ public function apply(\Imagine\Image\ImageInterface $image) { if (!file_exists($this->wm_path)) { return $image; } \Yii::warning('Watermark does not exists: ' . $this->wm_path); $watermark = $this->imagine->open($this->wm_path); $size = $image->getSize(); $wm_size = $watermark->getSize(); // Horizontal position switch ($this->wm_position) { case self::WM_POSITION_TOP_LEFT: case self::WM_POSITION_BOTTOM_LEFT: $this->pos_horizontal = $this->wm_margin; break; case self::WM_POSITION_TOP_RIGHT: case self::WM_POSITION_BOTTOM_RIGHT: $this->pos_horizontal = $size->getWidth() - $wm_size->getWidth() - $this->wm_margin; break; case self::WM_POSITION_CENTER: $this->pos_horizontal = ceil(($size->getWidth() - $wm_size->getWidth()) / 2); break; } // Vertical position switch ($this->wm_position) { case self::WM_POSITION_TOP_LEFT: case self::WM_POSITION_TOP_RIGHT: $this->pos_vertical = $this->wm_margin; break; case self::WM_POSITION_BOTTOM_LEFT: case self::WM_POSITION_BOTTOM_RIGHT: $this->pos_vertical = $size->getHeight() - $wm_size->getHeight() - $this->wm_margin; break; case self::WM_POSITION_CENTER: $this->pos_vertical = ceil(($size->getHeight() - $wm_size->getHeight()) / 2); break; } if ($this->pos_horizontal <= 0) { $this->pos_horizontal = 0; } if ($this->pos_vertical <= 0) { $this->pos_vertical = 0; } $wm_position_point = new \Imagine\Image\Point($this->pos_horizontal, $this->pos_vertical); try { $image = $image->paste($watermark, $wm_position_point); } catch (\Imagine\Exception\OutOfBoundsException $e) { \Yii::warning($e->getMessage()); } return $image; }
/** * {@inheritdoc} */ public function execute(ImageInterface &$image, $parameters) { $size = $image->getSize(); $retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1; $newWidth = isset($parameters['x']) ? intval($parameters['x']) * $retina : null; $newHeight = isset($parameters['y']) ? intval($parameters['y']) * $retina : null; if ($newHeight == null) { $newHeight = $size->getHeight() / $size->getWidth() * $newWidth; } if ($newWidth == null) { $newWidth = $size->getWidth() / $size->getHeight() * $newHeight; } $image->resize(new Box($newWidth, $newHeight)); }
public function apply(ImageInterface &$image, ImagineInterface $imagine) { $box = $image->getSize(); // x and y is the center of the crop $x = Helper::percentValue($this->_x, $boxw = $box->getWidth()); $y = Helper::percentValue($this->_y, $boxh = $box->getHeight()); $w = Helper::percentValue($this->_width, $boxw); $h = Helper::percentValue($this->_height, $boxh); if ($this->_ratio) { switch ($this->_ratio) { case self::COVER: Helper::scaleSize($w, $h, $box); $w = $h = min($w, $h); break; case self::CONTAIN: Helper::scaleSize($w, $h, $box); $max = max($w, $h); $img = $imagine->create(new Box($max, $max), new Color(0, 100)); $img->paste($image, new Point(($max - $boxw) * 0.5, ($max - $boxh) * 0.5)); $image = $img; return; default: // custom ratio $this->fitByRatio($w, $h, $w && $h ? $w / $h : 0); if (!$w || !$h) { throw new \RuntimeException('Invalid ratio supplied'); } break; } } else { Helper::scaleSize($w, $h, $box); } $halfw = $w / 2; $halfh = $h / 2; if ($x + $halfw > $boxw) { $x = $boxw - $halfw; } if ($y + $halfh > $boxh) { $y = $boxh - $halfh; } if ($x < $halfw) { $x = $halfw; } if ($y < $halfh) { $y = $halfh; } $image->crop(new Point($x - $w / 2, $y - $h / 2), new Box($w, $h)); }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { $watermark = $this->imagine->open($this->watermarkFilename); $size = $image->getSize(); $wSize = $watermark->getSize(); // Watermark in top left $topLeftPoint = new Point(self::WATERMARK_PADDING, self::WATERMARK_PADDING); $image->paste($watermark, $topLeftPoint); // Watermark in bottom right $bottomRightPoint = new Point($size->getWidth() - $wSize->getWidth() - self::WATERMARK_PADDING, $size->getHeight() - $wSize->getHeight() - self::WATERMARK_PADDING); $image->paste($watermark, $bottomRightPoint); // Watermark in middle $middlePoint = new Point(floor($size->getWidth() / 2 - $wSize->getWidth() / 2), floor($size->getHeight() / 2 - $wSize->getHeight() / 2)); $image->paste($watermark, $middlePoint); return $image; }
/** * 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)); }