Esempio n. 1
1
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * @param $image \Imagine\Image\ImageInterface
  * @param $width
  * @param $height
  * @return \Imagine\Image\ImageInterface|\Imagine\Image\ManipulatorInterface
  */
 public static function aligningImage($image, $width, $height)
 {
     $maxBox = new Box($width, $height);
     if ($maxBox->contains($image->getSize())) {
         return $image;
     }
     $ratios = array($maxBox->getWidth() / $image->getSize()->getWidth(), $maxBox->getHeight() / $image->getSize()->getHeight());
     $box = $image->getSize()->scale(min($ratios));
     return $image->resize($box);
 }
 /**
  * Wrapper for Imagines thumbnail.
  *
  * This method had a bunch of issues and the code inside the method is a
  * workaround! Please see:
  *
  * @link https://github.com/burzum/cakephp-imagine-plugin/issues/42
  * @link https://github.com/avalanche123/Imagine/issues/478
  *
  * @throws \InvalidArgumentException
  * @param array Array of options for processing the image.
  * @throws InvalidArgumentException if no height or width was passed
  * @return $this
  */
 public function thumbnail(array $options = [])
 {
     if (empty($options['height']) || empty($options['width'])) {
         throw new \InvalidArgumentException(__d('imagine', 'You have to pass height and width in the options!'));
     }
     $mode = ImageInterface::THUMBNAIL_INSET;
     if (isset($options['mode']) && $options['mode'] === 'outbound') {
         $mode = ImageInterface::THUMBNAIL_OUTBOUND;
     }
     $filter = ImageInterface::FILTER_UNDEFINED;
     if (isset($options['filter'])) {
         $filter = $options['filter'];
     }
     $size = new Box($options['width'], $options['height']);
     $imageSize = $this->_image->getSize();
     $ratios = array($size->getWidth() / $imageSize->getWidth(), $size->getHeight() / $imageSize->getHeight());
     // if target width is larger than image width
     // AND target height is longer than image height
     if ($size->contains($imageSize)) {
         return $this->_image;
     }
     if ($mode === ImageInterface::THUMBNAIL_INSET) {
         $ratio = min($ratios);
     } else {
         $ratio = max($ratios);
     }
     if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) {
         if (!$imageSize->contains($size)) {
             $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight()));
         } else {
             $imageSize = $this->_image->getSize()->scale($ratio);
             $this->_image->resize($imageSize, $filter);
         }
         $this->_image->crop(new Point(max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))), $size);
     } else {
         if (!$imageSize->contains($size)) {
             $imageSize = $imageSize->scale($ratio);
             $this->_image->resize($imageSize, $filter);
         } else {
             $imageSize = $this->_image->getSize()->scale($ratio);
             $this->_image->resize($imageSize, $filter);
         }
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function contains(BoxInterface $box, PointInterface $start = null)
 {
     return $this->box->contains($box, $start);
 }
Esempio n. 5
0
 /**
  * @param string $inFilename
  * @param Image  $spec
  * @param string $outFilename
  */
 public function convert($inFilename, Specification $spec, $outFilename)
 {
     $image = $this->imagine->open($inFilename);
     $options = array();
     if ($spec->getFormat()) {
         $options['format'] = $spec->getFormat();
     }
     if ($spec->getQuality()) {
         if (!empty($options['format']) && $options['format'] === 'png') {
             $options['png_compression_level'] = floor($spec->getQuality() / 10);
             $options['png_compression_filter'] = $spec->getQuality() % 10;
         } elseif (!empty($options['format']) && $options['format'] === 'jpg') {
             $options['jpeg_quality'] = $spec->getQuality();
         }
     }
     if ($spec->getColorspace()) {
         $image->strip();
         switch ($spec->getColorspace()) {
             case 'cmyk':
                 $image->usePalette(new CMYK());
                 break;
             case 'grayscale':
                 $image->usePalette(new Grayscale());
                 break;
             default:
                 $image->usePalette(new RGB());
         }
     }
     if ($spec->getScale()) {
         if ($spec->getScale() === 'up') {
             // only scale up
         } elseif ($spec->getScale() === 'down') {
             // only scale down
         }
     }
     $method = $spec->getResizeMode();
     if ($method === Image::RESIZE_METHOD_WIDTH) {
         $specSize = $image->getSize()->widen($spec->getWidth());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_HEIGHT) {
         $specSize = $image->getSize()->heighten($spec->getHeight());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_EXACT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_FIT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $image = $image->thumbnail($specSize, ImageInterface::THUMBNAIL_INSET);
     } elseif ($method === Image::RESIZE_METHOD_EXACT_FIT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $layer = $image->thumbnail($specSize, ImageInterface::THUMBNAIL_INSET);
         $layerSize = $layer->getSize();
         $palette = new RGB();
         if ($spec->getBackgroundColor()) {
             $color = $palette->color($spec->getBackgroundColor(), 100);
         } else {
             $color = $palette->color('#fff', 0);
         }
         $image = $this->imagine->create($specSize, $color);
         $image->paste($layer, new Point(floor(($specSize->getWidth() - $layerSize->getWidth()) / 2), floor(($specSize->getHeight() - $layerSize->getHeight()) / 2)));
     } elseif ($method === Image::RESIZE_METHOD_CROP) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $imageSize = $image->getSize();
         if (!$specSize->contains($imageSize)) {
             $ratios = array($specSize->getWidth() / $imageSize->getWidth(), $specSize->getHeight() / $imageSize->getHeight());
             $ratio = max($ratios);
             if (!$imageSize->contains($specSize)) {
                 $imageSize = new Box(min($imageSize->getWidth(), $specSize->getWidth()), min($imageSize->getHeight(), $specSize->getHeight()));
             } else {
                 $imageSize = $imageSize->scale($ratio);
                 $image->resize($imageSize);
             }
             if ($spec->getCenterX() && $spec->getCenterY()) {
                 $point = new Point($spec->getCenterX(), $spec->getCenterY());
             } else {
                 $point = new Point(max(0, round(($imageSize->getWidth() - $specSize->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $specSize->getHeight()) / 2)));
             }
             $image->crop($point, $specSize);
         }
     }
     $image->save($outFilename, $options);
 }