rapyd-laravel icon indicating copy to clipboard operation
rapyd-laravel copied to clipboard

Save images more than one size

Open holic-cl opened this issue 9 years ago • 6 comments

Hi!, i need to save a image in more than one size. I was reading the code, but i don't know how to override the "save" action in a form. Do you know where i have to do this?, can you give me any clue?.

Regards

holic-cl avatar May 18 '15 02:05 holic-cl

An answer to this question could be very handy.. I will have a look myself on this.

mberende avatar Jul 01 '16 19:07 mberende

no problem at all, remember that there is an image closure (and is documented) https://github.com/zofe/rapyd-laravel/wiki/Field-list

just a cut&paste from a project..

$form->add('logo','Logo', 'image')
            ->move('fullpath/image/')
            ->webPath('')  //this is intended to change (add or remove) path to the stored field
            ->preview(90,90) 
            ->image(function ($image)  {
                /** @var \Intervention\Image\ImageManager $image */

                $path = dirname($image->basepath());
                $foto_name  = $image->basename;
                $foto_thumb = filename_append($foto_name,    '_thumb');
                $foto_ricerca = filename_append($foto_name,  '_mini');
                $foto_evidenza = filename_append($foto_name, '_micro');

                $image->fit(150, 150);
                $image->save($path.'/'.$foto_thumb);

                $image->fit(88, 88);
                $image->save($path.'/'.$foto_ricerca);

                $image->fit(59, 59);
                $image->save($path.'/'.$foto_evidenza);


            })->rule('mimes:jpeg|max:3072');

....

//this is just an helper  made to add suffix before file extension
function filename_append($filename, $append)
{
    $ext   = pathinfo($filename, PATHINFO_EXTENSION);
    return basename($filename, ".$ext") . $append.'.' . $ext;
}



zofe avatar Jul 02 '16 05:07 zofe

Thanks Zofe. Actually you do not even have to use the callback as just calling the fit() or resize() function multiple time will save them resized/fitted (by default in the public folder )

I have slightly modified your imageProcess() function so I can pass a path instead of a filename when calling the fit() method

            if (count($this->fit)) {
                foreach ($this->fit as $fit) {

                    $this->image->fit($fit["width"], $fit["height"]);
                    $this->image->save($fit["filename"] .  $this->new_value );
                }

mberende avatar Jul 02 '16 05:07 mberende

@mberende did you forked the project to add this?
I don't see forks in your account, the good way to make changes keeping dependencies is

  • fork the repo https://help.github.com/articles/fork-a-repo/
  • change your composer to point to your fork

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/mberende/rapyd"
        }
    ],
    "require": {
        "zofe/rapyd": "^2.1"
    }
}
  • then share the code using pull request https://help.github.com/articles/creating-a-pull-request/

Note: this is only a suggestion feel free to edit your vendor.. and manage each composer update with a merge if you're more confident.

zofe avatar Jul 02 '16 06:07 zofe

Thanks for the instructions. I haven't forked this yet, I only made the fixes on my test env. I will fork and make some pull requests in the coming days.

mberende avatar Jul 02 '16 06:07 mberende

Thank You so much

zofe avatar Jul 02 '16 06:07 zofe