Пример #1
0
 /**
  * {@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;
 }
Пример #2
0
 /**
  * {@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;
 }
Пример #3
0
 /**
  * Draw $image on $this->resultImage.
  *
  * @param string $image Image blob.
  * @param Point  $point The point where to draw $image.
  *
  * @return void
  */
 protected function drawImage($image, Point $point)
 {
     try {
         $this->resultImage->paste($this->imagine->load($image), $point);
     } catch (\Exception $exception) {
         // Most likely an exception about a out of bounds past, we'll just ignore that
     }
 }
Пример #4
0
 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;
 }
Пример #5
0
 /**
  * Adds a watermark to an existing image.
  * @param string $watermarkFilename the file path or path alias of the watermark image.
  * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
  * @return static
  * @throws InvalidParamException if `$start` is invalid
  */
 public function watermark($watermarkFilename, array $start = [0, 0])
 {
     if (!isset($start[0], $start[1])) {
         throw new InvalidParamException('$start must be an array of two elements.');
     }
     $watermark = static::getImagine()->open(Yii::getAlias($watermarkFilename));
     $this->image->paste($watermark, new Point($start[0], $start[1]));
     return $this;
 }
Пример #6
0
 /**
  * Add a watermark to image. If no position is entered the watermark will be centered.
  *
  * @param $source
  * @param null $x
  * @param null $y
  * @return $this
  */
 public function addWatermark($source, $x = null, $y = null)
 {
     $watermark = $this->imagine->open($source);
     $wSize = $watermark->getSize();
     if (!$x && !$y) {
         $x = $this->openImage()->image->getSize()->getWidth() / 2 + $wSize->getWidth() / 2 - $wSize->getWidth();
         $y = $this->openImage()->image->getSize()->getHeight() / 2 + $wSize->getHeight() / 2 - $wSize->getHeight();
     }
     $position = new \Imagine\Image\Point($x, $y);
     $this->image = $this->image->paste($watermark, $position);
     return $this;
 }
 /**
  * @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
  */
 public function load(ImageInterface $image, array $options = array())
 {
     list($x, $y) = $options['start'];
     $destImage = $this->imagine->open($this->rootPath . '/' . $options['image']);
     return $image->paste($destImage, new Point($x, $y));
 }
Пример #8
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Filter\FilterInterface::apply()
  */
 public function apply(ImageInterface $image)
 {
     return $image->paste($this->image, $this->start);
 }
Пример #9
0
 /**
  * {@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));
 }
 /**
  * @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
  */
 public function load(ImageInterface $image, array $options = array())
 {
     $options += array('size' => null, 'position' => 'center');
     if (substr($options['size'], -1) == '%') {
         $options['size'] = substr($options['size'], 0, -1) / 100;
     }
     $watermark = $this->imagine->open($this->rootPath . '/' . $options['image']);
     $size = $image->getSize();
     $watermarkSize = $watermark->getSize();
     // If 'null': Downscale if needed
     if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
         $options['size'] = 1.0;
     }
     if ($options['size']) {
         $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
         $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
         $watermarkSize = $watermark->getSize();
     }
     switch ($options['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 \InvalidArgumentException("Unexpected position '{$options['position']}'");
             break;
     }
     return $image->paste($watermark, new Point($x, $y));
 }
Пример #11
0
 public function transform(ImageInterface $image, ImagineInterface $imagine)
 {
     if (!isset($this->path)) {
         throw new InvalidConfigException(sprintf('Path to the watermark file must be specified in %s::$path.', self::className()));
     }
     $this->imageWidth = $image->getSize()->getWidth();
     $this->imageHeight = $image->getSize()->getHeight();
     $watermarkImageMaxWidth = $this->imageWidth - 2 * $this->margin;
     if ($watermarkImageMaxWidth < 1) {
         return;
     }
     $watermarkImageMaxHeight = $this->imageHeight - 2 * $this->margin;
     if ($watermarkImageMaxHeight < 1) {
         return;
     }
     $watermarkImage = $imagine->open(\Yii::getAlias($this->path));
     if (!(new Box($watermarkImageMaxWidth, $watermarkImageMaxHeight))->contains($watermarkImage->getSize())) {
         $resize = new Resize();
         $resize->width = $watermarkImageMaxWidth;
         $resize->height = $watermarkImageMaxHeight;
         $resize->scaleTo = 'fit';
         $resize->transform($watermarkImage, $imagine);
     }
     $this->watermarkImageWidth = $watermarkImage->getSize()->getWidth();
     $this->watermarkImageHeight = $watermarkImage->getSize()->getHeight();
     switch ($this->align) {
         case 'top-left':
             $start = new Point($this->getStartXLeft(), $this->getStarYTop());
             break;
         case 'top-center':
             $start = new Point($this->getStartXCenter(), $this->getStarYTop());
             break;
         case 'top-right':
             $start = new Point($this->getStartXRight(), $this->getStarYTop());
             break;
         case 'bottom-left':
             $start = new Point($this->getStartXLeft(), $this->getStartYBottom());
             break;
         case 'bottom-center':
             $start = new Point($this->getStartXCenter(), $this->getStartYBottom());
             break;
         case 'bottom-right':
             $start = new Point($this->getStartXRight(), $this->getStartYBottom());
             break;
         case 'center':
             $start = new Point($this->getStartXCenter(), $this->getStartYCenter());
             break;
         default:
             throw new InvalidConfigException(sprintf('Invalid value for property "align": %s.', is_scalar($this->align) ? $this->align : gettype($this->align)));
     }
     $image->paste($watermarkImage, $start);
 }
 /**
  * Applies watermark to image.
  *
  * @param ImageInterface   $image
  * @param ImageInterface $watermark
  *
  * @return ImageInterface
  */
 private function applyWatermark(ImageInterface $image, ImageInterface $watermark)
 {
     $size = $image->getSize();
     $wSize = $watermark->getSize();
     $bottomRight = new Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight());
     $image->paste($watermark, $bottomRight);
     return $image;
 }
Пример #13
0
 private function pasteImage(ImageInterface $image, ImageInterface $imageToPaste, PointInterface $pastePoint)
 {
     if (!$this->boxContains($image->getSize(), $imageToPaste->getSize(), $pastePoint)) {
         $rectangle = Rectangle::createWithSize($image->getSize())->intersection(Rectangle::create($pastePoint, $imageToPaste->getSize()));
         if ($rectangle !== null) {
             $croppingPoint = new Point($rectangle->getStartingPoint()->getX() - $pastePoint->getX(), $rectangle->getStartingPoint()->getY() - $pastePoint->getY());
             $imageToPaste->crop($croppingPoint, $rectangle->getSize());
             $image->paste($imageToPaste, $this->ensureNonNegativePoint($pastePoint));
         }
     } else {
         $image->paste($imageToPaste, $pastePoint);
     }
 }