Gallery.php 7.8 KB

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