/** * 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; }
/** * {@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 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; }
private function getColor(ImageInterface $image) { if ($this->color instanceof ColorInterface) { return $this->color; } return $image->palette()->color($this->color); }
/** * @param ImageInterface|\Imagine\Gmagick\Image $image * * @return ImageInterface */ public function apply(ImageInterface $image) { /** @var \Gmagick $gmagick */ $gmagick = $image->getGmagick(); $gmagick->reduceNoiseImage((double) $this->getOption('radius', 0)); 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 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); }
/** * @param ImageInterface $image * @param $namespace * @param $image_hash * @param $image_thumb * @return File */ public function createFromImagine(ImageInterface $image, $namespace, $image_hash, $image_thumb) { umask(00); $dest = $this->createDestinationPath($namespace, $image_hash, $image_thumb); $image->save($dest); return new File($dest); }
/** * @param ImageInterface $image * @return ImageInterface */ public function generateThumb(ImageInterface $image, $width, $height) { $background = isset($this->options["background"]) ? $this->options["background"] : null; $fitSize = isset($this->options["fitSize"]) ? $this->options["fitSize"] : true; $sizeBox = new Box($width, $height); $thumbMode = ImageInterface::THUMBNAIL_INSET; $thumb = $image->thumbnail($sizeBox, $thumbMode); // fill the area if ($fitSize) { $palette = new RGB(); if (!$background || $background == "transparent") { $backgroundColor = $palette->color("fff", 1); } else { $backgroundColor = $palette->color($background); } // source http://harikt.com/blog/2012/12/17/resize-image-keeping-aspect-ratio-in-imagine/ $realThumb = Image::create($sizeBox, $backgroundColor); $sizeR = $thumb->getSize(); $widthR = $sizeR->getWidth(); $heightR = $sizeR->getHeight(); $startX = $startY = 0; if ($widthR < $width) { $startX = ($width - $widthR) / 2; } if ($heightR < $height) { $startY = ($height - $heightR) / 2; } $realThumb->paste($thumb, new Point($startX, $startY)); } else { $realThumb = $thumb; } return $realThumb; }
/** * @param ImageInterface|\Imagine\Gmagick\Image $image * * @return ImageInterface */ public function apply(ImageInterface $image) { /** @var \Gmagick $gmagick */ $gmagick = $image->getGmagick(); $gmagick->swirlimage((double) $this->getOption('degrees', 60)); return $image; }
/** * {@inheritDoc} */ public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null, array $saveOptions = []) { $cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage); if (!is_dir(dirname($cachePath))) { mkdir(dirname($cachePath), 0755, true); } $image->save($cachePath, $saveOptions); }
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; }
/** * 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; }
/** * {@inheritdoc} */ public function load(ImageInterface $image, array $options = array()) { $mode = ImageInterface::INTERLACE_LINE; if (!empty($options['mode'])) { $mode = $options['mode']; } $image->interlace($mode); return $image; }
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; }
/** * {@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)); }
/** * @param list ($width, $height, $method) $arguments; */ public function processArguments(ImagineImage $imagineImage, array $arguments, array $options = array()) { list($width, $height, $method) = $arguments; if ($method == 'standard') { $style = ImagineImage::THUMBNAIL_INSET; } else { $style = ImagineImage::THUMBNAIL_OUTBOUND; } return $imagineImage->thumbnail(new Box($width, $height), $style); }
/** * @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': } }
/** * {@inheritdoc} */ public function execute(ImageInterface &$image, $parameters) { $retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1; $x = isset($parameters['x']) ? intval($parameters['x']) * $retina : 0; $y = isset($parameters['y']) ? intval($parameters['y']) * $retina : 0; $width = isset($parameters['w']) ? intval($parameters['w']) : 0; $height = isset($parameters['h']) ? intval($parameters['h']) : 0; $point = new Point($x, $y); $box = new Box($width, $height); $image->crop($point, $box); }
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; }
/** * 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; }
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)); }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { $image->usePalette($this->palette)->strip(); if (is_callable($this->path)) { $path = call_user_func($this->path, $image); } elseif (null !== $this->path) { $path = $this->path; } else { return $image; } return $image->save($path, $this->options); }
/** * {@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; }
/** * 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; }
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 execute(ImageInterface $image, $parameters) { @trigger_error('CropTransformation is deprecated since version 1.4. Use the scale config instead', E_USER_DEPRECATED); $retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1; $x = isset($parameters['x']) ? intval($parameters['x']) * $retina : 0; $y = isset($parameters['y']) ? intval($parameters['y']) * $retina : 0; $width = isset($parameters['w']) ? intval($parameters['w']) : 0; $height = isset($parameters['h']) ? intval($parameters['h']) : 0; $point = new Point($x, $y); $box = new Box($width, $height); $image->crop($point, $box); return $image; }
/** * {@inheritdoc} */ public function load(ImageInterface $image, array $options = array()) { $background = new Color(isset($options['color']) ? $options['color'] : '#fff', isset($options['transparency']) ? $options['transparency'] : 0); $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); }
/** * @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)); }