使用Laravel干预图像上传到S3返回布尔值,而不是路径



我有一个问题在Laravel中上传图像。有时,图像需要重新定位 - 使用干预图像进行此操作时,它只是返回布尔值,而不是S3中图像的路径。在没有干预图像的情况下使用它时,它可以正常工作

我尝试了EXIF读取数据并使用imagerotate无用,它错误

我运行以下

$image = $request->file('photo');
$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image, 'public');
dd($path); // /users/1/posts/39grjigrje.jpg

$path变量很棒,是S3路径,但是运行:

$image = $request->file('photo');
$image = Image::make($image);
$image->orientate();
$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image, 'public');
dd($path); // true

$path变量只是一个布尔值,不返回存储的文件路径

我期望一条路径,例如我不使用干预图像时会得到的/images/1/kfjeieuge.jpg,当我使用干预时,我会得到布尔。

几年前,我也遇到了同样的问题。我通过使用以下步骤解决了这一点:

1.在干预操作后,您可能必须编码图像。要实现编码,请在流方法上参考此文档:创建编码图像流

以给定格式编码当前图像,并给定图像质量,并根据图像数据创建新的PSR-7流。

$image = Image::make(request()->file('image'))->orientate()->encode('jpg');

2.然后用流储存

$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image->stream(),'public');

这应该实现您的目标。

在第一个示例中:

$image = $request->file('photo');
$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image, 
'public');

$request->file('photo')文件将返回IlluminateHttpUploadedFile类的实例。您可以在文档链接中检查此处。

和tarun所说的put方法检查例如IlluminateHttpUploadedFile类。因此,在这种情况下,它将成功上传。

在第二个示例中:

$image = $request->file('photo');
$image = Image::make($image); //here
$image->orientate();
$path = Storage::disk('s3')->put('users/' . Auth::id() . '/posts', $image, 'public');

您正在用实例Image类(第二行(覆盖$image变量。这是错误的。您需要传递流或文件。

尝试以下代码:

$image = $request->file('photo');
$image = Image::make($image); //here
$image->orientate()->encode('jpg');
$filename = time() . '.jpg';
$path = Storage::disk('s3')->put('users/' . Auth::id() . '/posts/'.$filename, $image->getEncoded(), 'public');

您可以按照以下链接

中描述的

https://laracasts.com/discuss/channels/laravel/saving-an-intervention-image-image-instance-instance-into-into-amazon-s3

$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image->stream()->__toString(), 'public');

更新:5 Jul

如果您查看源代码,则应该仅返回bool

/**
     * Write the contents of a file.
     *
     * @param  string  $path
     * @param  string|resource  $contents
     * @param  mixed  $options
     * @return bool
     */
    public function put($path, $contents, $options = [])
    {
        $options = is_string($options)
                     ? ['visibility' => $options]
                     : (array) $options;
        // If the given contents is actually a file or uploaded file instance than we will
        // automatically store the file using a stream. This provides a convenient path
        // for the developer to store streams without managing them manually in code.
        if ($contents instanceof File ||
            $contents instanceof UploadedFile) {
            return $this->putFile($path, $contents, $options);
        }
        return is_resource($contents)
                ? $this->driver->putStream($path, $contents, $options)
                : $this->driver->put($path, $contents, $options);
    }

,您可以看到Streamfile的处理方式不同

,理想的方法是使用url功能获取URL

Storage::disk('s3')->url($filename)

尝试此

$image = Image::make($request->file('photo')->getRealpath());
$image->orientate();

$image->encode('jpg');
 OR
$image->response('jpg', 100);

$path = Storage::disk('s3')->put('users/'.Auth::id().'/posts', $image, 'public');

最新更新