Image.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\models\base;
  3. use yii\base\Model;
  4. use yii\db\ActiveRecord;
  5. use yii\helpers\Url;
  6. /**
  7. * @property string $file_name
  8. * @property string $url
  9. * @property string $file_title
  10. */
  11. class Image extends Model
  12. {
  13. const SIZE_1040x586 = 1;
  14. const SIZE_680x383 = 2;
  15. const SIZE_500x282 = 3;
  16. const SIZE_320x180 = 4;
  17. const SIZE_320alt = 5;
  18. const SIZE_HD = 6;
  19. const SIZE_841x473 = 7;
  20. const SIZE_ORIGINAL = 0;
  21. const PREFIX_IMG = [self::SIZE_320x180=>'size4', self::SIZE_320alt=>'320', self::SIZE_500x282=>'size3', self::SIZE_680x383=>'size2', self::SIZE_841x473=>'size5', self::SIZE_1040x586=>'size1', self::SIZE_HD=>'sizehd', self::SIZE_ORIGINAL=>'origin', self::SIZE_1040x586.'_'=>'size1', self::SIZE_841x473=>'size5', self::SIZE_680x383.'_'=>'size2'];
  22. public $types = ["webp","jpg"];
  23. public $post_id = null;
  24. public $type = NULL;
  25. public $photo_name = '';
  26. public function getUrl_($size = 1,$type = NULL,$full = true)
  27. {
  28. $this->type = $type ?? $this->type;
  29. switch ($this->type){
  30. case "jpg":
  31. if( $size > 4 ){
  32. //echo \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg"; return;
  33. if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg" ) ){
  34. $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
  35. }else if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg" )){
  36. $url = "/images/news/news/{$this->post_id}_320.jpg";
  37. }
  38. $url = "/images/default.jpg";
  39. }
  40. if( $url = ( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg" ) ){
  41. return "/images/news/news/{$this->post_id}_size{$size}.jpg";
  42. }
  43. $url = '';
  44. break;
  45. default:
  46. if(file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_size{$size}.webp")) $url = "/images/news/webp/{$this->post_id}_size{$size}.webp";
  47. elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg")) $url = "/images/news/news/{$this->post_id}_size{$size}.jpg";
  48. elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpeg")) $url = "/images/news/news/{$this->post_id}_size{$size}.jpeg";
  49. elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg")) $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
  50. else $url = "/images/news/webp/{$this->post_id}_size{$size}.webp";
  51. }
  52. $base = Url::base('https');
  53. return ($full?$base:"").$url;
  54. }
  55. public function getUrl($size = 1,$type = NULL,$full = true)
  56. {
  57. $this->type = $type ?? $this->type;
  58. $url = $this->getSomebody($size, $this->type);
  59. //$url = '/images/default.jpeg';
  60. if( $url === false ) return false;
  61. $base = Url::base('https');
  62. if( stripos( $base,'amic.ru' ) === false ) $full = false;
  63. // if( IMG_GLOB == true ){
  64. // $full = true;
  65. // $base = 'https://www.amic.ru';
  66. // }
  67. return ($full?$base:"").$url;
  68. }
  69. public function getSomebody($one, $type){
  70. $f = false;
  71. // Повторное изображение из другой новости
  72. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  73. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  74. $obj = new image();
  75. $obj = $obj->findOne($this->photo_name*1);
  76. return $obj->getUrl($one, $type, false);
  77. }
  78. return $this->photo_name;
  79. }
  80. //
  81. foreach( self::PREFIX_IMG as $key => $pref ){
  82. $key = str_replace( '_', '', $key );
  83. if( $key == $one ) $f = true;
  84. if( $f ){
  85. if( $type == 'webp' ){
  86. if( file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
  87. return "/images/news/webp/{$this->post_id}_{$pref}.webp";
  88. }else{
  89. if( file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  90. }
  91. }
  92. if( $type == 'jpg' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg" ) ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  93. }
  94. }
  95. foreach( self::PREFIX_IMG as $key => $pref ){
  96. if( $key == $one ) break;
  97. if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
  98. return "/images/news/webp/{$this->post_id}_{$pref}.webp";
  99. }else{
  100. if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  101. }
  102. if( $type == 'jpg' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  103. }
  104. return false;
  105. }
  106. public function getIcon(){
  107. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  108. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  109. return "/images/news/news/{$this->photo_name}.jpg";
  110. }
  111. return $this->photo_name;
  112. }
  113. return "/images/news/news/{$this->post_id}.jpg";
  114. }
  115. public static function find(){
  116. $model = new static();
  117. return $model;
  118. }
  119. public static function findOne($id,$type="webp",$photo_name=""){
  120. $model = self::find();
  121. $model->type = $type;
  122. $model->post_id = $id;
  123. $model->photo_name = $photo_name;
  124. return $model;
  125. }
  126. }