How to Store and Retrieve Image Contents from the Database Using Laravel

How to store and retrieve image contents from the database using Laravel

Actually with Laravel it only involves a few lines of code. Let's say you have a user that has an avatar which is stored in the database. Assuming you're using MySQL, here's how you would store and retrieve the avatar from the database:

1. First you'll need to have an avatar column in the users table that can store binary data. Depending on how large you want to allow the avatar image to be, the data type of the column can be one of the following:

BLOB up to 64KB

MEDIUMBLOB up to 16MB

LONGBLOB up to 4GB

2. To store the uploaded image in the database you can do this:

Route::post('user/{id}', function (Request $request, $id) {
// Get the file from the request
$file = $request->file('image');

// Get the contents of the file
$contents = $file->openFile()->fread($file->getSize());

// Store the contents to the database
$user = App\User::find($id);
$user->avatar = $contents;
$user->save();
});

3. To fetch and ouput the avatar you can do the following:

Route::get('user/{id}/avatar', function ($id) {
// Find the user
$user = App\User::find(1);

// Return the image in the response with the correct MIME type
return response()->make($user->avatar, 200, array(
'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($user->avatar)
));
});

So to access the avatar of the user with an id equal to 10 you can just use this URL:

http://domain.com/user/10/avatar

How can I store the images as Blob file in database using laravel 8?

This might do the job.

$path = $request->file('file')->getRealPath();    
$logo = file_get_contents($path);
$base64 = base64_encode($logo);
$account->logo = $base64;
$account->save();

Source

Laravel: Storing image custom name to db

You need to create a table in your database for storing the images. In your table, you basically need an ID and the name of the image file in your storage, in your case called $newImageName. You can do this by migration as below:

Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('filename');
$table->timestamps();
});
  • Tip: the timestamp store your image creation time so you wouldn't need to store it in your filename. Instead, later after the creation of the item, you can retrieve the ID generated by the new Object to use for your filename in order to prevent overwriting. This is a bit more secure approach as there is a nuance possibility of simultaneous uploads with the same name.

In your controller whenever you are uploading a new item, do accordingly:

$image = new Image();
$image->filename = $newImageName;
$image->save();

This will save the Image filename in your DB for further usage.

  • Tip: If you want to go further on creating a more structured design, you can go ahead and create a Model for your Image and attach them to your other Models, in case of retrieving them in an easier way.

How to Store Image in Database Using Laravel With Base URL

Try this ....

 public function uploadimage(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$employees = new Image($request->input()) ;

if($file = $request->hasFile('image')) {
$file = $request->file('image') ;
$fileName = $file->getClientOriginalName() ;
$destinationPath = public_path().'/images/' ;
$file->move($destinationPath,$fileName);
$employees->image = '/public/images/'.$fileName ;
}
$employees->save() ;
return response()->json(['message' => 'Image Uploaded Successfully']);
}

Laravel - Store uploaded image path in database,retrieve and display the image

Below is the steps I did regarding upload and retrieve user related images.

For the example, a user upload his profile picture which named myfile.png

So, for upload:

  1. Get the file from $request
  2. Get the file name with getClientOriginalName() (i.e. myfile) and also the file extension with getClientOriginalExtension() (i.e. .png)
  3. Generate some random strings which will be appended to the file name to avoid file name duplication (i.e. fx7ew6)
  4. Append the original file name with the generated random strings along with the extension (i.e. myfile_fx7ew6.png)
  5. Save image file to folder (i.e. public/img) and also save the file name to the database

For retrieve:

You can use <img src="/img/{{FILE_NAME}}"> to show the uploaded image which will result <img src="/img/myfile_fx7ew6.png"> IF you upload the image to public/img.



Related Topics



Leave a reply



Submit