/** * {@inheritdoc} */ public function convert(FileVersion $fileVersion, $formatKey) { $content = $this->storage->loadAsString($fileVersion->getName(), $fileVersion->getVersion(), $fileVersion->getStorageOptions()); $extractedImage = $this->mediaImageExtractor->extract($content); try { $image = $this->imagine->load($extractedImage); } catch (RuntimeException $e) { throw new InvalidFileTypeException($e->getMessage()); } $image = $this->toRGB($image); $format = $this->getFormat($formatKey); $cropParameters = $this->getCropParameters($image, $fileVersion->getFormatOptions()->get($formatKey), $this->formats[$formatKey]); if (isset($cropParameters)) { $image = $this->applyFormatCrop($image, $cropParameters); } if (isset($format['scale']) && $format['scale']['mode'] !== ImageInterface::THUMBNAIL_INSET) { $image = $this->applyFocus($image, $fileVersion, $format['scale']); } if (isset($format['scale'])) { $image = $this->applyScale($image, $format['scale']); } if (isset($format['transformations'])) { $image = $this->applyTransformations($image, $format['transformations']); } $image->strip(); // Set Interlacing to plane for smaller image size. if (count($image->layers()) == 1) { $image->interlace(ImageInterface::INTERLACE_PLANE); } $imagineOptions = $format['options']; $imageExtension = $this->getImageExtension($fileVersion->getName()); return $image->get($imageExtension, $this->getOptionsFromImage($image, $imageExtension, $imagineOptions)); }
/** * {@inheritdoc} */ public function returnImage($id, $formatName) { $setExpireHeaders = false; try { // load Media $media = $this->mediaRepository->findMediaById($id); if (!$media) { throw new ImageProxyMediaNotFoundException('Media was not found'); } // load Media Data list($fileName, $version, $storageOptions, $mimeType) = $this->getMediaData($media); try { // check if file has supported preview if (!$this->checkPreviewSupported($mimeType)) { throw new InvalidMimeTypeForPreviewException($mimeType); } // get format options $format = $this->getFormat($formatName); $formatOptions = $format['options']; // load Original $uri = $this->originalStorage->load($fileName, $version, $storageOptions); $original = $this->createTmpFile($this->getFile($uri, $mimeType)); // prepare Media $this->prepareMedia($mimeType, $original); // convert Media to format $image = $this->converter->convert($original, $format); // remove profiles and comments $image->strip(); // set Interlacing to plane for smaller image size if (count($image->layers()) == 1) { $image->interlace(ImageInterface::INTERLACE_PLANE); } // set extension $imageExtension = $this->getImageExtension($fileName); // get image $responseContent = $image->get($imageExtension, $this->getOptionsFromImage($image, $imageExtension, $formatOptions)); // HTTP Headers $status = 200; $setExpireHeaders = true; // save image if ($this->saveImage) { $this->formatCache->save($this->createTmpFile($responseContent), $media->getId(), $this->replaceExtension($fileName, $imageExtension), $storageOptions, $formatName); } } catch (MediaException $e) { // return when available a file extension icon list($responseContent, $status, $imageExtension) = $this->returnFileExtensionIcon($formatName, $this->getRealFileExtension($fileName), $e); } $responseMimeType = 'image/' . $imageExtension; } catch (MediaException $e) { $responseContent = $e->getCode() . ': ' . $e->getMessage(); $status = 404; $responseMimeType = 'text/plain'; } // clear temp files $this->clearTempFiles(); // set header $headers = $this->getResponseHeaders($responseMimeType, $setExpireHeaders); // return image return new Response($responseContent, $status, $headers); }
/** * Prepares data. * * @param UploadedFile $uploadedFile * @param array $data * @param UserInterface $user * * @return Media * * @throws InvalidFileException */ private function buildData($uploadedFile, $data, $user) { if (!$uploadedFile instanceof UploadedFile) { throw new InvalidFileException('Given uploaded file is not of instance UploadedFile'); } $this->validator->validate($uploadedFile); $data['storageOptions'] = $this->storage->save($uploadedFile->getPathname(), $uploadedFile->getClientOriginalName(), 1); $data['name'] = $uploadedFile->getClientOriginalName(); $data['size'] = $uploadedFile->getSize(); $data['mimeType'] = $uploadedFile->getMimeType(); $data['type'] = ['id' => $this->typeManager->getMediaType($uploadedFile->getMimeType())]; return $this->createMedia($data, $user); }
/** * {@inheritdoc} */ public function delete($id, $checkSecurity = false) { $mediaEntity = $this->getEntityById($id); if ($checkSecurity) { $this->securityChecker->checkPermission(new SecurityCondition('sulu.media.collections', null, Collection::class, $mediaEntity->getCollection()->getId()), PermissionTypes::DELETE); } /** @var File $file */ foreach ($mediaEntity->getFiles() as $file) { /** @var FileVersion $fileVersion */ foreach ($file->getFileVersions() as $fileVersion) { $this->formatManager->purge($mediaEntity->getId(), $fileVersion->getName(), $fileVersion->getStorageOptions()); $this->storage->remove($fileVersion->getStorageOptions()); } } $this->em->remove($mediaEntity); $this->em->flush(); }