Image.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_ORIGINAL = 0;
  20. const PREFIX_IMG = [self::SIZE_320x180=>'size4', self::SIZE_320alt=>'320', self::SIZE_500x282=>'size3', self::SIZE_680x383=>'size2', self::SIZE_1040x586=>'size1', self::SIZE_HD=>'sizehd', self::SIZE_ORIGINAL=>'origin', self::SIZE_1040x586.'_'=>'size1', self::SIZE_680x383.'_'=>'size2'];
  21. public $types = ["webp","jpg"];
  22. public $post_id = null;
  23. public $type = NULL;
  24. public $photo_name = '';
  25. public function getUrl_($size = 1,$type = NULL,$full = true)
  26. {
  27. $this->type = $type ?? $this->type;
  28. switch ($this->type){
  29. case "jpg":
  30. if( $size > 4 ){
  31. //echo \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg"; return;
  32. if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg" ) ){
  33. $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
  34. }else if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg" )){
  35. $url = "/images/news/news/{$this->post_id}_320.jpg";
  36. }
  37. $url = "/images/default.jpg";
  38. }
  39. if( $url = ( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg" ) ){
  40. return "/images/news/news/{$this->post_id}_size{$size}.jpg";
  41. }
  42. $url = '';
  43. break;
  44. default:
  45. 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";
  46. 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";
  47. 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";
  48. elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg")) $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
  49. else $url = "/images/news/webp/{$this->post_id}_size{$size}.webp";
  50. }
  51. $base = Url::base('https');
  52. return ($full?$base:"").$url;
  53. }
  54. public function getUrl($size = 1,$type = NULL,$full = true)
  55. {
  56. $this->type = $type ?? $this->type;
  57. $url = $this->getSomebody($size, $this->type);
  58. //$url = '/images/default.jpeg';
  59. if( $url === false ) return false;
  60. $base = Url::base('https');
  61. if( stripos( $base,'amic.ru' ) === false ) $full = false;
  62. // if( IMG_GLOB == true ){
  63. // $full = true;
  64. // $base = 'https://www.amic.ru';
  65. // }
  66. return ($full?$base:"").$url;
  67. }
  68. public function getSomebody($one, $type){
  69. $f = false;
  70. foreach( self::PREFIX_IMG as $key => $pref ){
  71. $key = str_replace( '_', '', $key );
  72. if( $key == $one ) $f = true;
  73. if( $f ){
  74. if( $type == 'webp' ){
  75. if( file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
  76. return "/images/news/webp/{$this->post_id}_{$pref}.webp";
  77. }else{
  78. if( file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  79. }
  80. }
  81. 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";
  82. }
  83. }
  84. foreach( self::PREFIX_IMG as $key => $pref ){
  85. if( $key == $one ) break;
  86. if( $type == 'webp' && 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( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
  90. }
  91. 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";
  92. }
  93. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  94. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  95. $obj = new image();
  96. $obj = $obj->findOne($this->photo_name*1);
  97. return $obj->getUrl($one, $type, false);
  98. }
  99. return $this->photo_name;
  100. }
  101. return false;
  102. }
  103. public function getIcon(){
  104. if( isset( $this->photo_name ) && $this->photo_name != '' ){
  105. if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
  106. return "/images/news/news/{$this->photo_name}.jpg";
  107. }
  108. return $this->photo_name;
  109. }
  110. return "/images/news/news/{$this->post_id}.jpg";
  111. }
  112. public static function find(){
  113. $model = new static();
  114. return $model;
  115. }
  116. public static function findOne($id,$type="webp",$photo_name=""){
  117. $model = self::find();
  118. $model->type = $type;
  119. $model->post_id = $id;
  120. $model->photo_name = $photo_name;
  121. return $model;
  122. }
  123. }