在 Laravel6.x 中获取文件(图像)尺寸



在我的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>

你可以使用 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();

控制器映像存储函数

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://i.stack.imgur.com/o506N.jpg

相关内容

  • 没有找到相关文章

最新更新