• yii2.0上传图片


    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 appmodelsCompany;
     3 use yiihelpersFileHelper;
     4 use yiiwebController;
     5 use yiiwebUploadedFile;
     6 //use  yiiimageine'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 appmodelsCompany;
     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="'.appmodelsCompany::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(); ?>   <!-- 表单结束 -->
    
    
    结果显示:
    网页显示

    本地显示 (当前ID.jpeg)

  • 相关阅读:
    【BZOJ 2916】 2916: [Poi1997]Monochromatic Triangles (容斥)
    【BZOJ 2024】 2024: [SHOI2009] 舞会 (容斥原理+高精度)
    【BZOJ 3235】 3235: [Ahoi2013]好方的蛇 (单调栈+容斥原理)
    【BZOJ 4710】 4710: [Jsoi2011]分特产 (容斥原理)
    【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)
    【BZOJ 3812】 3812: 主旋律 (容斥原理**)
    【BZOJ 2839】 2839: 集合计数 (容斥原理)
    POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)
    POJ2533——Longest Ordered Subsequence(简单的DP)
    POJ2531——Network Saboteur(随机化算法水一发)
  • 原文地址:https://www.cnblogs.com/l-zl/p/6410453.html
Copyright © 2020-2023  润新知