doucang2871 2017-07-31 10:47
浏览 95
已采纳

Symfony Php:模拟FileUpload并创建UploadedFile

On a Symfony Commandline ContainerAwareCommand I want to emulate a file upload in order to call the following Method:

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->crop=$crop;  
        $this->thumbnail=$thumbnail;

        $this->imageLoader=$imageLoader;
        $this->imageSaver=$imageSaver;

        if($thumbnailWidth>0 && $this->thumbnailHeight>0){      
            $this->resizeParams= new Box($thumbnailWidth,$thumbnailHeight);
        }
    }


    /**
     * {@inheritDoc}
     * @see \AppUserBundle\Services\UploadedFileProcessor::process()
     */
    public function process(UploadedFile $f, array $params) 
    {
        $image=$this->openUploadedFileAsImageInterface($f);

                //I implement in such a manner to provide extra prossessings over thumbnail image
        if(isset($params[self::CROP_PARAMS])){
            $image=$this->crop->apply($image, $params[self::CROP_PARAMS]);
        }

        if($this->resizeParams){        
            $image=$this->thumbnail->apply($image,$this->resizeParams);
        }

        return $this->generateFileFromImageInterface($image);
    }

    /**
     * @param UploadedFile $f
     * @return ImageInterface
     */
    private function openUploadedFileAsImageInterface(UploadedFile $f)
    {
        return $this->imageLoader($f->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->imageSaver->save($image); 
    }
}

Now I want to see how the method process will behave so I created the following ContainerAwareCommand In order to emulate a file upload:

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->setName('appsuserbundle:create:profile:image')
            ->setDecrtiption("Process a file image like image profile.")
            ->setHelp("This command allows you to generate a file like a process image.")
            ->addArgument('file',InputArgument::REQUIRED,'The image file to process.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $file=$input->getArgument('file');

        /**
         * @var AppUserBundle\Services\ProfileImageGenerator $container
         */
        $imageGenerator=$this->getContainer()->getDefinition('app_user.thumbnail_generator');

        $cropParams=new CropParams(5,5,10,10);
        $file=null;//How To emulate an uploadedfile with realData?
        $processedFile=$imageGenerator->process($file,[ProfileImageGenerator::CROP_PARAMS=>$cropParams])
    }
}

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?

  • 写回答

1条回答 默认 最新

  • matlabmann 2017-07-31 11:25
    关注

    Have you looked at the Symfony Api Documentation ?

    __construct(string $path, string $originalName, string|null $mimeType = null, int|null $size = null, int|null $error = null, bool $test = false)
    

    =>

    new UploadedFile($path, $originalName, $mimeType, $size, $error, $test);
    

    Now you can insert the path you get from the command arguments and pass that through

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B