Example #1
0
 /**
  * Create and store thumbnail derivatives.
  *
  * Gets the thumbnailer from the service manager for each call to this
  * method. This gives thumbnailers an opportunity to be non-shared services,
  * which can be useful for resolving memory allocation issues.
  *
  * @param string $source
  * @param string $storageBaseName
  * @return bool Whether thumbnails were created and stored
  */
 public function storeThumbnails(File $file)
 {
     $thumbnailer = $this->getThumbnailer();
     $tempPaths = [];
     try {
         $thumbnailer->setSource($file->getTempPath());
         $thumbnailer->setOptions($this->config['thumbnail_options']);
         foreach ($this->config['thumbnail_types'] as $type => $config) {
             $tempPaths[$type] = $thumbnailer->create($config['strategy'], $config['constraint'], $config['options']);
         }
     } catch (Exception\CannotCreateThumbnailException $e) {
         // Delete temporary files created before exception was thrown.
         foreach ($tempPaths as $tempPath) {
             @unlink($tempPath);
         }
         return false;
     }
     // Finally, store the thumbnails.
     foreach ($tempPaths as $type => $tempPath) {
         $storagePath = $this->getStoragePath($type, $file->getStorageBaseName(), self::THUMBNAIL_EXTENSION);
         $this->getStore()->put($tempPath, $storagePath);
         // Delete the temporary file in case the file store hasn't already.
         @unlink($tempPath);
     }
     return true;
 }