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

Image variations #163

Open
paulocoutinhox opened this issue Sep 13, 2020 · 1 comment
Open

Image variations #163

paulocoutinhox opened this issue Sep 13, 2020 · 1 comment

Comments

@paulocoutinhox
Copy link
Contributor

Hi,

How to create image variations?

I need create original, thumb and mobile variations. But today i can work only with the original image:

'on afterSave' => function ($event) {
                    /* @var $file File */
                    $file = $event->file;
                    
                    $img = ImageManagerStatic::make($file->read())->resize(null, 600, function ($constraint) {
                        $constraint->aspectRatio();
                    });

                    $file->put($img->encode());
                }
@paulocoutinhox
Copy link
Contributor Author

paulocoutinhox commented Sep 13, 2020

I made a function to this. But it is creating the original file, the thumb (from copy function) and a third with other has:

private function generateThumb($filePath)
    {
        $storage = Yii::$app->galleryFileStorage;

        $storage->on(Storage::EVENT_AFTER_SAVE, function (/* @var $event StorageEvent */ $event) use (&$storage) {
            $fs = $event->filesystem;
            $img = ImageManagerStatic::make($fs->read($event->path))->fit(100, 100);

            $fs->put($event->path, $img->encode());
        });

        $parts = pathinfo($filePath);
        $dir = $parts['dirname'];
        $filename = $parts['basename'];
        $newFilePath = ($dir . DIRECTORY_SEPARATOR . 'thumb_' . $filename);
        copy($filePath, $newFilePath);

        $storage->save($filePath);
    }

image

In my example:

  1. TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg = original uploaded file
  2. thumb_TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg = original copied to be resized to thumb and overided
  3. DV2IDDsd13CbVzhia2tjLcPrX-mBZvXY.jpg = this is the third file resized (thumb) that has a new name instead of "thumb_TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg"

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

No branches or pull requests

1 participant