【问题标题】:Saving Images in two size in Laravel Livewire在 Laravel Livewire 中以两种尺寸保存图像
【发布时间】:2022-04-07 13:16:13
【问题描述】:

我有一个使用 Laravel Livewire 上传图片的表单。

在我的课堂上,我可以使用以下方法将文件保存为原始大小:

$filenames = collect($this->photos)->map->store('posts');

我面临的问题是我不知道如何将相同的图像(与上述相同的名称)保存在另一个文件夹(缩略图文件夹)中,以另一种尺寸。例如,宽度为 200px。

【问题讨论】:

标签: laravel laravel-livewire intervention


【解决方案1】:

我已经将它与 Laravel 8 一起使用,它可以工作。在这段代码之前我看不到你有什么,但你可以遍历你的照片并保存 2 张图像,一张在父文件夹中,一张在“缩略图”文件夹中调整大小;

$ext = $image->getClientOriginalExtension();
$date = new Carbon;
$folder = "photos";

// Save original file
$orig_filename = $date->format('YmdHisv') . '.' . $ext;
$img_url = $image->storeAs('public/' . $folder, $orig_filename);

// Create and store thumbnail
$thumbnail = $image->storeAs('public/' . $folder . '/thumbnails', $orig_filename);
$thumb_path = Storage::path('public/' . $folder . '/thumbnails/' . $orig_filename);
$thumb_img = Image::make($thumb_path)->resize(150, 93, function ($constraint) {
        $constraint->aspectRatio();
});
$thumb_img->save($path);

这是包Intervention\Imagehere 是一个很好的教程。

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2013-02-05
    • 2013-05-15
    相关资源
    最近更新 更多