[ 'class' => GalleryBehavior::className(), 'type' => 'post_gallery', 'extension' => 'jpg', 'directory' => Yii::getAlias('@webroot') . '/images/post_gallery/gallery', 'url' => Yii::getAlias('@web') . '/images/post_gallery/gallery', 'versions' => [ 'copy' => function ($img, $file) { $meta = $img->metadata(); $ifile = $meta['filepath']; $originname = $_FILES['gallery-image']['name']; $t = explode(".", $originname); $ext = strtolower( end($t) ); $t = explode(".", $file); $mext = end($t); // echo $ext; $copyName = str_replace( $mext, $ext, $file ); if( copy($ifile, $copyName ) ) $this->copyName = $copyName; return true; }, 'preview' => function ($img, $file) { /** @var \Imagine\Image\ImageInterface $img */ $meta = $img->metadata(); $size = $img->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $addp = "-depth 8 -filter Lanczos -quality 60"; $w = $this->previewWidth; $h = $this->previewHeight; $ifile = $meta['filepath']; $fork = ''; if( $this->copyName ){ $ifile = $this->copyName; $fork = " >/dev/null &"; } exec("convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file); return true; }, 'small' => function ($img, $file) { // \Yii::$app->async->run(function() { // $image = $img->copy()->thumbnail(new \Imagine\Image\Box(200, 200)); $meta = $img->metadata(); $ifile = $meta['filepath']; $image = new \Imagick($ifile); $geometry0 = $image->getImageGeometry(); $image_bg = new \Imagick( Yii::getAlias('@app').'/forks/galleryManager/assets/bg_photo.gif' ); $geometrybg = $image_bg->getImageGeometry(); $image->thumbnailImage($geometrybg['width'], $geometrybg['height'], true); $image->contrastImage( 1 ); if( $geometry0['width'] < $geometry0['height'] ){ // Вертикальные фото $geometry = $image->getImageGeometry(); $x = ( $geometrybg['width'] - $geometry['width'] ) / 2; $y = ( $geometrybg['height'] - $geometry['height'] ) / 2; $image_bg->compositeImage( $image, \imagick::COMPOSITE_OVER, $x, $y ); $image_bg->writeImages($file, false); }else{ // Горизонтальные фото $image->writeImages($file, false); } // }); return true; }, 'medium' => function ($img, $file) { /** @var \Imagine\Image\ImageInterface $img */ $dstSize = $img->getSize(); $meta = $img->metadata(); $maxWidth = 800; if ($dstSize->getWidth() > $maxWidth) { $dstSize = $dstSize->widen($maxWidth); } $addp = "-unsharp 0.25x0.25+8+0.065 -quality 75"; $size = $img->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $w = $dstSize->getWidth(); $h = $dstSize->getHeight(); $ifile = $meta['filepath']; $fork = ''; if( $this->copyName ){ $ifile = $this->copyName; $fork = " >/dev/null &"; } exec("nice -n 1 convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file.$fork); // echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file; return true; }, 'large' => function ($img, $file) { /** @var \Imagine\Image\ImageInterface $img */ $dstSize = $img->getSize(); $meta = $img->metadata(); $maxWidth = 1920; if ($dstSize->getWidth() > $maxWidth) { $dstSize = $dstSize->widen($maxWidth); } $addp = "-depth 8 -filter Lanczos -quality 95"; $size = $img->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $w = $dstSize->getWidth(); $h = $dstSize->getHeight(); $ifile = $meta['filepath']; $fork = ''; if( $this->copyName ){ $ifile = $this->copyName; $fork = " >/dev/null &"; } exec("nice -n 10 convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file.$fork); // echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file; return true; }, 'original' => function ($img, $file) { /** @var \Imagine\Image\ImageInterface $img */ $dstSize = $img->getSize(); $meta = $img->metadata(); $size = $img->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); $maxWidth = 3840; if($width < $maxWidth && $this->copyName ){ $maxWidth = 1920; copy( $this->copyName, $file ); return true; } if ($dstSize->getWidth() > $maxWidth) { $dstSize = $dstSize->widen($maxWidth); } $addp = "-depth 8 -filter Lanczos -quality 75"; $w = $dstSize->getWidth(); $h = $dstSize->getHeight(); $ifile = $meta['filepath']; $fork = ''; if( $this->copyName ){ $ifile = $this->copyName; $fork = " >/dev/null &"; } exec("nice -n 15 convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file.$fork); //echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file; return true; }, ] ] ]; } public function getInject() { return "##_gallery-".$this->id."##"; } }