博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii图片上传
阅读量:6868 次
发布时间:2019-06-26

本文共 3530 字,大约阅读时间需要 11 分钟。

首先感谢这里的博主,提供了思路,不过在调用 $model->b_image->extensionName 这个时,总提示错误,调呀调的,调烦了,就自己写一个吧。

其他不多说,看步骤

一、下载yii官方提供的图片处理扩展  具体配置跟官方的一样,这个扩展提供了两套调用方式,我选择了第一种,就是在配置文件里进行添加组件的方式,来张图,有图有真象哈

二、在protected目录下新建一utils目录,在目录下新建Upload.php文件,代码如下

request->baseUrl.'images/uploads/'.$name; //图片处理 $image = Yii::app()->image->load($tmp_name); $image->resize($width, $height)->rotate(0)->quality(100)->sharpen(20); $image->save($path); // move_uploaded_file($tmp_name,$path); return $path; } }}

三、在 protected/config/main.php 这个文件里,把刚才的utils目录载入

'import'=>array(

'application.models.*',
'application.components.*',
'application.extensions.*',
'application.helpers.*',
'application.utils.*',
'application.modules.boss.modules.srbac.controllers.SBaseController',
),

四、写代码

model部分

添加图片验证规则,其他规则根据自己需要进行添加,这里我只添加需要展示的代码(views和controller一样,不再说明了)

public function rules()	{		return array(			array('b_image', 'file', 'types' => 'jpg,gif,png', 'on' => 'insert'),		);	}

views

beginWidget('CActiveForm', array('id'=>'iot-books-form',// Please note: When you enable ajax validation, make sure the corresponding// controller action is handling ajax validation correctly.// There is a call to performAjaxValidation() commented in generated controller code.// See class documentation of CActiveForm for details on this.'enableAjaxValidation'=>false,'htmlOptions'=>array('enctype'=>'multipart/form-data'),//这个一定要加,否则获取不到图片)); ?>
labelEx($model,'b_image'); ?>
isNewRecord){ ?>
60,'maxlength'=>255)); ?>
error($model,'b_image'); ?>

controller

/**	 * Creates a new model.	 * If creation is successful, the browser will be redirected to the 'view' page.	 */	public function actionCreate()	{		$model=new IotBooks;		// Uncomment the following line if AJAX validation is needed		// $this->performAjaxValidation($model);		if(isset($_POST['IotBooks']))		{			$imgUrl = '';			if($_FILES){				$name = $_FILES['IotBooks']['name']['b_image']; //上传图片原名				$type = $_FILES['IotBooks']['type']['b_image']; //上传图片mime类型				$tmp_name = $_FILES['IotBooks']['tmp_name']['b_image']; //上传图片临时存放位置				$width = 100; //缩略图宽				$height = 100; //缩略图高				$imgUrl = Upload::createImageLink($name, $type, $tmp_name, $width, $height);			}			$model->attributes = $_POST['IotBooks'];			$model->b_image = $imgUrl;			if($model->save())				$this->redirect(array('view','id'=>$model->bid));		}		$this->render('create',array(			'model'=>$model,		));	}	/**	 * Updates a particular model.	 * If update is successful, the browser will be redirected to the 'view' page.	 * @param integer $id the ID of the model to be updated	 */	public function actionUpdate($id)	{		$model=$this->loadModel($id);		// Uncomment the following line if AJAX validation is needed		// $this->performAjaxValidation($model);		if(isset($_POST['IotBooks']))		{				$imgUrl = $model->b_image;			if($_FILES && ($_FILES['IotBooks']['name']['b_image'] != '')){				$name = $_FILES['IotBooks']['name']['b_image']; //上传图片原名				$type = $_FILES['IotBooks']['type']['b_image']; //上传图片mime类型				$tmp_name = $_FILES['IotBooks']['tmp_name']['b_image']; //上传图片临时存放位置				$width = 100; //缩略图宽				$height = 100; //缩略图高				$imgUrl = Upload::createImageLink($name, $type, $tmp_name, $width,$height);			}			$model->attributes = $_POST['IotBooks'];			$model->b_image = $imgUrl;			if($model->save())				$this->redirect(array('view','id'=>$model->bid));		}		$this->render('update',array(			'model'=>$model,		));	}

完工了,试一下吧

 

 

 

转载于:https://www.cnblogs.com/debmzhang/p/3403483.html

你可能感兴趣的文章
Applicatin Loader 3.0 安装步骤
查看>>
XenDesktop 5 修改XML端口
查看>>
【 58沈剑 架构师之路】究竟啥才是互联网架构“高并发”
查看>>
ionic 自动生成APP图标与启动界面
查看>>
Sql存储过程
查看>>
dsquery命令查询总结
查看>>
利用openssl API进行简单网络编程
查看>>
过度加班? - 是该到了认真考虑的时候了。
查看>>
linux启动出问题
查看>>
linux查看哪些进程占用swap空间
查看>>
VS_断点无效
查看>>
关于“无敌删除命令”
查看>>
017 搭建eureka注册中心
查看>>
nis服务器搭建
查看>>
红帽企业存储管理之DRBD应用详解
查看>>
Linux下mail服务器架构之源码实现postfix邮件基本功能
查看>>
Bios加密
查看>>
Apache 服务+ AWStat分析系统的应用
查看>>
前端技术学习之选择器(六)
查看>>
使用 Docker 搭建 Tomcat 运行环境
查看>>