On a successful import this method returns a PersistentResource object representing the
newly imported persistent resource and automatically publishes it to the configured
publication target.
public importResource ( string | resource $source, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null ) : |
||
$source | string | resource | A URI (can therefore also be a path and filename) or a PHP resource stream(!) pointing to the PersistentResource to import |
$collectionName | string | Name of the collection this new resource should be added to. By default the standard collection for persistent resources is used. |
$forcedPersistenceObjectIdentifier | string | INTERNAL: Force the object identifier for this resource to the given UUID |
Résultat | A resource object representing the imported resource |
/** * Imports a new image and persists it, including one variant * * @param string $importUri * @return string */ public function importAction($importUri) { $imageResource = $this->resourceManager->importResource($importUri); $image = new Image($imageResource); $imageVariant = new ImageVariant($image); $this->assetRepository->add($image); $this->assetRepository->add($imageVariant); $this->response->setHeader('X-ImageVariantUuid', $this->persistenceManager->getIdentifierByObject($imageVariant)); return 'ok'; }
/** * @param PersistentResource $originalResource * @param array $adjustments * @return array resource, width, height as keys * @throws ImageFileException * @throws InvalidConfigurationException * @throws Exception */ 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; }
/** * @param array $source * @param PropertyMappingConfigurationInterface $configuration * @return PersistentResource|FlowError * @throws InvalidPropertyMappingConfigurationException */ protected function handleHashAndData(array $source, PropertyMappingConfigurationInterface $configuration = null) { $hash = null; $resource = false; $givenResourceIdentity = null; if (isset($source['__identity'])) { $givenResourceIdentity = $source['__identity']; unset($source['__identity']); $resource = $this->resourceRepository->findByIdentifier($givenResourceIdentity); if ($resource instanceof PersistentResource) { return $resource; } if ($configuration->getConfigurationValue(ResourceTypeConverter::class, self::CONFIGURATION_IDENTITY_CREATION_ALLOWED) !== true) { throw new InvalidPropertyMappingConfigurationException('Creation of resource objects with identity not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_IDENTITY_CREATION_ALLOWED" to TRUE'); } } if (isset($source['hash']) && preg_match('/[0-9a-f]{40}/', $source['hash'])) { $hash = $source['hash']; } if ($hash !== null && count($source) === 1) { $resource = $this->resourceManager->getResourceBySha1($hash); } if ($resource === null) { $collectionName = $this->getCollectionName($source, $configuration); if (isset($source['data'])) { $resource = $this->resourceManager->importResourceFromContent($source['data'], $source['filename'], $collectionName, $givenResourceIdentity); } elseif ($hash !== null) { /** @var PersistentResource $resource */ $resource = $this->resourceManager->importResource($configuration->getConfigurationValue(ResourceTypeConverter::class, self::CONFIGURATION_RESOURCE_LOAD_PATH) . '/' . $hash, $collectionName, $givenResourceIdentity); if (is_array($source) && isset($source['filename'])) { $resource->setFilename($source['filename']); } } } if ($resource instanceof PersistentResource) { return $resource; } else { return new FlowError('The resource manager could not create a PersistentResource instance.', 1404312901); } }