【问题标题】:manually creating an Symfony UploadedFile手动创建 Symfony UploadedFile
【发布时间】:2015-11-11 16:57:25
【问题描述】:

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

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

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

这可能吗?

提前谢谢你

【问题讨论】:

    标签: php symfony laravel-5


    【解决方案1】:

    我想我找到了...仍然很好奇是否可以进行改进..

    $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
    }
    

    【讨论】:

      【解决方案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
      

      希望对您有所帮助!

      【讨论】:

      【解决方案3】:

      根据 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】:

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

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

        【讨论】:

          【解决方案5】:

          这是在 Symfony 4 Fixtures 中使用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
              }
          }
          

          【讨论】:

            【解决方案6】:

            如果这很重要,这就是我在 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 的方法。希望这会有所帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-12-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-22
              • 1970-01-01
              • 2014-12-16
              相关资源
              最近更新 更多