Ejemplo n.º 1
0
 /**
  * @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;
 }
Ejemplo n.º 2
0
 public function apply(\Imagine\Image\ImageInterface $original)
 {
     $originalThumb = $original->thumbnail($this->size, 'inset');
     $image = $this->createCanvas();
     $image = $this->pasteCentered($image, $originalThumb);
     return $image;
 }
 /**
  * @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);
 }
Ejemplo n.º 4
0
 /**
  * Command image thumb
  * 
  * @param int $width
  * @param int $height
  * 
  * @return void
  */
 protected function imageThumb($width, $height)
 {
     $width = (int) $width;
     $height = (int) $height;
     if ($width <= 0) {
         throw new BadMethodCallException('Invalid parameter width for command "thumb"');
     }
     if ($height <= 0) {
         throw new BadMethodCallException('Invalid parameter height for command "thumb"');
     }
     $this->image = $this->image->thumbnail(new Box($width, $height));
 }
Ejemplo n.º 5
0
 /**
  * Fit image to area
  * @param integer $width the width in pixels to create the thumbnail
  * @param integer $height the height in pixels to create the thumbnail
  * @param string $mode
  * @return static
  */
 public function fit($width, $height, $mode = ManipulatorInterface::THUMBNAIL_INSET)
 {
     $box = new Box($width, $height);
     $this->image = $this->image->thumbnail($box, $mode);
     // create empty image to preserve aspect ratio of thumbnail
     $thumb = static::getImagine()->create($box, new Color('FFF', 100));
     // calculate points
     $size = $this->image->getSize();
     $startX = 0;
     $startY = 0;
     if ($size->getWidth() < $width) {
         $startX = ceil($width - $size->getWidth()) / 2;
     }
     if ($size->getHeight() < $height) {
         $startY = ceil($height - $size->getHeight()) / 2;
     }
     $thumb->paste($this->image, new Point($startX, $startY));
     $this->image = $thumb;
     return $this;
 }
 /**
  * Create a thumbnail by scaling an image and cropping it to fit the exact dimensions
  *
  * @param ImageInterface $image The ImageInterface instance from Imagine
  * @param int $targetWidth The width in pixels
  * @param int $targetHeight The height in pixels
  * @return ImageInterface
  */
 protected function thumbnailCropScale(ImageInterface $image, $targetWidth, $targetHeight)
 {
     $target = new Box($targetWidth, $targetHeight);
     $sourceSize = $image->getSize();
     if ($sourceSize->getWidth() > $sourceSize->getHeight()) {
         $width = $sourceSize->getWidth() * ($target->getHeight() / $sourceSize->getHeight());
         $height = $targetHeight;
         $cropPoint = new Point((int) (max($width - $target->getWidth(), 0) / 2), 0);
     } else {
         $height = $sourceSize->getHeight() * ($target->getWidth() / $sourceSize->getWidth());
         $width = $targetWidth;
         $cropPoint = new Point(0, (int) (max($height - $target->getHeight(), 0) / 2));
     }
     $box = new Box($width, $height);
     return $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND)->crop($cropPoint, $target);
 }
 /**
  * Save image
  * @return ImageInterface
  */
 public function save()
 {
     if (file_exists($this->_pathObjectFile)) {
         $this->_url = $this->_urlObjectFile;
         return true;
     } elseif (file_exists($this->_pathObjectFileOrigin)) {
         $this->_image = Image::getImagine()->open($this->_pathObjectFileOrigin);
     } elseif (file_exists($this->_pathObjectPlaceholder)) {
         $this->_url = $this->_urlObjectPlaceholder;
         return true;
     } elseif (file_exists($this->_pathObjectPlaceholderOrigin)) {
         $this->_image = Image::getImagine()->open($this->_pathObjectPlaceholderOrigin);
         $this->_pathObjectFile = $this->_pathObjectPlaceholder;
         $this->_urlObjectFile = $this->_urlObjectPlaceholder;
     } elseif (file_exists($this->_pathRootPlaceholder)) {
         $this->_url = $this->_urlRootPlaceholder;
         return true;
     } elseif (file_exists($this->_pathRootPlaceholderOrigin)) {
         $this->_image = Image::getImagine()->open($this->_pathRootPlaceholderOrigin);
         $this->_pathObjectFile = $this->_pathRootPlaceholder;
         $this->_urlObjectFile = $this->_urlRootPlaceholder;
     }
     if ($this->_image) {
         if (!$this->width || !$this->height) {
             $ratio = $this->_image->getSize()->getWidth() / $this->_image->getSize()->getHeight();
             if ($this->width) {
                 $this->height = ceil($this->width / $ratio);
             } else {
                 $this->width = ceil($this->height * $ratio);
             }
             $box = new Box($this->width, $this->height);
             $this->_image->resize($box);
             $this->_image->crop(new Point(0, 0), $box);
         } else {
             $box = new Box($this->width, $this->height);
             $size = $this->_image->getSize();
             if ($size->getWidth() <= $box->getWidth() && $size->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) {
             } else {
                 $this->_image = $this->_image->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND);
                 // calculate points
                 $size = $this->_image->getSize();
                 $startX = 0;
                 $startY = 0;
                 if ($size->getWidth() < $this->width) {
                     $startX = ceil($this->width - $size->getWidth()) / 2;
                 }
                 if ($size->getHeight() < $this->height) {
                     $startY = ceil($this->height - $size->getHeight()) / 2;
                 }
                 // create empty image to preserve aspect ratio of thumbnail
                 $thumb = Image::getImagine()->create($box, new Color('FFF', 100));
                 $thumb->paste($this->_image, new Point($startX, $startY));
                 $this->_image = $thumb;
             }
         }
         $this->_image->save($this->_pathObjectFile);
         $this->_url = $this->_urlObjectFile;
         return true;
     }
     return false;
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function apply(ImageInterface $image)
 {
     return $image->thumbnail($this->size, $this->mode);
 }
