如何在 Laravel 中调整图像大小并同时将数据保存在数据库中

Posted

技术标签:

【中文标题】如何在 Laravel 中调整图像大小并同时将数据保存在数据库中【英文标题】:how to resize image in Laravel and save data in database at the same time 【发布时间】:2021-03-16 12:42:23 【问题描述】:

我正在尝试将数据(标题、描述和调整大小的图像)传递到我的数据库

这是存储功能,现在它将我的图像保存在该路径中,但不会将它们保存在数据库中

如果我删除调整大小的图像,商店功能可以工作。

    public function store(Request $request)

    $request->validate([
        'title' => 'required',
        'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:10240',
        'description' => 'required',
    ]);

    $path = $request->file('image');
    $filename = $path->getClientOriginalName();
    $image_resize = Image::make($path->getRealPath()); 
    $image_resize->resize(800, 800);             
    $image_resize->save(public_path('images/' .$filename));

    $post = new Post;

    $post->title = $request->title;
    $post->description = $request->description;
    $post->image = $image_resize;

    $post->save();

 
    return redirect()->route('posts.index')
                    ->with('success','Post has been created successfully.');

enter image description here enter image description here

这是新的商店功能

public function store(Request $request)

    $request->validate([
        'title' => 'required',
        'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:10240',
        'description' => 'required',
    ]);

    $path = $request->file('image');
    $filename = $path->getClientOriginalName();
    $image_resize = Image::make($path->getRealPath()); 
    $image_resize->resize(800, 800);             
    $image_resize->save(public_path('images/' .$filename));

    $post = new Post;

    $post->title = $request->title;
    $post->description = $request->description;
    $post->image = 'images/' .$filename;

    $post->save();

 
    return redirect()->route('posts.index')
                    ->with('success','Post has been created successfully.');

这是刀片代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Laravel 8 CRUD Tutorial</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>

<div class="container mt-2">

<div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Laravel 8 Post CRUD Tutorial</h2>
            </div>
            <div class="pull-right mb-2">
                <a class="btn btn-success" href=" route('posts.create') "> Create New Post</a>
            </div>
        </div>
    </div>
   
    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p> $message </p>
        </div>
    @endif
   
    <table class="table table-bordered">
        <tr>
            <th>S.No</th>
            <th>Image</th>
            <th>Title</th>
            <th>Description</th>
            <th >Action</th>
        </tr>
        @foreach ($posts as $post)
        <tr>
            <td> $post->id </td>
            <td><img src=" Storage::url($post->image) "    /></td>
            <td> $post->title </td>
            <td> $post->description </td>
            <td>
                <form action=" route('posts.destroy',$post->id) " method="POST">
    
                    <a class="btn btn-primary" href=" route('posts.edit',$post->id) ">Edit</a>
   
                    @csrf
                    @method('DELETE')
      
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
  
    !! $posts->links() !!

</body>
</html>

【问题讨论】:

数据库中的“图像”列是哪种类型? 【参考方案1】:

将文件名放入mysql而不是图像对象!!!!!!

$post->image = "images/$filename";

【讨论】:

数据库中的图片类型是VARCHAR

以上是关于如何在 Laravel 中调整图像大小并同时将数据保存在数据库中的主要内容,如果未能解决你的问题,请参考以下文章

如何在使用安全资产功能时将图像从数据库传递到 laravel 中查看

将图像上传到 Firebase 存储并同时将文本输入到 Firestore 时出错

向下滑动搜索栏并同时调整同级视图的大小

单击时将图像添加到屏幕并允许其可拖动/可旋转

如何使用 jQuery 在父 div 中单击时将子 div 的大小调整为单击点或区域?

仅作为 UITabBarItem 的图像