Images.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\helpers\FileHelper;
  6. use yii\image\drivers\Image;
  7. use yii\db\ActiveRecord;
  8. /**
  9. * This is the model class for table "images".
  10. *
  11. * @property integer $id
  12. * @property integer $related_id
  13. * @property string $path
  14. * @property string $type
  15. * @property integer $date_create
  16. */
  17. class Images extends ActiveRecord
  18. {
  19. public $_file;
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return 'images';
  26. }
  27. public function behaviors()
  28. {
  29. return [
  30. 'timestamp' => [
  31. 'class' => TimestampBehavior::className(),
  32. 'attributes' => [
  33. ActiveRecord::EVENT_BEFORE_INSERT => 'date_create',
  34. ]
  35. ]
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['related_id', 'type'], 'required'],
  45. [['related_id', 'date_create'], 'integer'],
  46. [['path'], 'string', 'max' => 255],
  47. ];
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => Yii::t('app', 'ID'),
  56. 'related_id' => Yii::t('app', 'Related ID'),
  57. 'path' => Yii::t('app', 'Path'),
  58. 'date_create' => Yii::t('app', 'Date Create'),
  59. 'type' => Yii::t('app', 'Type'),
  60. ];
  61. }
  62. public function upload()
  63. {
  64. if (!$this->validate()) {
  65. return false;
  66. }
  67. $path = "uploads/images/{$this->related_id}/";
  68. $croppedPath = $path . '75x75/';
  69. if (!is_dir($path)) {
  70. FileHelper::createDirectory($path);
  71. }
  72. if (!is_dir($croppedPath)) {
  73. FileHelper::createDirectory($croppedPath);
  74. }
  75. $time = time();
  76. $this->path = "{$path}{$this->_file->basename}_{$time}.{$this->_file->extension}";
  77. if ($this->_file->saveAs($this->path)) {
  78. /** @var Image $img * */
  79. $img = Yii::$app->image->load($this->path);
  80. $img->background('#fff', 0);
  81. $img->resize('75', '75', Image::INVERSE);
  82. $img->crop('75', '75');
  83. $img->save($croppedPath . "{$this->_file->basename}_{$time}.{$this->_file->extension}");
  84. (new Images([
  85. 'type' => 'thumbnail',
  86. 'path' => $croppedPath . "{$this->_file->basename}_{$time}.{$this->_file->extension}",
  87. 'related_id' => $this->related_id
  88. ]))->save(false);
  89. }
  90. return $this->save(false);
  91. }
  92. public function copy($related_id)
  93. {
  94. $path = str_replace($this->related_id, $related_id, $this->path);
  95. $directoryPath = explode('/', $path);
  96. array_pop($directoryPath);
  97. $directoryPath = implode('/', $directoryPath);
  98. FileHelper::createDirectory($directoryPath);
  99. copy($this->path, $path);
  100. $model = new Images();
  101. $data = $this->attributes;
  102. $data['path'] = $path;
  103. $data['related_id'] = $related_id;
  104. return $model->load($data, '') && $model->save(false);
  105. }
  106. public function beforeDelete()
  107. {
  108. if ($this->thumbnail && is_file($this->thumbnail->path)) {
  109. unlink($this->thumbnail->path);
  110. }
  111. parent::beforeDelete();
  112. }
  113. }