title: Symfony Php: emulate FileUpload and create UploadedFile
body: <p>On a Symfony Commandline ContainerAwareCommand I want to emulate a file upload in order to call the following Method:</p>
  
  <pre><code>namespace AppUserBundle\Services;
  
  use PcMagas\AppImageBundle\Filters\Crop\CropFilter;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Imagine\Image\ImageInterface;
  use PcMagas\AppImageBundle\Filters\Resize\ResizeToLimitsKeepintAspectRatio;
  use PcMagas\AppImageBundle\Filters\Resize\ResizeParams;
  use PcMagas\AppImageBundle\Loader\ImageLoaderInterface;
  use PcMagas\AppImageBundle\Saver\SaverInterface;
  use Imagine\Image\Box;
  
  class ProfileImageGenerator implements UploadedFileProcessor
  {
      const CROP_PARAMS='crop';
  
      /**
       * @var CropFilter
       */
      private $crop=null;
  
      /**
       * @var ThumbnailFilterLoader
       */
      private $thumbnail=null;
  
      /**
       * @var ResizeParams
       */
      private $resizeParams=null;
  
      /**
       * @var ImageLoaderInterface
       */
      private $imageLoader=null;
  
      /**
       * @var SaverInterface
       */
      private $imageSaver=null;
  
      public function __construct(CropFilter $crop,
                                      ResizeToLimitsKeepintAspectRatio $thumbnail,
                                      ImageLoaderInterface $imageLoader,
                                      SaverInterface $imageSaver,
                                      $thumbnailWidth,
                                      $thumbnailHeight
      ){
          $this-&gt;crop=$crop;  
          $this-&gt;thumbnail=$thumbnail;
  
          $this-&gt;imageLoader=$imageLoader;
          $this-&gt;imageSaver=$imageSaver;
  
          if($thumbnailWidth&gt;0 &amp;&amp; $this-&gt;thumbnailHeight&gt;0){      
              $this-&gt;resizeParams= new Box($thumbnailWidth,$thumbnailHeight);
          }
      }
  
  
      /**
       * {@inheritDoc}
       * @see \AppUserBundle\Services\UploadedFileProcessor::process()
       */
      public function process(UploadedFile $f, array $params) 
      {
          $image=$this-&gt;openUploadedFileAsImageInterface($f);
  
                  //I implement in such a manner to provide extra prossessings over thumbnail image
          if(isset($params[self::CROP_PARAMS])){
              $image=$this-&gt;crop-&gt;apply($image, $params[self::CROP_PARAMS]);
          }
  
          if($this-&gt;resizeParams){        
              $image=$this-&gt;thumbnail-&gt;apply($image,$this-&gt;resizeParams);
          }
  
          return $this-&gt;generateFileFromImageInterface($image);
      }
  
      /**
       * @param UploadedFile $f
       * @return ImageInterface
       */
      private function openUploadedFileAsImageInterface(UploadedFile $f)
      {
          return $this-&gt;imageLoader($f-&gt;getContents());
      }
  
      /**
       * @param ImageInterface $image
       * @return Symfony\Component\HttpFoundation\File
       * @throws RuntimeException
       */
      private function generateFileFromImageInterface(ImageInterface $image)
      {
          $tmpName=tempnam(sys_get_temp_dir()).'.png';
                  return $this-&gt;imageSaver-&gt;save($image); 
      }
  }
  </code></pre>
  
  <p>Now I want to see how the method <code>process</code> will behave so I created the following <code>ContainerAwareCommand</code> In order to emulate a file upload:</p>
  
  <pre><code>namespace AppUserBundle\Command;
  
  use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Output\OutputInterface;
  use Symfony\Component\Console\Input\InputArgument;
  use AppUserBundle\Services\ProfileImageGenerator;
  use PcMagas\AppImageBundle\Filters\Crop\CropParams;
  
  class CreateProfileImageCommand extends ContainerAwareCommand
  {
      protected function configure()
      {
          $this-&gt;setName('appsuserbundle:create:profile:image')
              -&gt;setDecrtiption("Process a file image like image profile.")
              -&gt;setHelp("This command allows you to generate a file like a process image.")
              -&gt;addArgument('file',InputArgument::REQUIRED,'The image file to process.');
      }
  
      protected function execute(InputInterface $input, OutputInterface $output)
      {
          $file=$input-&gt;getArgument('file');
  
          /**
           * @var AppUserBundle\Services\ProfileImageGenerator $container
           */
          $imageGenerator=$this-&gt;getContainer()-&gt;getDefinition('app_user.thumbnail_generator');
  
          $cropParams=new CropParams(5,5,10,10);
          $file=null;//How To emulate an uploadedfile with realData?
          $processedFile=$imageGenerator-&gt;process($file,[ProfileImageGenerator::CROP_PARAMS=&gt;$cropParams])
      }
  }
  </code></pre>
  
  <p>But I am stuck on how to create an Uploadedfile from a filesystem image in order to see whether the image works do you have ansy somt of idea how to do that?</p>