Image.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_ORIGINAL = 0;
  18. public $types = ["webp","jpg"];
  19. public $post_id = null;
  20. public $type = NULL;
  21. public function getUrl($size = 1,$type = NULL,$full = true)
  22. {
  23. $this->type = $type ?? $this->type;
  24. switch ($this->type){
  25. case "jpg":
  26. return "https://green.amic.ru/images/news/news/{$this->post_id}_size{$size}.jpg";
  27. break;
  28. default:
  29. return "https://green.amic.ru/images/news/webp/{$this->post_id}_size{$size}.webp";
  30. }
  31. return ($full?"https://api.amic.ru":"")."/uploads/news/images/".$this->file_name;
  32. }
  33. public static function find(){
  34. $model = new static();
  35. return $model;
  36. }
  37. public static function findOne($id,$type="webp"){
  38. $model = self::find();
  39. $model->type = $type;
  40. $model->post_id = $id;
  41. return $model;
  42. }
  43. }