yii2-file-kit icon indicating copy to clipboard operation
yii2-file-kit copied to clipboard

Image variations

Open paulocoutinhox opened this issue 4 years ago • 1 comments

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 avatar Sep 13 '20 18:09 paulocoutinhox

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"

paulocoutinhox avatar Sep 13 '20 19:09 paulocoutinhox