0

在我的 laravel 项目中,我已将图像上传到数据库中,并在视图文件中正确显示图像。现在我想获得图像尺寸,那么我该如何获得呢?有人可以帮我吗?这是我上传图片的控制器。

public function store(Request $request)
    {
        request()->validate([
            'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        // if ($request->hasFile('image')) {
        //     $image = $request->file('image');
        //     $extension = $image->getClientOriginalExtension();
        //     $filename = time() . "." . $extension;
        //     $image->move('public/mediaLibrary', $filename);

        // } 

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();//Getting extension
        $originalname = $image->getClientOriginalName();//Getting original name
        //this code will store image in laravel default storage folder $path = $image->storeAs('', $originalname);
        $path = $image->move('uploads/media/', $originalname);//This will store in customize folder
        $imgsizes = $path->getSize();
        $mimetype = $image->getClientMimeType();//Get MIME type

//Start Store in Database
        $picture = new mediaLibrary();
        $picture->mime = $mimetype;
        $picture->imgsize = $imgsizes;
        $picture->original_filename = $originalname;
        $picture->extension = $extension;
        $picture->filename = $path;       
        $picture->save();
//End Store
        return redirect()->route('media.index');
    }

这是我要显示的刀片文件代码

 <div class="card-body" >
                                <span><i class="fa fa-calendar" aria-hidden="true"></i> Uploaded on:</span>
                                <strong><p class="text-muted" style="display: inline">
                                  {{ date('F d, Y',strtotime($data->created_at)) }} at {{ date('g : ia',strtotime($data->created_at)) }}
                                </p></strong>
                                <hr>
                                <span>File Name:</span>
                                <strong><p style="display: inline;">{{$data->original_filename}}</p></strong> 
                                <hr>
                                <span>File type:</span>
                                <strong><p style="display: inline;">{{$data->extension}}</p></strong>
                                <hr>
                                <span>Fle Size:</span>
                                <strong><p style="display: inline;"></p>{{round(($data->imgsize)/1024 )}}KB</strong>
                                {{-- <strong><p style="display: inline;"></p>{{(File::size($data->filename))/1024}} KB</strong> --}}
                                <hr>
                                <span>Dimension:</span>
                                <strong><p style="display: inline;">

                                </p></strong>
</div>
4

2 回答 2

1

您可以使用 getimagesize () 原生 PHP 方法

list($width, $height) = getimagesize('path_to_image');

或通过

干预图像

// read width of image
$width = Image::make('public/foo.jpg')->width();

// read height of image
$height = Image::make('public/foo.jpg')->height();
于 2020-01-20T07:18:51.267 回答
0

控制器图像存储功能

 public function store(Request $request)
    {
        request()->validate([
            'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();//Getting extension
        $originalname = $image->getClientOriginalName();//Getting original name
        $path = $image->move('uploads/media/', $originalname);//This will store in customize folder
        $imgsizes = $path->getSize();
        $data = getimagesize($path);
        $width = $data[0]; 
        $height = $data[1];
        $mimetype = $image->getClientMimeType();//Get MIME type

//Start Store in Database
        $picture = new mediaLibrary();
        $picture->mime = $mimetype;
        $picture->imgsize = $imgsizes;
        $picture->original_filename = $originalname;
        $picture->extension = $extension;
        $picture->width = $width;
        $picture->height = $height;
        $picture->filename = $path;     
        $picture->save();
//End Store
        return redirect()->route('media.index');
    }

并查看文件

<div class="card-body" >
                                <span><i class="fa fa-calendar" aria-hidden="true"></i> Uploaded on:</span>
                                <strong><p class="text-muted" style="display: inline">
                                  {{ date('F d, Y',strtotime($data->created_at)) }} at {{ date('g : ia',strtotime($data->created_at)) }}
                                </p></strong>
                                <hr>
                                <span>File Name:</span>
                                <strong><p style="display: inline;">{{$data->original_filename}}</p></strong> 
                                <hr>
                                <span>File type:</span>
                                <strong><p style="display: inline;">{{$data->extension}}</p></strong>
                                <hr>
                                <span>Fle Size:</span>
                                <strong><p style="display: inline;"></p>{{round(($data->imgsize)/1024 )}}KB</strong>
                                {{-- <strong><p style="display: inline;"></p>{{(File::size($data->filename))/1024}} KB</strong> --}}
                                <hr>
                                <span>Dimension:</span>
                                <strong><p style="display: inline;">
                                    {{$data->width}} <i class="fa fa-times" aria-hidden="true"></i> {{$data->height}}
                                </p></strong>
                                <hr>
                                <span>File URL:</span>
                                <input type="text" class="mediaLib" name="image_url" value="{{url('uploads/media',$data->original_filename)}}" readonly="" style="
                                display: block; 
                                background-color: #eee;
                                padding: 0 8px;
                                line-height: 2;
                                border-radius: 4px;
                                border: 1px solid #7e8993;
                                color: #32373c;
                                border-spacing: 0;
                                width:-webkit-fill-available;">
                              </div>

注意这是数据库表字段: https ://imgur.com/O1w8ocT

于 2020-01-20T09:28:06.537 回答