コード例 #1
0
ファイル: Manager.php プロジェクト: harentius/blog-bundle
 /**
  * @param UploadedFile|null $uploadedFile
  * @param array $options
  * @return AssetFile
  */
 public function handleUpload(UploadedFile $uploadedFile = null, array $options = [])
 {
     $resolver = new OptionsResolver();
     $resolver->setDefaults(['type' => null, 'fallbackType' => null, 'targetUri' => null])->setAllowedTypes(['type' => ['string', 'null'], 'fallbackType' => ['int', 'null'], 'targetUri' => ['string', 'null']])->setAllowedValues(['type' => ['image', 'audio', 'file', null]]);
     $options = $resolver->resolve($options);
     if (!$uploadedFile instanceof UploadedFile || !$uploadedFile->isValid() || !($assetFile = new AssetFile($uploadedFile, null, $options['fallbackType'])) || $assetFile->getType() === null) {
         throw new \RuntimeException('Invalid uploaded file');
     }
     $assetFile->setOriginalName($uploadedFile->getClientOriginalName());
     if ($options['type'] !== null) {
         $this->validateAssetFileType($assetFile, $options['type']);
     }
     if ($options['targetUri'] !== null) {
         $uploadsDir = $this->assetsResolver->uriToPath($options['targetUri']);
     } else {
         $uploadsDir = $this->assetsResolver->assetPath($assetFile->getType());
     }
     $tempFile = $uploadedFile->move($uploadsDir, $this->getTargetFileName($uploadedFile->getClientOriginalName(), $uploadsDir));
     $assetFile->setFile($tempFile);
     $uri = $this->assetsResolver->pathToUri($assetFile->getFile()->getPathname());
     if ($uri === null) {
         throw new \RuntimeException('Unable to retrieve uploaded file uri');
     }
     $assetFile->setUri($uri);
     return $assetFile;
 }
コード例 #2
0
 /**
  * @param ImagePreview $imagePreviewData
  * @return string
  */
 private function resize(ImagePreview $imagePreviewData)
 {
     $filter = 'preview';
     if (!$this->cacheManager->isStored($imagePreviewData->getTargetName(), $filter)) {
         $binary = $this->dataManager->find($filter, $imagePreviewData->getSourceUri());
         $filteredBinary = $this->filterManager->applyFilter($binary, $filter, ['filters' => ['thumbnail' => ['size' => [$imagePreviewData->getWidth(), $imagePreviewData->getHeight()]]]]);
         $this->cacheManager->store($filteredBinary, $imagePreviewData->getTargetName(), $filter);
     }
     return $this->assetsResolver->uriToPath($this->targetBasePath . $imagePreviewData->getTargetName());
 }