UserexController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace manager\controllers;
  3. use dektrium\user\models\Profile;
  4. use dektrium\user\models\User;
  5. use yii\web\Controller;
  6. use yii\web\NotFoundHttpException;
  7. use yii\filters\VerbFilter;
  8. class UserexController extends BaseController
  9. {
  10. public static $story_dir = 'images/users';
  11. /**
  12. * Displays a single User model.
  13. * @param int $id id
  14. * @return string
  15. * @throws NotFoundHttpException if the model cannot be found
  16. */
  17. public function actionIndex($id)
  18. {
  19. $model = $this->findModel($id);
  20. if ($model && $this->request->isPost ) {
  21. $name = $_FILES['User']['tmp_name']['photo'];
  22. if (is_uploaded_file($name)) {
  23. $post = \Yii::$app->request->post();
  24. $pcut = isset($post['cropping'])?$post['cropping']:false;
  25. $file_tmp = \Yii::getAlias('@webroot').'/'.static::$story_dir."/".basename($name);
  26. if( move_uploaded_file($_FILES['User']['tmp_name']['photo'], $file_tmp) ){
  27. list($width, $height) = getimagesize($file_tmp);
  28. if( $pcut ){
  29. $cut = ['x'=>round($pcut['x']), 'y'=>round($pcut['y']), 'w'=>round($pcut['width']), 'h'=>round($pcut['height'])];
  30. $name = basename($id."_sm.jpg");
  31. $cutfile = \Yii::getAlias('@webroot').'/'.static::$story_dir."/$name";
  32. $this->generateImageCrop($file_tmp, $cutfile, 128, 128, $cut, ['sharpen' => 10]);
  33. $name = basename($id."_norm.jpg");
  34. $cutfile = \Yii::getAlias('@webroot').'/'.static::$story_dir."/$name";
  35. $this->generateImageCrop($file_tmp, $cutfile, 256, 256, $cut, ['sharpen' => 5]);
  36. }
  37. unlink($file_tmp);
  38. }
  39. }
  40. return $this->redirect(['/user/admin/index']);
  41. }
  42. return $this->render('view', [
  43. 'model' => $this->findModel($id), 'obj' => $this
  44. ]);
  45. }
  46. public function generateImageCrop( $ifile, $ofile, $ww, $hh, $param )
  47. {
  48. // to do add rotate, etc
  49. extract( $param );
  50. list($width, $height) = getimagesize($ifile);
  51. $addp = '';
  52. if( strrpos($ofile, '.webp') == strlen($ofile) - strlen('.webp') )
  53. {
  54. $addp = " -background '#FFFFFF' -alpha off -strip Plane -quality 88 -define webp:method=6 "; //-interlace
  55. }
  56. if(isset($sharpen))
  57. {
  58. $addp = $addp." -sharpen ".$sharpen;
  59. }
  60. $addp .= " -quality 80";
  61. umask(0113);
  62. exec("convert -define jpeg:size=".$width."x".$height."+0+0 $ifile -crop ".$w."x".$h."+".$x."+".$y." -thumbnail ".$ww."x".$hh."^ $addp -gravity center $ofile" );
  63. }
  64. /**
  65. * Finds the Authors model based on its primary key value.
  66. * If the model is not found, a 404 HTTP exception will be thrown.
  67. * @param int $id id
  68. * @return Authors the loaded model
  69. * @throws NotFoundHttpException if the model cannot be found
  70. */
  71. protected function findModel($id)
  72. {
  73. if (($model = User::findOne(['id' => $id])) !== null) {
  74. return $model;
  75. }
  76. throw new NotFoundHttpException('The requested page does not exist.');
  77. }
  78. }