Symfony Php:模拟FileUpload并创建UploadedFile

时间:2017-07-31 10:47:46

标签: php symfony

在Symfony Commandline上使用ContainerAwareCommand我想模拟文件上传,以便调用以下方法:

getch

现在我想了解方法getche的行为方式,以便创建以下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); } } 以模拟文件上传:

process

但我仍然坚持如何从文件系统映像创建一个Uploadedfile,以便查看图像是否有效?你是否知道如何做到这一点?

1 个答案:

答案 0 :(得分:3)

你看过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);

现在,您可以插入从命令参数获取的路径并通过

传递