Reputation: 1407
In my laravel project, I have uploaded the image in database and it shows image in view file properly. Now I want to get the image dimension, So How can I get this? Can anyone help me plz? This is my controller for uploading image.
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');
}
This is my blade file code where i want to show
<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>
Upvotes: 0
Views: 266
Reputation: 1407
Controller Image Store function
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');
}
And view the file
<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>
Note this is the database table fields: https://i.sstatic.net/o506N.jpg
Upvotes: 0
Reputation: 4813
You may use getimagesize() native PHP method
list($width, $height) = getimagesize('path_to_image');
Or via
Intervention Image package
// read width of image
$width = Image::make('public/foo.jpg')->width();
// read height of image
$height = Image::make('public/foo.jpg')->height();
Upvotes: 2