如何将图像大小保存在数据库中?

Posted

技术标签:

【中文标题】如何将图像大小保存在数据库中?【英文标题】:How can i save Image size in database? 【发布时间】:2019-12-23 15:42:46 【问题描述】:

我需要在数据库中保存大小以在前端查看它,这是我正在使用的控制器。我正在使用 Laravel 5.8

所以我的问题是 Laravel 是否提供任何 Facade 来处理这种情况?或者任何其他框架有更适合问题的能力是什么?

    public function addCourse(Request $request) 
        if ($request -> isMethod('post')) 
            $data = $request -> all();
            $course = new Courses;
            $course -> course_name = $data['course_name'];
            $course -> category_id = $data['course_sector'];
            $course -> course_code = $data['course_code'];
            if (!empty($data['course_description'])) 
                $course -> description = $data['course_description'];
             else 
                $course -> description = "";
            
            $course -> start_date = $data['start_date'];
            $course -> end_date = $data['end_date'];
            $course -> location = $data['course_location'];
            $course -> price = $data['course_price'];
            if ($request -> hasFile('course_image')) 
                $courseImage = Input::file('course_image');
                if ($courseImage -> isValid()) 
                    $extension = $courseImage -> getClientOriginalExtension();
                    $filename = rand(111,99999).'.'.$extension;
                    $large_image_path = "assets/manage_display/images/courses/large/".$filename;
                    $medium_image_path = "assets/manage_display/images/courses/medium/".$filename;
                    $small_image_path = "assets/manage_display/images/courses/small/".$filename;
                    Image::make($courseImage) -> save ($large_image_path);
                    Image::make($courseImage) -> resize (600,null, function ($constraint) 
                        $constraint -> aspectRatio();
                    ) -> save ($medium_image_path);
                    Image::make($courseImage) -> resize (300,null, function ($constraint) 
                        $constraint -> aspectRatio();
                    ) -> save ($small_image_path);
                    $course -> image = $filename;
                
            
            $course -> save();
            return redirect('/control/courses') -> with('flash_message_success', 'New Courses Added Successfully');
        

        $coursesCategories = CoursesCategory::where(['parent_id' => 0]) -> get();
        $coursesCategories_dropdown ="<option selected disabled>Select</option>";
        foreach ($coursesCategories as $coursesCategory) 
            $coursesCategories_dropdown .= "<option class='font-weight-bold' value='".$coursesCategory -> id."'>".$coursesCategory -> name."</option>";
            $subCoursesCategories = CoursesCategory::where(['parent_id' => $coursesCategory -> id]) -> get();
            foreach ($subCoursesCategories as $subCoursesCategory) 
                $coursesCategories_dropdown .= "<option class='blockquote-footer' value='".$subCoursesCategory -> id."'>&nbsp;-&nbsp;".$subCoursesCategory->name."</option>";
            
        

        return view('layouts.manage_layouts.courses.add_course') -> with(compact('coursesCategories_dropdown'));
    

【问题讨论】:

【参考方案1】:

您可以使用getSize()方法获取上传文件的大小,然后将其存储在数据库中:

$request->file('file')->getSize();

【讨论】:

请问如何在我的代码中使用它?你能复制我的代码包括这个请求吗?

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

如何将通过summernote生成的大型base64图像保存在我的数据库中?

如何在使用 gridfs 保存之前调整大小和图像?

matlab如何让调整保存图像的大小

检索、调整大小并将图像保存回 Access 数据库图像字段

如何以两种不同的大小从图像选择器中保存图像

如何保存调整大小的图像?