Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Why not just save the path of the image?

$pathToFile = '/foo/bar/baz.jpg';

// resize image
Image::make(Input::file('files')->getRealPath())
    ->resize(870, null, true, false)
    ->save($pathToFile);

// save image to database
$user->image = $pathToFile;
$user->save();
Last updated 1 year ago.
0

Tuurbo said:

Why not just save the path of the image?

$pathToFile = '/foo/bar/baz.jpg';

// resize image
Image::make(Input::file('files')->getRealPath())
   ->resize(870, null, true, false)
   ->save($pathToFile);

// save image to database
$user->image = $pathToFile;
$user->save();

this should answer that

Last updated 1 year ago.
0

No, i really need to save the image in database, not on disk.

Last updated 1 year ago.
0

Hey mabasic - if you really must save your image data into a database/datastore you could do this:

$img_data = file_get_contents($img_path);
$type = pathinfo($img_path, PATHINFO_EXTENSION);
$base64 = $base64_encode($img_data);

// save $base64 & $type into your datastore
...

You could then use the Data URI string in the view if you want:

'data:image/' . $your_image_model->type . ';base64,' . $your_image_model->base64

Does that seem right?

Last updated 1 year ago.
0

@mabasic: I think something like this should work:

$user->image = $big_image->encode('jpg', 80);
$user->save();

Data type in the DB must be some sort of binary (e.g. MySQL BLOB type).

Last updated 1 year ago.
0

mabasic said:

No, i really need to save the image in database, not on disk.

:D unless your db is in memory it's also on disk

Last updated 1 year ago.
0

popolla said:

@mabasic: I think something like this should work:

$user->image = $big_image->encode('jpg', 80);
$user->save();

Data type in the DB must be some sort of binary (e.g. MySQL BLOB type).

I like this approach, but how do you display that image in a view ?

Last updated 1 year ago.
0

zenry said:

mabasic said:

No, i really need to save the image in database, not on disk.

:D unless your db is in memory it's also on disk

I know, but you get what I meant to say.

Last updated 1 year ago.
0

If you are wondering how to perform the base64() as I mentioned above, you can use:

// encode image as data-url
$data = Image::make('public/bar.png')->encode('data-url');

See: http://intervention.olivervogel.net/image/methods/encode

It's easy to use this in the view if you are wanting to working with data-url images.

Or if you want to reference the images by a URL (no matter how you encode them) you can write a proxy route that will set the headers to match your image mime type e.g if you want to do this:

<img src="/my-image.jpg">

Then you would need to do something like:

<img src="/image-proxy/my-image">

This would then retrieve the image details from the database and you would then set the headers using for serving an image. I found this image proxy for laravel with a quick Google search: https://github.com/spescina/timthumb

Last updated 1 year ago.
0

jimhill said:

If you are wondering how to perform the base64() as I mentioned above, you can use:

// encode image as data-url $data = Image::make('public/bar.png')->encode('data-url');

See: http://intervention.olivervogel.net/image/methods/encode

It's easy to use this in the view if you are wanting to working with data-url images.

Or if you want to reference the images by a URL (no matter how you encode them) you can write a proxy route that will set the headers to match your image mime type e.g if you want to do this:

Then you would need to do something like:

This would then retrieve the image details from the database and you would then set the headers using for serving an image. I found this image proxy for laravel with a quick Google search: https://github.com/spescina/timthumb

Hi, i ended up using this

$user->image = $big_image->encode('data-url');

And now to fetch the image from database I am trying with

<img src="{{ $user->image }}" />

But with no luck.

Last updated 1 year ago.
0

Please elaborate "no luck".
Are you using MySQL? If so, are you using BLOB or CLOB (TEXT)?

Last updated 1 year ago.
0

popolla said:

Please elaborate "no luck".
Are you using MySQL? If so, are you using BLOB or CLOB (TEXT)?

