123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\models\base;
- use yii\base\Model;
- use yii\db\ActiveRecord;
- use yii\helpers\Url;
- /**
- * @property string $file_name
- * @property string $url
- * @property string $file_title
- */
- class Image extends Model
- {
- const SIZE_1040x586 = 1;
- const SIZE_680x383 = 2;
- const SIZE_500x282 = 3;
- const SIZE_320x180 = 4;
- const SIZE_320alt = 5;
- const SIZE_HD = 6;
- const SIZE_841x473 = 7;
- const SIZE_ORIGINAL = 0;
- 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'];
- public $types = ["webp","jpg"];
- public $post_id = null;
- public $type = NULL;
- public $photo_name = '';
- public function getUrl_($size = 1,$type = NULL,$full = true)
- {
- $this->type = $type ?? $this->type;
- switch ($this->type){
- case "jpg":
- if( $size > 4 ){
- //echo \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg"; return;
- if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg" ) ){
- $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
- }else if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg" )){
- $url = "/images/news/news/{$this->post_id}_320.jpg";
- }
- $url = "/images/default.jpg";
- }
- if( $url = ( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg" ) ){
- return "/images/news/news/{$this->post_id}_size{$size}.jpg";
- }
- $url = '';
- break;
- default:
- 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";
- 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";
- 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";
- elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg")) $url = "/images/news/news/{$this->post_id}_{$size}.jpg";
- else $url = "/images/news/webp/{$this->post_id}_size{$size}.webp";
- }
- $base = Url::base('https');
- return ($full?$base:"").$url;
- }
- public function getUrl($size = 1,$type = NULL,$full = true)
- {
- $this->type = $type ?? $this->type;
- $url = $this->getSomebody($size, $this->type);
- //$url = '/images/default.jpeg';
- if( $url === false ) return false;
- $base = Url::base('https');
- if( stripos( $base,'amic.ru' ) === false ) $full = false;
- // if( IMG_GLOB == true ){
- // $full = true;
- // $base = 'https://www.amic.ru';
- // }
- return ($full?$base:"").$url;
- }
- public function getSomebody($one, $type){
- $f = false;
- // Повторное изображение из другой новости
- if( isset( $this->photo_name ) && $this->photo_name != '' ){
- if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
- $obj = new image();
- $obj = $obj->findOne($this->photo_name*1);
- return $obj->getUrl($one, $type, false);
- }
- return $this->photo_name;
- }
- //
- foreach( self::PREFIX_IMG as $key => $pref ){
- $key = str_replace( '_', '', $key );
- if( $key == $one ) $f = true;
- if( $f ){
- if( $type == 'webp' ){
- if( file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
- return "/images/news/webp/{$this->post_id}_{$pref}.webp";
- }else{
- if( file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg") ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
- }
- }
- 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";
- }
- }
- foreach( self::PREFIX_IMG as $key => $pref ){
- if( $key == $one ) break;
- if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp") ){
- return "/images/news/webp/{$this->post_id}_{$pref}.webp";
- }else{
- 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";
- }
- 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";
- }
- return false;
- }
- public function getIcon(){
- if( isset( $this->photo_name ) && $this->photo_name != '' ){
- if( is_numeric( $this->photo_name ) && is_int( $this->photo_name*1 ) ){
- return "/images/news/news/{$this->photo_name}.jpg";
- }
- return $this->photo_name;
- }
- return "/images/news/news/{$this->post_id}.jpg";
- }
- public static function find(){
- $model = new static();
- return $model;
- }
- public static function findOne($id,$type="webp",$photo_name=""){
- $model = self::find();
- $model->type = $type;
- $model->post_id = $id;
- $model->photo_name = $photo_name;
- return $model;
- }
- }
|