Image.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. foreach( self::PREFIX_IMG as $key => $pref ){
  72. $key = str_replace( '_', '', $key );
  73. if( $key == $one ) $f = true;
  74. if( $f ){
  75. if( $type == 'webp' ){
  76. if( file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
  77. return "/images/news/webp/{$this->post_id}_{$pref}.webp";
  78. }else{
  79. if( file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  80. }
  81. }
  82. 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";
  83. }
  84. }
  85. foreach( self::PREFIX_IMG as $key => $pref ){
  86. if( $key == $one ) break;
  87. if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
  88. return "/images/news/webp/{$this->post_id}_{$pref}.webp";
  89. }else{
  90. 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";
  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. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  95. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  96. $obj = new image();
  97. $obj = $obj->findOne($this->photo_name*1);
  98. return $obj->getUrl($one, $type, false);
  99. }
  100. return $this->photo_name;
  101. }
  102. return false;
  103. }
  104. public function getIcon(){
  105. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  106. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  107. return "/images/news/news/{$this->photo_name}.jpg";
  108. }
  109. return $this->photo_name;
  110. }
  111. return "/images/news/news/{$this->post_id}.jpg";
  112. }
  113. public static function find(){
  114. $model = new static();
  115. return $model;
  116. }
  117. public static function findOne($id,$type="webp",$photo_name=""){
  118. $model = self::find();
  119. $model->type = $type;
  120. $model->post_id = $id;
  121. $model->photo_name = $photo_name;
  122. return $model;
  123. }
  124. }