I am using MySQL, and field type is binary(blob). No luck means that i get the correct string in img src, but the image does not show.

Should I use TEXT instead of BLOB ?

Last updated 1 year ago.
0

look at @jimhill last code block

Last updated 1 year ago.
0

zenry said:

look at @jimhill last code block

Please, don't reply to this topic again if you have nothing constructive to say. I read every post multiple times.

Last updated 1 year ago.
0

Try storing directly as binary (e.g. jpg) then retrieving with a route/controller.
A very basic example:

Route::get('user/{id}/image', function($id)
{
    $user = User::find($id);
    $response = Response::make($user->image, 200);
    $response->header('Content-Type', 'image/jpeg');
    return $response;
});
Last updated 1 year ago.
0

popolla said:

Try storing directly as binary (e.g. jpg) then retrieving with a route/controller.
A very basic example:

Route::get('user/{id}/image', function($id)
{
   $user = User::find($id);
   $response = Response::make($user->image, 200);
   $response->header('Content-Type', 'image/jpeg');
   return $response;
});

I have done that, but I see only 50% of the image loaded. The rest is blank.

Last updated 1 year ago.
0

mabasic said:

zenry said:

look at @jimhill last code block

Please, don't reply to this topic again if you have nothing constructive to say. I read every post multiple times.

I don't think zenry was being unconstructive at all. What he was saying is look at this line:

'data:image/' . $your_image_model->type . ';base64,' . $your_image_model->base64

The output you need from your HTML should be a data URI. If you have never used them before then take a look at http://css-tricks.com/data-uris/

Your final rendered HTML should look somethin like:

<img width="16" height="16" alt="star" src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" />

I would personally prefer to use the proxy method (actually I would rather use files in a CDN rather than storing in a database due to overhead on large traffic sites).

How large are the images that you are storing in file size and have you also tried opening them in your browser directly before uploading via your app?

I ask this as I have seen a problem many times where people have been trying to debug the code when the image file itself is corrupt. So always good to debug from the start of the process.

Last updated 1 year ago.
0

@mabasic: maybe your image is more than 64kB? If so, try with MEDIUMBLOB.

Last updated 1 year ago.
0

jimhill said:

mabasic said:

zenry said:

look at @jimhill last code block

Please, don't reply to this topic again if you have nothing constructive to say. I read every post multiple times.

I don't think zenry was being unconstructive at all. What he was saying is look at this line:

'data:image/' . $your_image_model->type . ';base64,' . $your_image_model->base64

The output you need from your HTML should be a data URI. If you have never used them before then take a look at http://css-tricks.com/data-uris/

Your final rendered HTML should look somethin like:

I would personally prefer to use the proxy method (actually I would rather use files in a CDN rather than storing in a database due to overhead on large traffic sites).

How large are the images that you are storing in file size and have you also tried opening them in your browser directly before uploading via your app?

I ask this as I have seen a problem many times where people have been trying to debug the code when the image file itself is corrupt. So always good to debug from the start of the process.

Hi, I have tried using the URI method and using the PROXY method. With URI method, i can't get a image to show. With PROXY method it shows only half (50%). The image I am uploading is a valid image; not corrupt. Images are less than 0.5 MB in size.

I have been using Amazon S3 to handle the images until now, but it makes it hard to work with images while offline.

Last updated 1 year ago.
0

popolla said:

@mabasic: maybe your image is more than 64kB? If so, try with MEDIUMBLOB.

I am guessing that there is a image size limit ?

Last updated 1 year ago.
0

mabasic said:

zenry said:

look at @jimhill last code block

Please, don't reply to this topic again if you have nothing constructive to say. I read every post multiple times.

not my intention, just didn't want to repeat jimhill's answer, I'll try to explain more next time

uzivaj brate ;)

Last updated 1 year ago.
0

mabasic said:

popolla said:

@mabasic: maybe your image is more than 64kB? If so, try with MEDIUMBLOB.

I am guessing that there is a image size limit ?

