Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented 'Image' field. Added 'cropper' dependancy. #56

Merged
merged 1 commit into from
Aug 6, 2016

Conversation

dezlitz
Copy link
Contributor

@dezlitz dezlitz commented Jul 10, 2016

Usage:

$this->crud->addField([
                                'label' => "Profile Image",
                                'name' => "image",
                                'filename' => "image_filename", // set to null if not needed
                                'type' => 'image',
                                'aspect_ratio' => 1, // set to 0 to allow any aspect ratio
                                'crop' => true, // set to true to allow cropping, false to disable
                                'src' => 'getImageSource' // null to read straight from DB, otherwise set to model accessor function

                            ]);

Images are submitted in base64 format. In my basic tests images may be 1.5 to 3 times larger than the original.

When saving directly to DB (e.g. MySQL) make sure you set the field type to 'LONGBLOB' and increase the limits on form uploads: e.g. mysqld --innodb_log_file_size=1G --max_allowed_packet=100MB

Alternatively you can intercept the 'image' request field and save to alternate location. e.g. in CRUD controller 'update' or 'store' (Using the 'Intervention' and 'Laravel-MediaLibrary' packages):

use Intervention\Image\ImageManagerStatic as Image;

$imageFilename = $request->input('image_filename');
 if ($imageFilename !== ''){ 
            $imageFilename = uniqid('image_').'.png'; 
            Image::make($request->input('image'))->resize(200,200)->save($storagePath.$imageFilename);
            $user->addMedia($storagePath.$imageFilename)->toCollection('image');
        }

And then retrieve the media on edit. e.g. in User Model:

public function getImageSource() {

        if ($this->getMedia('image')->last()){
            return '/media/user/' . $this->id . '/image';
        }
        return '';
    }

And set up a route and controller:
Route::get('media/user/{user}/{collection}', 'UserMediaController@getMedia');

public function getMedia($userId, $collection)

    {
        $user = User::findOrFail($userId);
        $media = $user->getMedia($collection)->last();
        $filePath = isset($media) ? $media->getPath() : abort(404, 'Media not found.');
        return Image::make($media->getPath())->response();
    }

@tabacitu
Copy link
Member

@deslittle , I am SO SO SORRY for not replying sooner. I archived the email thinking I'd remember to reply the same day, but apparently I forgot.

This is SO COOL. Thanks a lot for the work, I've tried it and it looks and works great. However, I don't think storing images in the database is a good idea for most people. It might be for a few use cases, so it might be the right technical solution for your project, but I think if we include this in the core as the only "image" field type, a lot of developers will store them that way, and we'd be making a niche practice mainstream.

So here's what I propose, if you're ok with it:

  1. I'll rename this one from image to base64_image;
  2. I'll add a field type called image, with the same front-end as this one, but that saves the image on disk;

Making these two fields available at the same time will both:

  • solve my fear of encouraging developers to store images in the db;
  • (and)
  • allow developers to easily store base64 in the db;

What do you think, is it ok if I do that?

Thanks a lot, your work is great and seeing people like you contribute really makes all this effort worthwhile. And sorry again for the delay...

Cheers!

@dezlitz
Copy link
Contributor Author

dezlitz commented Jul 27, 2016

Haha no problem at all. This was actually my first open source contribution so I wasn't sure if I had done something wrong.

I agree it's generally not a good idea to store images in the DB and as I mentioned above you quickly run into problems the larger the images get.

Good idea splitting the fields into image and base64_image.

Thanks for the great library! I was able to get up my admin area up and running in just a couple of hours. I particularly like the modular design and how it stays out of my main app logic while allowing me to override certain views as I need to. Keep up the good work!

@tabacitu tabacitu added this to the 3.2 - maturity milestone Jul 28, 2016
@tabacitu tabacitu self-assigned this Jul 28, 2016
@tabacitu tabacitu added the ready label Jul 31, 2016
@DennisCraandijk
Copy link

+1 for this feature, very useful.

@tabacitu tabacitu merged commit 550ee82 into Laravel-Backpack:master Aug 6, 2016
@tabacitu tabacitu removed the ready label Aug 6, 2016
@tabacitu
Copy link
Member

tabacitu commented Aug 6, 2016

Update: still haven't gotten the time to create the image field based on yours, so I'll go ahead and include this as base64_image - so at least we'll have that.

Cheers!

@bahman2216
Copy link

base64_image store 4-5 times larger than original image size. I have jpg image 16kb size and after crop and save on DB it got 90kb size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants