/**
  * @param Image $image
  * @return array
  */
 protected function getImagePreviewData(Image $image)
 {
     $imageProperties = ['originalImageResourceUri' => $this->resourceManager->getPublicPersistentResourceUri($image->getResource()), 'originalDimensions' => ['width' => $image->getWidth(), 'height' => $image->getHeight(), 'aspectRatio' => $image->getAspectRatio()], 'mediaType' => $image->getResource()->getMediaType()];
     $thumbnail = $this->thumbnailService->getThumbnail($image, $this->thumbnailService->getThumbnailConfigurationForPreset('TYPO3.Neos:Preview'));
     if ($thumbnail !== null) {
         $imageProperties['previewImageResourceUri'] = $this->thumbnailService->getUriForThumbnail($thumbnail);
         $imageProperties['previewDimensions'] = ['width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()];
     }
     return $imageProperties;
 }
 /**
  * @param Image $image
  * @return array
  */
 protected function getImagePreviewData(Image $image)
 {
     $thumbnail = $image->getThumbnail(600, 600);
     $imageProperties = array('originalImageResourceUri' => $this->resourceManager->getPublicPersistentResourceUri($image->getResource()), 'previewImageResourceUri' => $this->resourceManager->getPublicPersistentResourceUri($thumbnail->getResource()), 'originalDimensions' => array('width' => $image->getWidth(), 'height' => $image->getHeight(), 'aspectRatio' => $image->getAspectRatio()), 'previewDimensions' => array('width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'mediaType' => $image->getResource()->getMediaType());
     return $imageProperties;
 }
    /**
     * Import resources to asset management
     *
     * This command detects Flow "Resource" objects which are not yet available as "Asset" objects and thus don't appear
     * in the asset management. The type of the imported asset is determined by the file extension provided by the
     * Resource object.
     *
     * @param boolean $simulate If set, this command will only tell what it would do instead of doing it right away
     * @return void
     */
    public function importResourcesCommand($simulate = false)
    {
        $this->initializeConnection();
        $sql = '
			SELECT
				r.persistence_object_identifier, r.filename, r.mediatype
			FROM typo3_flow_resource_resource r
			LEFT JOIN typo3_media_domain_model_asset a
			ON a.resource = r.persistence_object_identifier
			LEFT JOIN typo3_media_domain_model_thumbnail t
			ON t.resource = r.persistence_object_identifier
			WHERE a.persistence_object_identifier IS NULL AND t.persistence_object_identifier IS NULL
		';
        $statement = $this->dbalConnection->prepare($sql);
        $statement->execute();
        $resourceInfos = $statement->fetchAll();
        if ($resourceInfos === array()) {
            $this->outputLine('Found no resources which need to be imported.');
            $this->quit();
        }
        foreach ($resourceInfos as $resourceInfo) {
            $mediaType = $resourceInfo['mediatype'];
            if (substr($mediaType, 0, 6) === 'image/') {
                $resource = $this->persistenceManager->getObjectByIdentifier($resourceInfo['persistence_object_identifier'], 'TYPO3\\Flow\\Resource\\Resource');
                if ($resource === null) {
                    $this->outputLine('Warning: Resource for file "%s" seems to be corrupt. No resource object with identifier %s could be retrieved from the Persistence Manager.', array($resourceInfo['filename'], $resourceInfo['persistence_object_identifier']));
                    continue;
                }
                if (!$resource->getStream()) {
                    $this->outputLine('Warning: Resource for file "%s" seems to be corrupt. The actual data of resource %s could not be found in the resource storage.', array($resourceInfo['filename'], $resourceInfo['persistence_object_identifier']));
                    continue;
                }
                $image = new Image($resource);
                if ($simulate) {
                    $this->outputLine('Simulate: Adding new image "%s" (%sx%s px)', array($image->getResource()->getFilename(), $image->getWidth(), $image->getHeight()));
                } else {
                    $this->assetRepository->add($image);
                    $this->outputLine('Adding new image "%s" (%sx%s px)', array($image->getResource()->getFilename(), $image->getWidth(), $image->getHeight()));
                }
            }
        }
    }
 /**
  * Replaces images (<img src="Foo.png">) by persistent resources (<img src="_Resources/....">)
  *
  * @param string $bodyText
  * @return string the text with replaced image tags
  */
 protected function replaceImages($bodyText)
 {
     $self = $this;
     $configuration = $this->bundleConfiguration;
     $resourceManager = $this->resourceManager;
     $resourcePublisher = $this->resourcePublisher;
     $bodyText = preg_replace_callback('/(<img .*?src=")([^"]*)(".*?\\/>)/', function ($matches) use($self, $configuration, $resourceManager, $resourcePublisher) {
         $imageRootPath = isset($configuration['imageRootPath']) ? $configuration['imageRootPath'] : Files::concatenatePaths(array($configuration['renderedDocumentationRootPath'], '_images'));
         $imagePathAndFilename = Files::concatenatePaths(array($imageRootPath, basename($matches[2])));
         $imageResource = $resourceManager->importResource($imagePathAndFilename);
         $image = new Image($imageResource);
         if ($image->getWidth() > $configuration['imageMaxWidth'] || $image->getHeight() > $configuration['imageMaxHeight']) {
             $image = $image->getThumbnail($configuration['imageMaxWidth'], $configuration['imageMaxHeight']);
         }
         $imageUri = $resourcePublisher->publishPersistentResource($image->getResource());
         if ($image->getWidth() > $configuration['thumbnailMaxWidth'] || $image->getHeight() > $configuration['thumbnailMaxHeight']) {
             $thumbnail = $image->getThumbnail(710, 800);
             $thumbnailUri = $resourcePublisher->getPersistentResourceWebUri($thumbnail->getResource());
             return sprintf('<a href="/%s" class="lightbox">%s/%s" style="width: %dpx" /></a>', $imageUri, $matches[1], $thumbnailUri, $thumbnail->getWidth());
         } else {
             return sprintf('%s/%s" style="width: %dpx" />', $matches[1], $imageUri, $image->getWidth());
         }
     }, $bodyText);
     return $bodyText;
 }