手动创建Symfony UploadedFile

时间:2015-08-18 16:25:51

标签: php symfony laravel-5

我面临以下问题,似乎无法解决这个问题。 我写了一个API端点,接受带有二进制数据的POST(header:content-type:image / jpeg)。

我知道我可以用file_get_content('php://input')或Laravel' $request->getContent()读出原始字符串。 PHP还有一个函数createimagefromstring($string),它似乎也正确地读取了字符串。

我想要做的是从这个原始数据创建一个UploadedFile,这样我就可以用已编写的函数来处理它。

这可能吗?

提前谢谢

6 个答案:

答案 0 :(得分:8)

我想我发现了......仍然很好奇是否有可以改进的地方......

$imgRaw = imagecreatefromstring( $request->getContent() );
if ($imgRaw !== false) {
    imagejpeg($imgRaw, storage_path().'/tmp/tmp.jpg',100);
    imagedestroy($imgRaw);
    $file =  new UploadedFile( storage_path().'/tmp/tmp.jpg', 'tmp.jpg', 'image/jpeg',null,null,true);
    // DO STUFF WITH THE UploadedFile
}

答案 1 :(得分:2)

您可以尝试使用base64编码。 Symfony有一些很好的东西。

你的代码会像这样生成:

$base64Content = $request->request->get('base64Content'); // this is your post data
$yourFile = new UploadedBase64EncodedFile(new Base64EncodedFile($base64Content)); // this is an instance of UploadedFile

希望它有所帮助!

答案 2 :(得分:1)

无需手动创建它,Symfony为您解析收到的$ _FILES数组。 Http Request对象有一个名为$ files的FileBag属性,带有一个返回UploadedFile实例的get()方法。

/** @var UploadedFile $file */
$file = $request->files->get('user-pictures-upload')[0];
$cmd = new UploadPictureCmd($file, $this->getUser()->getId());

答案 3 :(得分:1)

根据Laravel 8

只需遵循构造函数:

     * @param string      $path         The full temporary path to the file
     * @param string      $originalName The original file name of the uploaded file
     * @param string|null $mimeType     The type of the file as provided by PHP; null defaults to application/octet-stream
     * @param int|null    $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
     * @param bool        $test         Whether the test mode is active

    $file = new UploadedFile(
        $pathIncludingFilename, 
        $fileName, 
        'image/jpeg', 
        null, 
        false
    );
 

答案 4 :(得分:0)

如果它有任何意义,这就是我在Laravel 5.4中的表现。就我而言,我希望能够轻松调整图像大小,并能够做到这样的事情。

request()->file('image')->resize(250, 250)->store('path/to/storage');

这就是我对UploadedFile类所做的。

Illuminate \ Http \ UploadedFile.php 〜此文件附带Laravel框架

public function resize($w, $h) {

$image = Intervention::make($this)->fit($w, $h)->save($this->getPathname());
$filesize = filesize($this->getPathname());

    return new static(
        $this->getPathname(),
        $this->getClientOriginalName(),
        $this->getClientMimeType(),
        $filesize,
        null,
        false
    );
}

使用Intervention,我在上传文件时调整了存储在/ tmp /文件夹中的图像的大小,然后将其保存在同一个位置。现在我所做的就是创建一个UploadedFile实例,以便我可以继续在request()->file('image')上使用Laravel的方法。希望这会有所帮助。

答案 5 :(得分:0)

以下是在Symfony 4灯具中使用fzaninotto/faker生成图像文件的示例

class FileFixtures extends Fixture
{
    private $faker;
    private $parameterBag;

    public function __construct(ParameterBagInterface $parameterBag)
    {
        $this->faker = Factory::create();
        $this->parameterBag = $parameterBag;
    }

    public function load(ObjectManager $manager)
    {            
        $tempFixturesPath = $this->parameterBag->get('kernel.project_dir') . '/tmp';
        if (!file_exists($tempFixturesPath)) {
            mkdir($tempFixturesPath);
        }
        $fileName = $this->faker->image($tempFixturesPath, 640, 480, 'cats', false, true);
        $file = new UploadedFile($tempFixturesPath . '/' . $fileName, $fileName, 'image/jpeg', null, null, true);
        //do something with $file
    }
}