Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderName()));
     }
     $image = $this->adapter->load($in->getContent());
     $size = $media->getBox();
     if (null != $settings['height']) {
         if ($size->getHeight() > $size->getWidth()) {
             $higher = $size->getHeight();
             $lower = $size->getWidth();
         } else {
             $higher = $size->getWidth();
             $lower = $size->getHeight();
         }
         $crop = $higher - $lower;
         if ($crop > 0) {
             $point = $higher == $size->getHeight() ? new Point(0, 0) : new Point($crop / 2, 0);
             $image->crop($point, new Box($lower, $lower));
             $size = $image->getSize();
         }
     }
     $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
         $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->mode)->get($format, array('quality' => $settings['quality']));
     } else {
         $content = $image->get($format, array('quality' => $settings['quality']));
     }
     $out->setContent($content);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $size = $media->getBox();
     if ($settings['width'] == null && $settings['height'] == null) {
         throw new \RuntimeException(sprintf('Width/Height parameter is missing in context "%s" for provider "%s". Please add at least one parameter.', $media->getContext(), $media->getProviderName()));
     }
     if ($settings['height'] == null) {
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     if ($settings['width'] == null) {
         $settings['width'] = (int) ($settings['height'] * $size->getWidth() / $size->getHeight());
     }
     return $this->computeBox($media, $settings);
 }
Esempio n. 3
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string
  */
 public function getDownloadMode(MediaInterface $media)
 {
     $context = $this->getContext($media->getContext());
     return $context['download']['mode'];
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function generatePath(MediaInterface $media)
 {
     $repFirstLevel = (int) ($media->getId() / $this->firstLevel);
     $repSecondLevel = (int) (($media->getId() - $repFirstLevel * $this->firstLevel) / $this->secondLevel);
     return sprintf('%s/%04s/%02s', $media->getContext(), $repFirstLevel + 1, $repSecondLevel + 1);
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function getFormatName(MediaInterface $media, $format)
 {
     if ($format == 'admin') {
         return 'admin';
     }
     if ($format == 'reference') {
         return 'reference';
     }
     $baseName = $media->getContext() . '_';
     if (substr($format, 0, strlen($baseName)) == $baseName) {
         return $format;
     }
     return $baseName . $format;
 }