Gallery.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace manager\models;
  3. use app\forks\galleryManager\GalleryBehavior;
  4. use Yii;
  5. //use yii\imagine\Image;
  6. /**
  7. * @property string $inject
  8. */
  9. class Gallery extends \app\models\base\Gallery
  10. {
  11. public function behaviors()
  12. {
  13. return [
  14. 'galleryBehavior' => [
  15. 'class' => GalleryBehavior::className(),
  16. 'type' => 'post_gallery',
  17. 'extension' => 'jpg',
  18. 'directory' => Yii::getAlias('@webroot') . '/images/post_gallery/gallery',
  19. 'url' => Yii::getAlias('@web') . '/images/post_gallery/gallery',
  20. 'versions' => [
  21. 'copy' => function ($img, $file) {
  22. $meta = $img->metadata();
  23. $ifile = $meta['filepath'];
  24. $originname = $_FILES['gallery-image']['name'];
  25. $t = explode(".", $originname);
  26. $ext = strtolower( end($t) );
  27. $t = explode(".", $file);
  28. $mext = end($t);
  29. // echo $ext;
  30. $copyName = str_replace( $mext, $ext, $file );
  31. if( copy($ifile, $copyName ) ) $this->copyName = $copyName;
  32. return true;
  33. },
  34. 'preview' => function ($img, $file) {
  35. /** @var \Imagine\Image\ImageInterface $img */
  36. $meta = $img->metadata();
  37. $size = $img->getSize();
  38. $width = $size->getWidth();
  39. $height = $size->getHeight();
  40. $addp = "-depth 8 -filter Lanczos -quality 60";
  41. $w = $this->previewWidth;
  42. $h = $this->previewHeight;
  43. $ifile = $meta['filepath'];
  44. $fork = '';
  45. if( $this->copyName ){
  46. $ifile = $this->copyName;
  47. $fork = " >/dev/null &";
  48. }
  49. exec("convert -define jpeg:size=".$width."x".$height."+0+0 ".$ifile." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file);
  50. return true;
  51. },
  52. 'small' => function ($img, $file) {
  53. // \Yii::$app->async->run(function() {
  54. // $image = $img->copy()->thumbnail(new \Imagine\Image\Box(200, 200));
  55. $meta = $img->metadata();
  56. $ifile = $meta['filepath'];
  57. $image = new \Imagick($ifile);
  58. $geometry0 = $image->getImageGeometry();
  59. $image_bg = new \Imagick( Yii::getAlias('@app').'/forks/galleryManager/assets/bg_photo.gif' );
  60. $geometrybg = $image_bg->getImageGeometry();
  61. $image->thumbnailImage($geometrybg['width'], $geometrybg['height'], true);
  62. $image->contrastImage( 1 );
  63. if( $geometry0['width'] < $geometry0['height'] ){
  64. // Вертикальные фото
  65. $geometry = $image->getImageGeometry();
  66. $x = ( $geometrybg['width'] - $geometry['width'] ) / 2;
  67. $y = ( $geometrybg['height'] - $geometry['height'] ) / 2;
  68. $image_bg->compositeImage( $image, \imagick::COMPOSITE_OVER, $x, $y );
  69. $image_bg->writeImages($file, false);
  70. }else{
  71. // Горизонтальные фото
  72. $image->writeImages($file, false);
  73. }
  74. // });
  75. return true;
  76. },
  77. 'medium' => function ($img, $file) {
  78. /** @var \Imagine\Image\ImageInterface $img */
  79. $dstSize = $img->getSize();
  80. $meta = $img->metadata();
  81. $maxWidth = 800;
  82. if ($dstSize->getWidth() > $maxWidth) {
  83. $dstSize = $dstSize->widen($maxWidth);
  84. }
  85. $addp = "-unsharp 0.25x0.25+8+0.065 -quality 75";
  86. $size = $img->getSize();
  87. $width = $size->getWidth();
  88. $height = $size->getHeight();
  89. $w = $dstSize->getWidth();
  90. $h = $dstSize->getHeight();
  91. $ifile = $meta['filepath'];
  92. $fork = '';
  93. if( $this->copyName ){
  94. $ifile = $this->copyName;
  95. $fork = " >/dev/null &";
  96. }
  97. 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);
  98. // echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file;
  99. return true;
  100. },
  101. 'large' => function ($img, $file) {
  102. /** @var \Imagine\Image\ImageInterface $img */
  103. $dstSize = $img->getSize();
  104. $meta = $img->metadata();
  105. $maxWidth = 1920;
  106. if ($dstSize->getWidth() > $maxWidth) {
  107. $dstSize = $dstSize->widen($maxWidth);
  108. }
  109. $addp = "-depth 8 -filter Lanczos -quality 95";
  110. $size = $img->getSize();
  111. $width = $size->getWidth();
  112. $height = $size->getHeight();
  113. $w = $dstSize->getWidth();
  114. $h = $dstSize->getHeight();
  115. $ifile = $meta['filepath'];
  116. $fork = '';
  117. if( $this->copyName ){
  118. $ifile = $this->copyName;
  119. $fork = " >/dev/null &";
  120. }
  121. 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);
  122. // echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file;
  123. return true;
  124. },
  125. 'original' => function ($img, $file) {
  126. /** @var \Imagine\Image\ImageInterface $img */
  127. $dstSize = $img->getSize();
  128. $meta = $img->metadata();
  129. $size = $img->getSize();
  130. $width = $size->getWidth();
  131. $height = $size->getHeight();
  132. $maxWidth = 3840;
  133. if($width < $maxWidth && $this->copyName ){
  134. $maxWidth = 1920;
  135. copy( $this->copyName, $file );
  136. return true;
  137. }
  138. if ($dstSize->getWidth() > $maxWidth) {
  139. $dstSize = $dstSize->widen($maxWidth);
  140. }
  141. $addp = "-depth 8 -filter Lanczos -quality 75";
  142. $w = $dstSize->getWidth();
  143. $h = $dstSize->getHeight();
  144. $ifile = $meta['filepath'];
  145. $fork = '';
  146. if( $this->copyName ){
  147. $ifile = $this->copyName;
  148. $fork = " >/dev/null &";
  149. }
  150. 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);
  151. //echo "convert -define jpeg:size=".$width."x".$height."+0+0 ".$meta['filepath']." -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp ".$file;
  152. return true;
  153. },
  154. ]
  155. ]
  156. ];
  157. }
  158. public function getInject()
  159. {
  160. return "##_gallery-".$this->id."##";
  161. }
  162. }