Neos\Media\Domain\Service\ImageService::processImage PHP Method

processImage() public method

public processImage ( PersistentResource $originalResource, array $adjustments ) : array
$originalResource Neos\Flow\ResourceManagement\PersistentResource
$adjustments array
return array resource, width, height as keys
    public function processImage(PersistentResource $originalResource, array $adjustments)
    {
        $additionalOptions = array();
        $adjustmentsApplied = false;
        // TODO: Special handling for SVG should be refactored at a later point.
        if ($originalResource->getMediaType() === 'image/svg+xml') {
            $originalResourceStream = $originalResource->getStream();
            $resource = $this->resourceManager->importResource($originalResourceStream, $originalResource->getCollectionName());
            fclose($originalResourceStream);
            $resource->setFilename($originalResource->getFilename());
            return ['width' => null, 'height' => null, 'resource' => $resource];
        }
        $resourceUri = $originalResource->createTemporaryLocalCopy();
        $resultingFileExtension = $originalResource->getFileExtension();
        $transformedImageTemporaryPathAndFilename = $this->environment->getPathToTemporaryDirectory() . uniqid('ProcessedImage-') . '.' . $resultingFileExtension;
        if (!file_exists($resourceUri)) {
            throw new ImageFileException(sprintf('An error occurred while transforming an image: the resource data of the original image does not exist (%s, %s).', $originalResource->getSha1(), $resourceUri), 1374848224);
        }
        $imagineImage = $this->imagineService->open($resourceUri);
        $convertCMYKToRGB = $this->getOptionsMergedWithDefaults()['convertCMYKToRGB'];
        if ($convertCMYKToRGB && $imagineImage->palette() instanceof CMYK) {
            $imagineImage->usePalette(new RGB());
        }
        if ($this->imagineService instanceof Imagine && $originalResource->getFileExtension() === 'gif' && $this->isAnimatedGif(file_get_contents($resourceUri)) === true) {
            $imagineImage->layers()->coalesce();
            $layers = $imagineImage->layers();
            $newLayers = array();
            foreach ($layers as $index => $imagineFrame) {
                $imagineFrame = $this->applyAdjustments($imagineFrame, $adjustments, $adjustmentsApplied);
                $newLayers[] = $imagineFrame;
            }
            $imagineImage = array_shift($newLayers);
            $layers = $imagineImage->layers();
            foreach ($newLayers as $imagineFrame) {
                $layers->add($imagineFrame);
            }
            $additionalOptions['animated'] = true;
        } else {
            $imagineImage = $this->applyAdjustments($imagineImage, $adjustments, $adjustmentsApplied);
        }
        if ($adjustmentsApplied === true) {
            $imagineImage->save($transformedImageTemporaryPathAndFilename, $this->getOptionsMergedWithDefaults($additionalOptions));
            $imageSize = $imagineImage->getSize();
            // TODO: In the future the collectionName of the new resource should be configurable.
            $resource = $this->resourceManager->importResource($transformedImageTemporaryPathAndFilename, $originalResource->getCollectionName());
            if ($resource === false) {
                throw new ImageFileException('An error occurred while importing a generated image file as a resource.', 1413562208);
            }
            unlink($transformedImageTemporaryPathAndFilename);
            $pathInfo = UnicodeFunctions::pathinfo($originalResource->getFilename());
            $resource->setFilename(sprintf('%s-%ux%u.%s', $pathInfo['filename'], $imageSize->getWidth(), $imageSize->getHeight(), $pathInfo['extension']));
        } else {
            $originalResourceStream = $originalResource->getStream();
            $resource = $this->resourceManager->importResource($originalResourceStream, $originalResource->getCollectionName());
            fclose($originalResourceStream);
            $resource->setFilename($originalResource->getFilename());
            $imageSize = $this->getImageSize($originalResource);
            $imageSize = new Box($imageSize['width'], $imageSize['height']);
        }
        $this->imageSizeCache->set($resource->getCacheEntryIdentifier(), array('width' => $imageSize->getWidth(), 'height' => $imageSize->getHeight()));
        $result = array('width' => $imageSize->getWidth(), 'height' => $imageSize->getHeight(), 'resource' => $resource);
        return $result;
    }

Usage Example

 /**
  * @param Thumbnail $thumbnail
  * @return void
  * @throws Exception\NoThumbnailAvailableException
  */
 public function refresh(Thumbnail $thumbnail)
 {
     try {
         $adjustments = array(new ResizeImageAdjustment(array('width' => $thumbnail->getConfigurationValue('width'), 'maximumWidth' => $thumbnail->getConfigurationValue('maximumWidth'), 'height' => $thumbnail->getConfigurationValue('height'), 'maximumHeight' => $thumbnail->getConfigurationValue('maximumHeight'), 'ratioMode' => $thumbnail->getConfigurationValue('ratioMode'), 'allowUpScaling' => $thumbnail->getConfigurationValue('allowUpScaling'))));
         $processedImageInfo = $this->imageService->processImage($thumbnail->getOriginalAsset()->getResource(), $adjustments);
         $thumbnail->setResource($processedImageInfo['resource']);
         $thumbnail->setWidth($processedImageInfo['width']);
         $thumbnail->setHeight($processedImageInfo['height']);
     } catch (\Exception $exception) {
         $message = sprintf('Unable to generate thumbnail for the given image (filename: %s, SHA1: %s)', $thumbnail->getOriginalAsset()->getResource()->getFilename(), $thumbnail->getOriginalAsset()->getResource()->getSha1());
         throw new Exception\NoThumbnailAvailableException($message, 1433109654, $exception);
     }
 }
All Usage Examples Of Neos\Media\Domain\Service\ImageService::processImage