Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony Php:模拟文件上载并创建上载文件_Php_Symfony - Fatal编程技术网

Symfony Php:模拟文件上载并创建上载文件

Symfony Php:模拟文件上载并创建上载文件,php,symfony,Php,Symfony,在Symfony命令行ContainerWareCommand上,我希望模拟文件上载,以便调用以下方法: namespace AppUserBundle\Services; use PcMagas\AppImageBundle\Filters\Crop\CropFilter; use Symfony\Component\HttpFoundation\File\UploadedFile; use Imagine\Image\ImageInterface; use PcMagas\AppImage

在Symfony命令行ContainerWareCommand上,我希望模拟文件上载,以便调用以下方法:

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); 
    }
}
现在,我想看看方法
过程
将如何运行,因此我创建了以下
ContainerWareCommand
以模拟文件上载:

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])
    }
}
但是我一直在研究如何从文件系统映像创建一个上传的文件,以便查看映像是否有效。您对如何实现这一点有什么想法吗?

您看过了吗

=>

现在,您可以插入从命令参数获得的路径并将其传递给用户

__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);