Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

I have to store and retrieve an image from the database. I want to store an image instead of image path or image name. When I searched for it I only found solutions for storing an image path or name. So can someone tell me how to store and retrieve an image with its size in KBs from database?

This is my route.

Route::get('big_image/{id}/image', function ($id)
{
   $user = big_image::find($id);
   return response()->make($user->image, 200, array(
   'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($user->image)
));

});

This is my controller

 public  function new_store(Request $request ,$id){
    $event_id = $id;
    $img = new big_image;
    $file =  $request->file('image');
    $contents = $file->openFile()->fread($file->getSize());
    $img = big_image::find($id);
    $img->image = $contents;
    $img->image_name = $request->image_name;
    $img->event_id = $event_id;
    $img->save();
  $images=DB::table('big_image')->where('event_id','=',$event_id)->get();
  return view('image_upload',compact('images','id','response'));
}

My display.blade.php file

@extends('app')
@section('content')
  <h2 style="text-align: center;margin-top: 10px;">Image Gallery</h2>
  <a href="{{url('pic_upload/'.$id.'')}}" class="col-sm-12"             style="padding-left: 0;"><span> add more pictures</span></a><br>
  <a href="{{url('events')}}"><span>Back to events</span></a><br>
   @foreach($images as $item)
    <div class="col-sm-4">
    {{--<img src="data:image/jpeg;base64,'.base64_encode( $item->image  ).'"/>;--}}
    </div>
@endforeach
@stop
like image 565
Shweta Avatar asked Feb 16 '16 09:02

Shweta


People also ask

How to upload image to database&storage in Laravel?

Laravel 8 Upload Image to Database & Storage Tutorial 1 Download Laravel 8 Application 2 Setup Database with App 3 Create Model & Migration 4 Create Routes 5 Create Controller By Artisan Command 6 Create Blade View 7 Create Images Directory inside Storage/app/public 8 Run Development Server

How to store image file in database in PHP?

Store Image File in Database (upload.php) 1 Check whether the user selects an image file to upload. 2 Retrieve the content of image file by the tmp_name using PHP file_get_contents () function. 3 Insert the binary content of the image in the database using PHP and MySQL. 4 Show the image uploading status to the user. More ...

How do I retrieve an avatar in 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.

How to make a file public in Laravel?

If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.


1 Answers

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
like image 51
Bogdan Avatar answered Oct 18 '22 00:10

Bogdan