yii2.0上传图片

时间:2022-10-20 20:41:51

model:

 1 use Yii;
 2 public $imageUpload;
 3 public function rules()
 4 {
 5     return [
 6     [['imageUpload'], 'image', 'extensions' => 'png, jpg, jpeg', 'minWidth' => 200, 'maxWidth' => 1000, 'minHeight' => 200, 'maxHeight' => 1000, 'maxSize' => 1024 * 1024],
 7    ];
 8 }
 9 
10 public function attributeLabels()
11 {
12     return [
13     'imageUpload' => '头像',
14   ];
15 }
16 /**
17  * 获取头像url地址
18  * @return 头像的url地址
19  */
20 public function getPortraitUrl()
21 {
22     return self::getAvatarUrlById($this->id);
23 }
24 
25 /**
26  * 获取头像存放路径
27  * @return 头像的存放路径
28  */
29 public static function getAvatarPathById($companyId)
30 {
31     $string = str_pad($companyId, 6, '0', STR_PAD_LEFT);
32   //头像的存放路径
33     $path = 'company/' . substr($string, 0, -4) . '/' . substr($string, -4, 2) . '/' . substr($string, -2, 2) . image_type_to_extension(IMAGETYPE_JPEG);;
34     return $path;
35 }
36 
37 /**
38  * 通过用户ID获取头像url地址
39  */
40 public static function getAvatarUrlById($companyId)
41 {
42     $path = self::getAvatarPathById($companyId);
43     $file = Yii::getAlias('@uploadPath/' . $path);
44     if (file_exists($file)) {
45         $makeTime = filemtime($file);
46         return Yii::getAlias('@uploadUrl/' . $path) . '?' . $makeTime;
47     }
48     return Yii::getAlias('@resUrl/' . self::DEFAULT_AVATAR);
49 }

 

控制器显示:
 1 use Yii;
 2 use app\models\Company;
 3 use yii\helpers\FileHelper;
 4 use yii\web\Controller;
 5 use yii\web\UploadedFile;
 6 //use  yii\imageine'Image;  //需要通过composer引入
 7 public function actionProfile()
 8 {
 9     $id = Yii::$app->session['companyId'];//得到id
10     $model = Company::findOne($id);
11     $imageUpload = UploadedFile::getInstance($model, 'imageUpload');//返回一个上传文件
12     if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {//判断是否post提交,加载数据
13         if ($imageUpload && $model->validate(['imageUpload'])) {//数据验证
14             $path =  Company::getAvatarPathById($id);//图片的路径别名
15             $avatarImage = Yii::getAlias('@uploadPath/' . $path);//将路径别名转化为实际的路径
16             FileHelper::createDirectory(dirname(Yii::getAlias($avatarImage)));//创建目录
17             $imageUpload->saveAs(Yii::getAlias($avatarImage));//保存图片
18  //   Image::thumbnail($avatarImage, 300, 300)->save($avatarImage);  //将保存的图片改成300*300像素再保存到网页上
19         }
20         if($model->save()) {
21             Yii::$app->session['companyName'] = $model->name;
22         }
23     }
24     return$this->render('profile', ['model' => $model]);
25 }

 

视图显示:
 1 use app\models\Company;
 2 <?php $form = ActiveForm::begin([     /*表单开始*/
 3     'options' => ['enctype' => 'multipart/form-data']   //文件上传必须加
 4 ]); ?>
 5 <?= $form->field($model, 'imageUpload')->fileInput(['maxlength' => true])->hint('建议上传尺寸:100*100--300*300') ?>
 6 <?= $form->field($model, 'imageUpload', ['inputTemplate'=>'<img src="'.\app\models\Company::getAvatarUrlById($model->id).'" width="100px" height="100px" />'])->staticControl() ?>
 7 <div class="form-group">
 8     <div class="col-sm-offset-2 col-sm-6">
 9         <?= Html::submitButton('修改', ['class' => 'btn btn-green btn-sm']) ?>    <!-- 显示提交按钮 -->
10     </div>
11 </div>
12 <?php ActiveForm::end(); ?>   <!-- 表单结束 -->
 
结果显示:
网页显示
yii2.0上传图片
本地显示 (当前ID.jpeg)

yii2.0上传图片