First of all, you need to create a resizer service in your kit to put it in the configuration of the Sonata sonata package.
The second argument to the service in this case should be " outbound ". Valid parameters are ImageInterface::THUMBNAIL_INSET and ImageInterface::THUMBNAIL_OUTBOUND .
Now the code is Acme\Bundle\CoreBundle\Resizer\CustomResizer :
<?php namespace Acme\Bundle\CoreBundle\Resizer; use Imagine\Image\ImagineInterface; use Imagine\Image\Box; use Gaufrette\File; use Sonata\MediaBundle\Model\MediaInterface; use Sonata\MediaBundle\Resizer\ResizerInterface; use Imagine\Image\ImageInterface; use Imagine\Exception\InvalidArgumentException; use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; class CustomResizer implements ResizerInterface { protected $adapter; protected $mode; protected $metadata; public function __construct(ImagineInterface $adapter, $mode, MetadataBuilderInterface $metadata) { $this->adapter = $adapter; $this->mode = $mode; $this->metadata = $metadata; } public function resize(MediaInterface $media, File $in, File $out, $format, array $settings) { if (!(isset($settings['width']) && $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()); $content = $image ->thumbnail($this->getBox($media, $settings), $this->mode) ->get($format, array('quality' => $settings['quality'])); $out->setContent($content, $this->metadata->get($media, $out->getName())); } 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); } 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 = [ $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); } }
Well done. Your service is defined. You must link it in app/config.yml and everything will be done. I have included the entire sonata_media configuration to provide a good example, but remember that you only need the last three lines.
sonata_media: default_context: default db_driver: doctrine_orm
source share