SiteController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use app\common\helpers\Capcha;
  6. use app\common\helpers\Session;
  7. use app\common\components\Upload;
  8. use app\modules\admin\models\Attachment;
  9. use yii\web\Response;
  10. class SiteController extends Controller
  11. {
  12. public function actions()
  13. {
  14. return [
  15. 'captcha' => [
  16. 'class' => 'easydowork\rotateCaptcha\CaptchaAction',
  17. 'captchaOption' => [
  18. 'imagePath' => BASE_PATH.'web'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'captcha'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR,//验证码图片库目录
  19. 'cacheImagePath' => BASE_PATH.'web'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'captcha'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR,//旋转后验证码缓存目录
  20. 'cacheHandel' => '',//缓存类继承yii\caching\Cache
  21. 'cacheExpire' => 60,//验证码token过期时间
  22. 'salt' => Yii::$app->params['authKey'],//加密字符串参数 默认为当前文件地址
  23. 'size' => 350,//生成图片大小 px
  24. 'earea' => 10,//验证图片时允许旋转角度的误差值
  25. 'imageHandle'=>'gd',//推荐使用gd库,个人测试imagick时在windows和linux上生成的图片不一致,有能力的欢迎pr.
  26. 'imageHandleConfig' => [ //生成验证码的参数
  27. 'quality' => 80,//图片质量
  28. 'bgcolor' => '#fff', // 底色
  29. ],
  30. ]
  31. ],
  32. ];
  33. }
  34. public function actionIndex()
  35. {
  36. if((defined('IN_ADMIN')&&IN_ADMIN==true))//如果是后台入口文件,跳转到后台首页
  37. {
  38. $this->redirect(APP_URL.'admin/default/index');
  39. }
  40. }
  41. //生成图形验证码
  42. public function actionImgcode()
  43. {
  44. if(isset($_GET['getcode'])&&$_GET['getcode']==1)
  45. {
  46. $result = array('error'=>0,'msg'=>'','data'=>['scode'=>generatePwd(Session::getInitCls()->get(Yii::$app->params['capcha']))]);
  47. echo_json($result);
  48. }
  49. else
  50. {
  51. ob_end_clean();
  52. $_vc = new Capcha();//实例化一个对象
  53. if(isset($_GET['width']))$_vc->width = intval($_GET['width']);
  54. if(isset($_GET['height']))$_vc->height = intval($_GET['height']);
  55. $_vc->doimg();
  56. Session::getInitCls()->set(Yii::$app->params['capcha'],$_vc->getCode());
  57. Session::getInitCls()->set(Yii::$app->params['capchaTime'],TIMESTAMP);
  58. exit;
  59. }
  60. }
  61. //生成缩略图
  62. public function actionMakethumb()
  63. {
  64. $get = Yii::$app->request->get();
  65. $cut = isset($get['cut'])?intval($get['cut']):0;
  66. if($get['width']==$get['height'])$defaultType=1;//正方形
  67. if($get['width']>$get['height'])$defaultType=2;//宽屏
  68. if($get['width']<$get['height'])$defaultType=3;//竖屏
  69. $dstrootdir = THUMB_PATH.get_date(TIMESTAMP,'Ymd').DIRECTORY_SEPARATOR;
  70. if($get['hash']=='noavatar'||$get['hash']=='noimage')
  71. {
  72. $imageconfig = \app\modules\admin\models\Config::find()->where("name='imageconfig'")->one();
  73. $imageconfig = string2array($imageconfig->value);
  74. //默认缺省图
  75. $defaultImg = $imageconfig['noimage'.$defaultType];
  76. //默认缺省头像
  77. $defaultAvatar = $imageconfig['noavatar'];
  78. if(empty($defaultImg)||empty($defaultAvatar))
  79. {
  80. exit('请设置缺省图和缺省头像(设置入口:后台 > 系统设置 > 基础设置)');
  81. }
  82. $srcimg = $get['hash']=='noimage'?getFileUrl($defaultImg):getFileUrl($defaultAvatar);
  83. $dstimg = $dstrootdir.intval($get['width']).'_'.intval($get['height']).'_'.md5($srcimg).'.jpg';
  84. if(!file_exists($dstimg))
  85. {
  86. $resize = new \app\common\components\ResizeImage($srcimg, intval($get['width']), intval($get['height']), md5($srcimg),0,1,$dstrootdir);
  87. $dstimg = $resize->dstimg;
  88. }
  89. }
  90. else
  91. {
  92. $dstimg = $dstrootdir.intval($get['width']).'_'.intval($get['height']).'_'.$get['hash'].'.jpg';
  93. if(!file_exists($dstimg))
  94. {
  95. $img = Attachment::find()->where("hash='".$get['hash']."'")->limit(1)->one();
  96. if (!empty($img)){
  97. $srcimg =getFileUrl($img->file_path,Yii::$app->params['oss']['OPEN_INTERNAL']);
  98. $resize = new \app\common\components\ResizeImage($srcimg, intval($get['width']), intval($get['height']), $get['hash'], $cut,1,$dstrootdir);
  99. $dstimg = $resize->dstimg;
  100. }
  101. }
  102. }
  103. ob_end_clean();
  104. header('Content-type: image/jpeg');
  105. echo file_get_contents($dstimg);
  106. exit();
  107. }
  108. //webUploader上传
  109. public function actionUpload()
  110. {
  111. try {
  112. Yii::$app->response->format = Response::FORMAT_JSON;
  113. $model = new Upload();
  114. $info = $model->upImage();
  115. if ($info && is_array($info)) {
  116. return $info;
  117. } else {
  118. return ['code' => 1, 'msg' => 'error'];
  119. }
  120. } catch (\Exception $e) {
  121. return ['code' => 1, 'msg' => $e->getMessage()];
  122. }
  123. }
  124. }