Advertisement
pmtpenza22

Untitled

Feb 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public function actionSaveImg()
  2. {
  3. Yii::$app->response->format = Response::FORMAT_JSON;
  4. $this->enableCsrfValidation = false;
  5.  
  6. $dir = Yii::getAlias('@images').'/product';
  7. if(!file_exists($dir)){
  8. FileHelper::createDirectory($dir);
  9. }
  10. $result_link = str_replace('admin.','',Url::home(true)).'uploads/images/'.'';
  11. $file = UploadedFile::getInstanceByName('ImagesManager[attachment]');
  12.  
  13. $model = new ImagesManager();
  14. $model->name = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) .'.'.$file->extension;
  15. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  16.  
  17.  
  18. if($file->saveAs($dir . $model->name)){
  19. /* $imag = Yii::$app->image->load($dir . $model->name);
  20. $imag->resize(800, null, Yii\image\drivers\Image::PRECISE)->save($dir . $model->name,85);*/
  21. $result = [
  22. 'filelink' => $result_link . $model->name,
  23. 'filename' => $model->name
  24. ];
  25.  
  26. $model->save();
  27. } else {
  28. $result = [
  29. 'error' => 'Ошибка загрузки'
  30. ];
  31. }
  32.  
  33. return $result;
  34. }
  35.  
  36. return [
  37. 'error' => $model->errors(),
  38. ];
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement