Exemplo n.º 1
0
 /**
  * The given $value is valid if it is an \TYPO3\Flow\Resource\Resource of the configured resolution
  * Note: a value of NULL or empty string ('') is considered valid
  *
  * @param \TYPO3\Flow\Resource\Resource $resource The resource object that should be validated
  * @return void
  * @api
  */
 protected function isValid($resource)
 {
     if (!$resource instanceof \TYPO3\Flow\Resource\Resource) {
         $this->addError('The given value was not a Resource instance.', 1327865587);
         return;
     }
     $fileExtension = $resource->getFileExtension();
     if ($fileExtension === null || $fileExtension === '') {
         $this->addError('The file has no file extension.', 1327865808);
         return;
     }
     if (!in_array($fileExtension, $this->options['allowedExtensions'])) {
         $this->addError('The file extension "%s" is not allowed.', 1327865764, array($resource->getFileExtension()));
         return;
     }
 }
 /**
  * @test
  */
 public function setFilenameDoesNotAppendFileExtensionIfItIsEmpty()
 {
     $resource = new Resource();
     $resource->setFilename('FileWithoutExtension');
     $this->assertSame('', $resource->getFileExtension());
     $this->assertSame('FileWithoutExtension', $resource->getFilename());
 }
 /**
  * Returns the publish path and filename to be used to publish the specified persistent resource
  *
  * @param \TYPO3\Flow\Resource\Resource $resource The resource to build the publish path and filename for
  * @param boolean $returnFilename FALSE if only the directory without the filename should be returned
  * @return string The publish path and filename
  */
 protected function buildPersistentResourcePublishPathAndFilename(\TYPO3\Flow\Resource\Resource $resource, $returnFilename)
 {
     $publishPath = $this->resourcesPublishingPath . 'Persistent/';
     if ($returnFilename === TRUE) {
         return $publishPath . $resource->getResourcePointer()->getHash() . '.' . $resource->getFileExtension();
     }
     return $publishPath;
 }
 /**
  * {@inheritDoc}
  */
 public function getFileExtension()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getFileExtension', array());
     return parent::getFileExtension();
 }
 /**
  * @param FlowResource $originalResource
  * @param array $adjustments
  * @return array resource, width, height as keys
  * @throws ImageFileException
  * @throws InvalidConfigurationException
  * @throws \TYPO3\Flow\Resource\Exception
  */
 public function processImage(FlowResource $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);
     if ($this->imagineService instanceof \Imagine\Imagick\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;
 }