/**
  * {@inheritDoc}
  */
 public function find($path)
 {
     if (false !== strpos($path, '../')) {
         throw new NotLoadableException(sprintf("Source image was searched with '%s' out side of the defined root path", $path));
     }
     $absolutePath = $this->rootPath . '/' . ltrim($path, '/');
     if (false == file_exists($absolutePath)) {
         throw new NotLoadableException(sprintf('Source image not found in "%s"', $absolutePath));
     }
     $mimeType = $this->mimeTypeGuesser->guess($absolutePath);
     return new Binary(file_get_contents($absolutePath), $mimeType, $this->extensionGuesser->guess($mimeType));
 }
示例#2
0
 /**
  * {@inheritDoc}
  */
 public function find($path)
 {
     $s3Url = $this->client->getObjectUrl($this->bucket, trim($this->prefix, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR));
     if (false == @getimagesize($s3Url)) {
         return $this->fallbackLoader->find($path);
     }
     $tmpFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($s3Url);
     file_put_contents($tmpFilePath, file_get_contents($s3Url));
     $mimeType = $this->mimeTypeGuesser->guess($tmpFilePath);
     unlink($tmpFilePath);
     return new Binary(file_get_contents($s3Url), $mimeType, $this->extensionGuesser->guess($mimeType));
 }
 /**
  * {@inheritDoc}
  */
 public function guess($binary)
 {
     $tmpFile = tempnam(sys_get_temp_dir(), 'liip-imagine-bundle');
     try {
         file_put_contents($tmpFile, $binary);
         $mimeType = $this->mimeTypeGuesser->guess($tmpFile);
         unlink($tmpFile);
         return $mimeType;
     } catch (\Exception $e) {
         unlink($tmpFile);
         throw $e;
     }
 }
示例#4
0
 /**
  * {@inheritDoc}
  */
 public function find($path)
 {
     if (false !== strpos($path, '../')) {
         throw new NotLoadableException(sprintf("Source image was searched with '%s' out side of the defined root path", $path));
     }
     if (false == @getimagesize($path)) {
         throw new NotLoadableException(sprintf('Source image not found in "%s"', $path));
     }
     $tmpFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($path);
     file_put_contents($tmpFilePath, file_get_contents($path));
     $mimeType = $this->mimeTypeGuesser->guess($tmpFilePath);
     unlink($tmpFilePath);
     return new Binary(file_get_contents($path), $mimeType, $this->extensionGuesser->guess($mimeType));
 }
 /**
  * {@inheritdoc}
  */
 public function guess($binary)
 {
     if (false === ($tmpFile = tempnam(sys_get_temp_dir(), 'liip-imagine-bundle'))) {
         throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', sys_get_temp_dir()));
     }
     try {
         file_put_contents($tmpFile, $binary);
         $mimeType = $this->mimeTypeGuesser->guess($tmpFile);
         unlink($tmpFile);
         return $mimeType;
     } catch (\Exception $e) {
         unlink($tmpFile);
         throw $e;
     }
 }
示例#6
0
 /**
  * @param Media $media
  *
  * @throws \RuntimeException when the file does not exist
  */
 public function prepareMedia(Media $media)
 {
     if (null === $media->getUuid()) {
         $uuid = uniqid();
         $media->setUuid($uuid);
     }
     $content = $media->getContent();
     if (empty($content)) {
         return;
     }
     if (!$content instanceof File) {
         if (!is_file($content)) {
             throw new \RuntimeException('Invalid file');
         }
         $file = new File($content);
         $media->setContent($file);
     }
     if ($content instanceof UploadedFile) {
         $pathInfo = pathinfo($content->getClientOriginalName());
         $media->setOriginalFilename($this->urlizer->urlize($pathInfo['filename']) . '.' . $pathInfo['extension']);
         $name = $media->getName();
         if (empty($name)) {
             $media->setName($media->getOriginalFilename());
         }
     }
     $media->setFileSize(filesize($media->getContent()));
     $contentType = $this->mimeTypeGuesser->guess($media->getContent()->getPathname());
     $media->setContentType($contentType);
     $relativePath = sprintf('/%s.%s', $media->getUuid(), ExtensionGuesser::getInstance()->guess($media->getContentType()));
     $media->setUrl('/uploads/media' . $relativePath);
     $media->setLocation('local');
 }
 /**
  * {@inheritdoc}
  */
 public function transformDimensions(array &$dimensions, $uri)
 {
     // Test to see if EXIF is supported by the image format.
     $mime_type = $this->mimeTypeGuesser->guess($uri);
     if (!in_array($mime_type, ['image/jpeg', 'image/tiff'])) {
         // Not an EXIF enabled image, return.
         return;
     }
     if ($dimensions['width'] && $dimensions['height'] && $this->configuration['scan_exif']) {
         // Both dimensions in input, and effect is configured to check the
         // the input file. Read EXIF data, and determine image orientation.
         if (($file_path = $this->fileSystem->realpath($uri)) && function_exists('exif_read_data')) {
             if ($exif_data = @exif_read_data($file_path)) {
                 $orientation = isset($exif_data['Orientation']) ? $exif_data['Orientation'] : NULL;
                 if (in_array($orientation, [5, 6, 7, 8])) {
                     $tmp = $dimensions['width'];
                     $dimensions['width'] = $dimensions['height'];
                     $dimensions['height'] = $tmp;
                 }
                 return;
             }
         }
     }
     // Either no full dimensions in input, or effect is configured to skip
     // checking the input file, or EXIF extension is missing. Set both
     // dimensions to NULL.
     $dimensions['width'] = $dimensions['height'] = NULL;
 }