Ejemplo n.º 9
0
 /**
  * Handle image manipulation.
  *
  * @param  \Imagine\Image\ImageInterface  $image
  * @param  array  $data
  *
  * @return \Imagine\Image\ImageInterface
  */
 protected function handleImageManipulation(ImageInterface $image, array $data)
 {
     $width = $height = $data['dimension'];
     return $image->thumbnail(new Box($width, $height), $data['mode'], $data['filter']);
 }
Ejemplo n.º 10
0
 /**
  * @param \Imagine\Image\ImageInterface $image
  * @param array $options
  * @return \Imagine\Image\ImageInterface
  */
 protected function applyThumbnailFilter(ImageInterface $image, $options)
 {
     // Defaults
     $size = new Box(120, 120);
     $mode = ImageInterface::THUMBNAIL_OUTBOUND;
     if (isset($options['size']) && count($options['size']) == 2) {
         list($width, $height) = $options['size'];
         $size = new Box($width, $height);
     }
     if (isset($options['mode']) && $options['mode'] == 'inset') {
         $mode = ImageInterface::THUMBNAIL_INSET;
     }
     return $image->thumbnail($size, $mode);
 }
Ejemplo n.º 11
0
 /**
  * @param \Imagine\Image\ImageInterface $image
  * @param array $commandOptions array('size' => ('width' => 123, 'height => 456), 'mode' => 'outbound')
  * @return \Imagine\Image\ImageInterface
  */
 protected function thumbnailCommand(\Imagine\Image\ImageInterface $image, array $commandOptions)
 {
     if (!isset($commandOptions['size'])) {
     }
     $dimensions = $this->parseBox($commandOptions['size']);
     if (isset($commandOptions['mode']) && $commandOptions['mode'] === 'outbound') {
         $mode = \Imagine\Image\ManipulatorInterface::THUMBNAIL_OUTBOUND;
     } else {
         $mode = \Imagine\Image\ManipulatorInterface::THUMBNAIL_INSET;
     }
     return $image->thumbnail($dimensions, $mode);
 }
Ejemplo n.º 12
0
Archivo: Scaler.php Proyecto: sulu/sulu
 /**
  * {@inheritdoc}
  */
 public function scale(ImageInterface $image, $x, $y, $mode = ImageInterface::THUMBNAIL_OUTBOUND, $forceRatio = true, $retina = false)
 {
     list($newWidth, $newHeight) = $this->getHeightWidth($x, $y, $retina, $forceRatio, $image->getSize(), $mode);
     return $image->thumbnail(new Box($newWidth, $newHeight), $mode);
 }