调整上传文件的大小并在同一位置以相同的名称保存

问题描述 投票:0回答:2

我尝试保存图像并使用干预调整大小。 Filesystem.php 有 Laravel 10 的默认设置。

我可以成功保存在storage/app/public/image下:

$file->storeAs('public/image/', $uniqueFileName);

比我尝试调整大小并使用相同的名称保存。

$manager = new ImageManager();
$resizedImage = $manager->make(storage_path('app/public/image/' . $uniqueFileName))->resize(100, 100);
Storage::disk('public')->put(storage_path('public/image/' . $uniqueFileName), $resizedImage);

问题是,最后一行创建了这样一个文件夹并尝试保存在此位置下:

/storage/app/public/Users/tolga/Desktop/project-path/storage/image

此位置下调整大小的图像实际上是空文件。

php laravel intervention
2个回答
1
投票

如果您使用

storage_path
,则不应使用
Storage facade
生成路径,因为
storage_path
将生成绝对路径,但
Storage facade
disk local or public
已经知道存储路径。所以就这样写吧

  Storage::disk('public')->put('relative path in storage', $file);

0
投票

正如 Aro 提到的,我从 Storage:put 方法中删除了 storage_path,但后来我意识到,通过干预,我实际上不需要这一行。

我首先添加为别名:

'aliases' => Facade::defaultAliases()->merge([
    'Image' => Intervention\Image\ImageManagerStatic::class,

比,作为单行代码:

use Image;

...

Image::make(storage_path('app/public/image/' . $uniqueFileName))->fit(200, 200)->save();

这会调整大小为正方形,即使图像文件不是正方形,并且会以相同的名称保存。

© www.soinside.com 2019 - 2024. All rights reserved.