/** * @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; }
/** * @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); }
/** * {@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); }
/** * {@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; }
/** * @param \Sonata\MediaBundle\Model\MediaInterface $media * * @return string */ public function getDownloadMode(MediaInterface $media) { $context = $this->getContext($media->getContext()); return $context['download']['mode']; }
/** * @param MediaInterface $media * * @return string */ protected function generateReferenceName(MediaInterface $media) { return sha1($media->getName() . rand(11111, 99999)) . '.' . $media->getBinaryContent()->guessExtension(); }
/** * {@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); }
/** * {@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); }
/** * {@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; }