Example #1
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string the file extension for the $media, or the $defaultExtension if not available
  */
 protected function getExtension(MediaInterface $media)
 {
     $ext = $media->getExtension();
     if (!is_string($ext) || strlen($ext) < 3) {
         $ext = $this->defaultFormat;
     }
     return $ext;
 }
Example #2
0
 /**
  * @throws InvalidArgumentException
  *
  * @param MediaInterface $media
  * @param array          $settings
  *
  * @return Box
  */
 private function computeBox(MediaInterface $media, array $settings)
 {
     if ($this->mode !== ImageInterface::THUMBNAIL_INSET && $this->mode !== ImageInterface::THUMBNAIL_OUTBOUND) {
         throw new InvalidArgumentException('Invalid mode specified');
     }
     $size = $media->getBox();
     $ratios = array($settings['width'] / $size->getWidth(), $settings['height'] / $size->getHeight());
     if ($this->mode === ImageInterface::THUMBNAIL_INSET) {
         $ratio = min($ratios);
     } else {
         $ratio = max($ratios);
     }
     return $size->scale($ratio);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function getHelperProperties(MediaInterface $media, $format, $options = array())
 {
     if ($format == 'reference') {
         $box = $media->getBox();
     } else {
         $resizerFormat = $this->getFormat($format);
         if ($resizerFormat === false) {
             throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                     Is the format registered in your zym_media configuration?', $format));
         }
         $box = $this->resizer->getBox($media, $resizerFormat);
     }
     return array_merge(array('title' => $media->getName(), 'src' => $this->generatePublicUrl($media, $format), 'width' => $box->getWidth(), 'height' => $box->getHeight()), $options);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $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();
         }
         if ($higher - $lower > 0) {
             return new Box($lower, $lower);
         }
     }
     $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
         return new Box($settings['width'], $settings['height']);
     }
     return $size;
 }
Example #5
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string
  */
 public function getDownloadMode(MediaInterface $media)
 {
     $context = $this->getContext($media->getContext());
     return $context['download']['mode'];
 }
Example #6
0
 /**
  * @param MediaInterface $media
  *
  * @return string
  */
 protected function generateReferenceName(MediaInterface $media)
 {
     return sha1($media->getName() . rand(11111, 99999)) . '.' . $media->getBinaryContent()->guessExtension();
 }
Example #7
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);
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
 {
     return new RedirectResponse(sprintf('http://www.youtube.com/watch?v=%s', $media->getProviderReference()), 302, $headers);
 }
Example #9
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;
 }