I don't get your reply. If the image is bigger than 64kB, it will be truncated if stored as BLOB.

Last updated 1 year ago.
0

popolla said:

mabasic said:

popolla said:

@mabasic: maybe your image is more than 64kB? If so, try with MEDIUMBLOB.

I am guessing that there is a image size limit ?

I don't get your reply. If the image is bigger than 64kB, it will be truncated if stored as BLOB.

I was not aware of the 64kB limit on BLOB, that's all.

Last updated 1 year ago.
0

mabasic said:

popolla said:

mabasic said:

popolla said:

@mabasic: maybe your image is more than 64kB? If so, try with MEDIUMBLOB.

I am guessing that there is a image size limit ?

I don't get your reply. If the image is bigger than 64kB, it will be truncated if stored as BLOB.

I was not aware of the 64kB limit on BLOB, that's all.

Take a look at the MySQL data types and their limits http://www.developphp.com/view_lesson.php?v=246

Last updated 1 year ago.
0

I am going to implement MEDIUMBLOB as you suggested. Will post back with results tonight.

Last updated 1 year ago.
0

Hi, after reconsidering requirements for the app I decided to go with S3 to hold images online. I have marked the solution to my question. Thank you all for your efforts.

Last updated 1 year ago.
0

Hello Everyone to display image correctly check your data by inspecting correctly data:image/gif;base64,R&644... it should be coming wrong when u inspect element:- data:image/image/ remove image/ and add correct path and u will be done

Last updated 1 year ago.
0

For anyone else failing to save the encoded data as a full data uri, you can just cast it to string like so:

$data = (string) Image::make('public/bar.png')->encode('data-url');

Update docu link: http://image.intervention.io/api/encode

0

Hi all, I'm facing to the same problem. I get upload image correctly: same size, not corrupted. I put them in Longblob. It seems that they have the same size in blob. But when I get them back, there size are truncated. It appears ONLY above a certain image size. I mean I did it for images thoses size are under 1Mo and it works fine. I did it for 1.4 Mo and I get back only 1.0Mo file size

So when I donwload truncated files I get 50% of the image the rest is blank ... ????

 public function download($id)
    {
       $marketingImage = MarketingImage::findOrFail($id);
                
      $associatedGtin = Product::where('id',$marketingImage->product_id)->pluck('gtin')->first();

        // Make the Document available on Disk
       $pathToFile = "downloads/". $associatedGtin .'/marketing_image.' . $marketingImage->mime;

      $contents = $marketingImage->data;

       Storage::put($pathToFile, $contents);

      // Return the response
        $completePath = \Config::get('filesystems.disks.local.root').$pathToFile;

       return response()->download($completePath);            
    } 

Help would be really appreciated

Last updated 8 years ago.
0

mabasic said:

No, i really need to save the image in database, not on disk.

reason ?

0

I had the same problem with BLOB and solved it using TEXT instead...

For the DB, I used:

    $table->text('image')->nullable()->default(NULL);
    $table->text('imageType')->nullable()->default(NULL);

After migrating it, I then save the image in the controller as follows:

    public function store( Request $request ) {
        $user = Auth::user();

        if( $request->hasFile( 'image' ) ) {
            $image = $request->file( 'image' );
            $imageType = $image->getClientOriginalExtension();
            $imageStr = (string) Image::make( $image )->
                                     resize( 300, null, function ( $constraint ) {
                                         $constraint->aspectRatio();
                                     })->encode( $imageType );

            $user->image = base64_encode( $imageStr );
            $user->imageType = $imageType;
            $user->save();

        }
        return view( 'user', compact( 'user' ) );
    }

And finally, I get the image with:

    @if( Auth::user()->image )
        <img src="{{ "data:image/" . $user->imageType . ";base64," . $user->image }}">
    @else
        <img src="/uploads/images/default.png">
    @endif

Well, hope it helps!

Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mabasic mabasic Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.