コード例 #1
0
 /**
  * @param MediaInterface $media
  *
  * @return mixed
  */
 protected function getRootCategory(MediaInterface $media)
 {
     if (!$this->rootCategories) {
         $this->rootCategories = $this->container->get('sonata.classification.manager.category')->getRootCategories(false);
     }
     if (!array_key_exists($media->getContext(), $this->rootCategories)) {
         throw new \RuntimeException(sprintf('There is no main category related to context: %s', $media->getContext()));
     }
     return $this->rootCategories[$media->getContext()];
 }
コード例 #2
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string
  */
 public function generatePath(MediaInterface $media)
 {
     $segments = preg_split('#/#', $media->getId(), null, PREG_SPLIT_NO_EMPTY);
     if (count($segments) > 1) {
         // remove last part from id
         array_pop($segments);
         $path = join($segments, '/');
     } else {
         $path = '';
     }
     return $path ? sprintf('%s/%s', $media->getContext(), $path) : $media->getContext();
 }
コード例 #3
0
    /**
     * @abstract
     * @param \Sonata\MediaBundle\Model\MediaInterface $media
     * @return string
     */
    public function generatePath(MediaInterface $media)
    {
        $rep_first_level = (int) ($media->getId() / $this->firstLevel);
        $rep_second_level = (int) (($media->getId() - ($rep_first_level * $this->firstLevel)) / $this->secondLevel);

        return sprintf('%s/%04s/%02s', $media->getContext(), $rep_first_level + 1, $rep_second_level + 1);
    }
コード例 #4
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, $this->metadata->get($media, $out->getName()));
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $size = $media->getBox();
     $skipBox = false;
     $constraint = array_key_exists('constraint', $settings) && $settings['constraint'] === false;
     if ($constraint && $size->getHeight() > $size->getWidth()) {
         $settings['width'] = $settings['height'];
         $this->mode = ImageInterface::THUMBNAIL_INSET;
         $skipBox = true;
     }
     $hasWidth = array_key_exists('width', $settings) && $settings['width'] > 0;
     $hasHeight = array_key_exists('height', $settings) && $settings['height'] > 0;
     if (!$hasWidth && !$hasHeight) {
         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 ($hasWidth && $hasHeight && !$skipBox) {
         return new Box($settings['width'], $settings['height']);
     }
     if (!$hasHeight) {
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     if (!$hasWidth) {
         $settings['width'] = (int) ($settings['height'] * $size->getWidth() / $size->getHeight());
     }
     return $this->computeBox($size, $settings);
 }
コード例 #6
0
 public function getFormatName(MediaInterface $media, $format)
 {
     if (in_array($format, array('admin', 'reference', 'orangegate'))) {
         return $format;
     }
     $baseName = $media->getContext() . '_';
     if (substr($format, 0, strlen($baseName)) == $baseName) {
         return $format;
     }
     return $baseName . str_replace('-', '_', $format);
 }
コード例 #7
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  * @param \Gaufrette\File $in
  * @param \Gaufrette\File $out
  * @param string $format
  * @param array $settings
  * @return void
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderClass()));
     }
     $image = $this->getAdapter()->load($in->getContent());
     if ($settings['height'] == null) {
         $size = $image->getSize();
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->getMode())->get($format);
     $out->setContent($content);
 }
コード例 #8
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);
 }
コード例 #9
0
 public function getFormatName(MediaInterface $media, $format)
 {
     if ($format == 'admin') {
         return 'admin';
     }
     if ($format == 'reference') {
         return 'reference';
     }
     if ($format == 'orangegate') {
         return 'orangegate';
     }
     $baseName = $media->getContext() . '_';
     if (substr($format, 0, strlen($baseName)) == $baseName) {
         return $format;
     }
     return $baseName . $format;
 }
コード例 #10
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $size = $media->getBox();
     $hasWidth = isset($settings['width']) && $settings['width'];
     $hasHeight = isset($settings['height']) && $settings['height'];
     if (!$hasWidth && !$hasHeight) {
         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 ($hasWidth && $hasHeight) {
         return new Box($settings['width'], $settings['height']);
     }
     if (!$hasHeight) {
         $settings['height'] = intval($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     if (!$hasWidth) {
         $settings['width'] = intval($settings['height'] * $size->getWidth() / $size->getHeight());
     }
     return $this->computeBox($media, $settings);
 }
コード例 #11
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string
  */
 public function getDownloadMode(MediaInterface $media)
 {
     $context = $this->getContext($media->getContext());
     return $context['download']['mode'];
 }
コード例 #12
0
 /**
  * @param MediaInterface|null $media
  *
  * @return array
  */
 protected function getFormatChoices(MediaInterface $media = null)
 {
     $formatChoices = array();
     if (!$media instanceof MediaInterface) {
         return $formatChoices;
     }
     $formats = $this->getMediaPool()->getFormatNamesByContext($media->getContext());
     foreach ($formats as $code => $format) {
         $formatChoices[$code] = $code;
     }
     return $formatChoices;
 }
コード例 #13
0
 /**
  * {@inheritdoc}
  */
 public function generatePath(MediaInterface $media)
 {
     $id = $media->getId();
     return sprintf('%s/%04s/%02s', $media->getContext(), substr($id, 0, 4), substr($id, 4, 2));
 }