Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

How to Store and Show image fields with file type, file size, and with dimensions?

In laravel, how can I store the images with file type, file size, dimension and also with file URL. can someone help me, how can I get these field. THis is my store function for image

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

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();
        $originalname = $image->getClientOriginalName();
        $path = $image->move('uploads\media', $originalname);
        $mimetype = $image->getClientMimeType();

        $picture = new mediaLibrary();
        $picture->mime = $mimetype;
        $picture->original_filename = $originalname;
        $picture->filename = $path;
        $picture->save();

        return redirect()->route('media.index');
    }
0 likes
4 replies
Neeraj1005's avatar

Is there no other method for image information?

tisuchi's avatar

@neeraj1005

Actually @tray2 suggested the link to make your life easier. The intervention is a well-known image manipulation package in PHP.

Of course, there are other way around where you need to do a lot. You can check it in the php documentation.

This is a sample function. There are more functions you can search for based on your requirements. https://www.php.net/manual/en/function.getimagesize.php

1 like
Neeraj1005's avatar
Neeraj1005
OP
Best Answer
Level 9
 public function show($id)
    {
        $data = mediaLibrary::findOrFail($id);

        return view('mediaLibrary.show',compact('data'));
    }


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');
    }
1 like

Please or to participate in this conversation.