示例#8
0
 /**
  * Gets the mime type of the object. Defaults to application/octet-stream.
  *
  * @param \SplFileInfo $object
  *
  * @return string
  */
 private function getMimeType(\SplFileInfo $object)
 {
     if ($object instanceof File) {
         return $object->getMimeType();
     }
     if ($this->mimeTypeGuesser && ($mimeType = $this->mimeTypeGuesser->guess($object->getPathname()))) {
         return $mimeType;
     }
     return 'application/octet-stream';
 }
示例#9
0
 public function postProcess($object)
 {
     if (!$object instanceof User) {
         return;
     }
     $imagePath = $object->getAvatarImage();
     // No image given, bail out
     if (empty($imagePath)) {
         return;
     }
     $fullPath = "{$this->vendorBaseDir}/{$imagePath}";
     // Image doesn't exist, bail out
     if (!file_exists($fullPath)) {
         return;
     }
     $mimeType = $this->mimeTypeGuesser->guess($fullPath);
     $image = new Binary(file_get_contents($fullPath), $mimeType, $this->extensionGuesser->guess($mimeType));
     $avatarImage = $this->userService->storeImageAsAvatar($image);
     $object->setAvatarImage($avatarImage);
 }
 public function postBuild(Fixture $fixture)
 {
     /** @var Media $media */
     $media = $fixture->getEntity();
     $filePath = $media->getOriginalFilename();
     $data = new File($filePath, true);
     $contentType = $this->mimeTypeGuesser->guess($data->getPathname());
     if (method_exists($data, 'getClientOriginalName')) {
         $media->setOriginalFilename($data->getClientOriginalName());
     } else {
         $media->setOriginalFilename($data->getFilename());
     }
     if ($media->getName() === null) {
         $media->setName($media->getOriginalFilename());
     }
     $media->setContent($data);
     $media->setContentType($contentType);
     $media->setFolder($this->folder);
     $this->fileHandler->prepareMedia($media);
     $this->fileHandler->updateMedia($media);
     $this->fileHandler->saveMedia($media);
 }
 /**
  * @param mixed $data
  *
  * @return Media
  */
 public function createNew($data)
 {
     if ($data instanceof File) {
         /** @var $data File */
         $media = new Media();
         if (method_exists($data, 'getClientOriginalName')) {
             $media->setOriginalFilename($data->getClientOriginalName());
         } else {
             $media->setOriginalFilename($data->getFilename());
         }
         $media->setContent($data);
         $contentType = $this->mimeTypeGuesser->guess($media->getContent()->getPathname());
         $media->setContentType($contentType);
         return $media;
     }
     return null;
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     parent::submitForm($form, $form_state);
     $config_key = $form_state->getValue('config_key');
     $this->editableConfig = [$config_key];
     $config = $this->config($config_key);
     // Exclude unnecessary elements before saving.
     $form_state->cleanValues();
     $form_state->unsetValue('var');
     $form_state->unsetValue('config_key');
     $values = $form_state->getValues();
     // If the user uploaded a new logo or favicon, save it to a permanent location
     // and use it in place of the default theme-provided file.
     if ($this->moduleHandler->moduleExists('file')) {
         if ($file = $values['logo_upload']) {
             $filename = file_unmanaged_copy($file->getFileUri());
             $values['default_logo'] = 0;
             $values['logo_path'] = $filename;
             $values['toggle_logo'] = 1;
         }
         if ($file = $values['favicon_upload']) {
             $filename = file_unmanaged_copy($file->getFileUri());
             $values['default_favicon'] = 0;
             $values['favicon_path'] = $filename;
             $values['toggle_favicon'] = 1;
         }
         unset($values['logo_upload']);
         unset($values['favicon_upload']);
         // If the user entered a path relative to the system files directory for
         // a logo or favicon, store a public:// URI so the theme system can handle it.
         if (!empty($values['logo_path'])) {
             $values['logo_path'] = $this->validatePath($values['logo_path']);
         }
         if (!empty($values['favicon_path'])) {
             $values['favicon_path'] = $this->validatePath($values['favicon_path']);
         }
         if (empty($values['default_favicon']) && !empty($values['favicon_path'])) {
             $values['favicon_mimetype'] = $this->mimeTypeGuesser->guess($values['favicon_path']);
         }
     }
     theme_settings_convert_to_config($values, $config)->save();
 }