【问题标题】:Resizing uploaded image in Laravel and store in S3 is not working在 Laravel 中调整上传图像的大小并在 S3 中存储不起作用
【发布时间】:2016-01-05 04:47:33
【问题描述】:

我正在尝试将几个调整大小的图像上传到 S3,但不知何故,所有图像的大小都相同。以不同的大小在本地存储它们没有问题。我错过了什么?

public function uploadFileToS3(Request $request) {
    $image = Image::make($request->file('image'))->encode('jpg', 75);
    $s3 = Storage::disk('s3');

    $image_file_name = $this->generateName($request->name) . '.jpg';
    $file_path = '/' . config('folder') . '/' . $request->name . '/';

    $s3->put($file_path.'original_'.$image_file_name, $image, 'public');
    $s3->put($file_path.'medium_'.$image_file_name, $image->fit(300, 300), 'public');
    $s3->put($file_path.'thumb_'.$image_file_name, $image->fit(100, 100), 'public');

    return json_encode(array(
        'filename' => $image_file_name
    ));
}

所有版本都成功存储在 S3 中,只是大小相同

【问题讨论】:

    标签: php laravel amazon-s3 laravel-5


    【解决方案1】:

    我有两种可能的解决方案。

    在尝试存储之前尝试完全处理图像:

    $s3->put($file_path.'original_'.$image_file_name, $image, 'public');
    $image->fit(300, 300);
    $s3->put($file_path.'medium_'.$image_file_name, $image, 'public');
    $image->fit(100, 100);
    $s3->put($file_path.'thumb_'.$image_file_name, $image, 'public');
    

    尝试将图像转换为字符串,实际输出文件内容应该可以正常工作:

    $s3->put($file_path.'original_'.$image_file_name, (string) $image, 'public');
    

    【讨论】:

      【解决方案2】:

      ...我设法通过首先将调整大小的图像存储到本地 tmp 文件夹然后上传它来解决这个问题。这是一个我非常不喜欢的解决方法,因为在我看来,对于应该在 Laravel 中处理的事情来说,它的编码太多了。

          $folder = $_SERVER['DOCUMENT_ROOT'] . '/tmp/';
          $image_file_name = $this->generateName($request->name) . '.jpg';
      
          // save original
          $image->save($folder . 'original_' . $image_file_name);
          $s3->put($file_path.'original_'.$image_file_name, fopen($folder.'original_'.$image_file_name, 'r+'), 'public');
      
          // generate thumb
          $image = $image->fit(100, 100);
          $image->save($folder . 'thumb_' . $image_file_name);
          $s3->put($file_path.'thumb_'.$image_file_name, fopen($folder.'thumb_'.$image_file_name, 'r+'), 'public');
      

      【讨论】:

        猜你喜欢
        • 2015-04-08
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 2021-04-10
        • 2013-01-12
        • 1970-01-01
        • 2018-02-11
        • 2015-05-19
        相关资源
        最近更新 更多