yii2-file-kit
yii2-file-kit copied to clipboard
Image variations
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());
}
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);
}
In my example:
- TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg = original uploaded file
- thumb_TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg = original copied to be resized to thumb and overided
- DV2IDDsd13CbVzhia2tjLcPrX-mBZvXY.jpg = this is the third file resized (thumb) that has a new name instead of "thumb_TLAbUMPno_YsS4daKHLiIChWNunU_LAS.jpg"