123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace manager\models;
- use app\forks\galleryManager\GalleryBehavior;
- use yii\imagine\Image;
- use Yii;
- //use yii\imagine\Image;
- /**
- * @property string $inject
- */
- class Gallery extends \app\models\base\Gallery
- {
- public $tmpName = '';
- public function behaviors()
- {
- return [
- 'galleryBehavior' => [
- '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;
- // exec( "cp $ifile $copyName" );
- // $this->copyName = $copyName;
- 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 5 convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file);
- $this->tmpName = $file;
- // file_put_contents($file.".sh", "nice -n 1 convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file);
- // echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file;
- 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 &";
- }
- if( $this->tmpName ){
- $ifile = $this->tmpName;
- list($width, $height) = getimagesize($ifile);
- // $fork = " >/dev/null &";
- }
- exec("convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file.$fork);
- // file_put_contents($file.".sh", "convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file.$fork);
- return true;
- },
- 'small' => function ($img, $file) {
- // \Yii::$app->async->run(function() {
- // $image = $img->copy()->thumbnail(new \Imagine\Image\Box(200, 200));
- // if( $this->tmpName ){
- // $img1 = Image::getImagine()->open($this->tmpName);
- // }
- $meta = $img->metadata();
- $ifile = $meta['filepath'];
- if( $this->tmpName && file_exists($this->tmpName)){
- $ifile = $this->tmpName;
- // file_put_contents($file.".x", "(".$this->tmpName.").(".$ifile.")");
- }
- $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 &";
- }
- if( $this->tmpName ){
- $ifile = $this->tmpName;
- $fork = " >/dev/null &";
- list($width, $height) = getimagesize($ifile);
- }
- exec("nice -n 5 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=".$w."x".$h."+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."##";
- }
- }
|