/** * @param BinaryInterface $binary * @param array $config * * @throws \InvalidArgumentException * * @return Binary */ public function apply(BinaryInterface $binary, array $config) { $config = array_replace(array('filters' => array(), 'quality' => 100, 'animated' => false), $config); $image = $this->imagine->load($binary->getContent()); foreach ($config['filters'] as $eachFilter => $eachOptions) { if (!isset($this->loaders[$eachFilter])) { throw new \InvalidArgumentException(sprintf('Could not find filter loader for "%s" filter type', $eachFilter)); } $image = $this->loaders[$eachFilter]->load($image, $eachOptions); } $options = array('quality' => $config['quality']); if (isset($config['jpeg_quality'])) { $options['jpeg_quality'] = $config['jpeg_quality']; } if (isset($config['png_compression_level'])) { $options['png_compression_level'] = $config['png_compression_level']; } if (isset($config['png_compression_filter'])) { $options['png_compression_filter'] = $config['png_compression_filter']; } if ($binary->getFormat() === 'gif' && $config['animated']) { $options['animated'] = $config['animated']; } $filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat(); $filteredContent = $image->get($filteredFormat, $options); $filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent); return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); }
/** * Retrieves an image with the given filter applied. * * @param string $filter * @param string $path * * @throws \LogicException * * @return \Tecnoready\ImagineService\Binary\BinaryInterface */ public function find($filter, $path) { $loader = $this->getLoader($filter); $binary = $loader->find($path); if (!$binary instanceof BinaryInterface) { $mimeType = $this->mimeTypeGuesser->guess($binary); $binary = new Binary($binary, $mimeType, $this->extensionGuesser->guess($mimeType)); } if (null === $binary->getMimeType()) { throw new \LogicException(sprintf('The mime type of image %s was not guessed.', $path)); } if (0 !== strpos($binary->getMimeType(), 'image/')) { throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.', $path, $binary->getMimeType())); } return $binary; }