Gregwar\ImageBundle\Services\ImageHandling::open PHP Method

open() public method

Get a manipulable image instance.
public open ( string $file ) : Gregwar\ImageBundle\ImageHandler
$file string the image path
return Gregwar\ImageBundle\ImageHandler a manipulable image instance
    public function open($file)
    {
        if (strlen($file) >= 1 && $file[0] == '@') {
            try {
                if ($this->fileLocator instanceof FileLocatorInterface) {
                    $file = $this->fileLocator->locate($file);
                } else {
                    $this->fileLocator->locateResource($file);
                }
            } catch (\InvalidArgumentException $exception) {
                if ($this->throwException || false == $this->fallbackImage) {
                    throw $exception;
                }
                $file = $this->fallbackImage;
            }
        }
        return $this->createInstance($file);
    }

Usage Example

 /**
  * Au post upload, je vais générer les redimentionnements définis en conf
  * @param  UploadEvent $event [description]
  * @return [type]             [description]
  */
 public function onPostUpload(Event $event)
 {
     $object = $event->getObject();
     $mapping = $event->getMapping();
     $file = $mapping->getFile($object);
     $mimetype = $file->getMimeType();
     $mimetype_parts = explode('/', $mimetype);
     if (is_array($mimetype_parts) && count($mimetype_parts) == 2 && $mimetype_parts[0] == 'image') {
         $adapter = $this->filesystem_map->get($mapping->getUploadDestination())->getAdapter();
         $base_directory = $mapping->hasDirectoryNamer() ? $mapping->getDirectoryNamer()->directoryName($object, $mapping) . '/' : '';
         foreach ($this->resizes as $suffix => $resize_conf) {
             $resize_file = $base_directory . ResizedNamer::getName($mapping->getFileName($object), $suffix);
             // Génération du resize
             $image_content = $this->image_handling->open($file->getPathname())->resize($resize_conf['width'], $resize_conf['height'])->get();
             // copie du resize
             $adapter->setMetadata($resize_file, array('contentType' => $mimetype));
             $adapter->write($resize_file, $image_content);
         }
     }
 }
All Usage Examples Of Gregwar\ImageBundle\Services\ImageHandling::open