蛋黄派 2015-03-02 09:29:39 12425次浏览 1条评论 10 1 0

_form.php

<?php $form = ActiveForm::begin([ 'options' => [ 'enctype' => 'multipart/form-data' ]]); ?>//上传图片必须设置的参数
<?php
    echo $form->field($model, 'logo_url')->widget(FileInput::classname(), [
        'options' => ['accept' => 'image/*'],
    ] );
?>

控制器

public function qiniu($imageUploadFile){
    $typeUrl = 'event/';
    $helper = new Helper();
    $saveUrl = $helper->saveImage($imageUploadFile, '', '', $typeUrl);
    //            var_dump($saveUrl);exit;
    $imageUrl = Yii::$app->getBasePath() .'/'.$saveUrl;

    $ret = $helper->qiniu($imageUrl,$saveUrl);
    //            Yii::error(  json_encode("错误:".$ret,JSON_UNESCAPED_UNICODE) ) ;
    unlink($imageUrl);
    if(!isset($ret['key']) || empty($ret['key'])){
        throw new LogicErrorHttpException("上传图片失败", $this->error);
    }
    return $saveUrl;
}
/**
* Creates a new EventInfo model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
    $model = new EventInfo();

    $model->user_info_id = 312;

    if ($model->load(Yii::$app->request->post())) {

        $this->setFormatData($model, Yii::$app->request->post());

        //var_dump($_FILES);exit;
        $imageUploadFile =   UploadedFile::getInstance($model,'logo_url');
        if($imageUploadFile != null ){
            //$imageUploadFile = UploadedFile::getInstanceByName('EventInfo[logo_url]');
            $saveUrl = $this->qiniu($imageUploadFile);
            $model->logo_url = $saveUrl;
        }
        $mPicUrlInstance =   UploadedFile::getInstance($model,'m_pic_url');
        if($mPicUrlInstance != null ){
            // $imageUploadFile = UploadedFile::getInstanceByName('EventInfo[m_pic_url]');
            $saveUrl = $this->qiniu( $mPicUrlInstance );
            $model->m_pic_url = $saveUrl;
        }
        if ($model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
        else
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

helper

/**
* 通过url上传图片到七牛
* @param $profileImage
* @param $userPhoto
*/
public function qiniu($profileImageUrl,$userPhoto){
    $imageUrl = (string)$profileImageUrl;//头像地址链接
    $imageName = (string)$userPhoto;//图片文件拼接名称,如:user_photo/2015/01/30/1422587704_640x194.png
    //七牛的密钥、域名和空间
    $ak = Yii::$app->params['ak'];
    $sk = Yii::$app->params['sk'];
    $domain = Yii::$app->params['qiniu_domain'];
    $bucket = Yii::$app->params['qiniu_bucket'];
    $qiNiu = new Qiniu($ak, $sk,$domain, $bucket);//登入到七牛
    $ret = $qiNiu->uploadByUrl($imageUrl,$imageName);
    //$url = $qiNiu->getLink($imageName);//获取已上传到七牛的图片url
    return $ret;
}

/**
 *
 * @param type $imageUploadInstance UploadedFile::getInstance
 * @return string | null
 */
public static function saveImage($imageUploadInstance, $width, $height, $type)
{
    if ($imageUploadInstance == null)
    {
        return null;
    }
    $imageFileExt = strtolower($imageUploadInstance->getExtension());
    $save_path    = Yii::$app->getBasePath().'/'.$type;
    if (!file_exists($save_path))
    {
        mkdir($save_path, 0777, true);
    }
    $ymd = date("Y/md");
    $save_path .= $ymd . '/';
    if (!file_exists($save_path))
    {
        mkdir($save_path, 0777, true);
    }
    $img_prefix    = date("YmdHis") . '_' . rand(10000, 99999);
    $imageFileName = $img_prefix . '.' . $imageFileExt;
    $save_path .= $imageFileName;
    $imageUploadInstance->saveAs($save_path);
    //$obj = Image::thumbnail($save_path, $width, $height)->save($save_path);
    return  $type.$ymd . '/'. $imageFileName;
}
觉得很赞
您需要登录后才可以评论。登录 | 立即注册