标签:style blog http color os io 文件 数据
用yii自带的CUploadfile进行图片的上传,因为这个类只提供上传的功能,并没有图片的处理功能比如图片的缩减、锐化、旋转等,所以必须借助yii的扩展image来实现。
一、图片上传
数据表:
CREATE TABLE `img_show` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(150) DEFAULT NULL, `url` varchar(100) DEFAULT NULL, `img_path` varchar(150) DEFAULT NULL, `handle_img_path` varchar(150) DEFAULT NULL, `add_time` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
用gii生成对应的model、curd和controller,更改如下:
model:
public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array(‘add_time‘, ‘numerical‘, ‘integerOnly‘=>true), array(‘title, img_path, handle_img_path‘, ‘length‘, ‘max‘=>150), array(‘url‘, ‘length‘, ‘max‘=>100), array(‘img_path‘, ‘file‘, //定义为file类型 // ‘allowEmpty‘=>true, ‘types‘=>‘jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx‘, //上传文件的类型 ‘maxSize‘=>1024*1024*10, //上传大小限制,注意不是php.ini中的上传文件大小 ‘tooLarge‘=>‘文件大于10M,上传失败!请上传小于10M的文件!‘ ), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array(‘id, title, url, img_path, handle_img_path, add_time‘, ‘safe‘, ‘on‘=>‘search‘), ); }
controller:
public function actionCreate() { $model=new ImgShow; if(isset($_POST[‘ImgShow‘])) { $model->attributes=$_POST[‘ImgShow‘]; $model->add_time = time(); //获取一个CUploadfile的实例 $file = CUploadedFile::getInstance($model,‘img_path‘); //判断实例化是否成功 将图片重命名 if(is_object($file)&&get_class($file) === ‘CUploadedFile‘){ $model->img_path = ‘./images/upfile/file‘.time().‘_‘.rand(0,9999).‘.‘.$file->extensionName; $file->saveAs($model->img_path); } //对图片进行处理 /* $image = Yii::app()->image->load($model->img_path); $image->resize(600, 300)->rotate(-45)->quality(75)->sharpen(20); $model->handle_img_path = ‘./images/upfile/file‘.time().‘_‘.rand(0,9999).‘_small‘.‘.‘.$file->extensionName; $image->save($model->handle_img_path);*/ if($model->save()) $this->redirect(array(‘view‘,‘id‘=>$model->id)); } $this->render(‘create‘,array( ‘model‘=>$model, )); }
views:
<div class="form"> <?php $form=$this->beginWidget(‘CActiveForm‘, array( ‘id‘=>‘img-show-form‘, ‘enableAjaxValidation‘=>false, ‘htmlOptions‘=>array(‘enctype‘=>‘multipart/form-data‘), )); ?> <p>字段带<span class="required">*</span> 的为必填项.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,‘title‘); ?> <?php echo $form->textField($model,‘title‘,array(‘size‘=>60,‘maxlength‘=>150)); ?> <?php echo $form->error($model,‘title‘); ?> </div> <div class="row"> <?php echo $form->labelEx($model,‘url‘); ?> <?php echo $form->textField($model,‘url‘,array(‘size‘=>60,‘maxlength‘=>100)); ?> <?php echo $form->error($model,‘url‘); ?> </div> <div class="row"> <?php echo $form->labelEx($model,‘img_path‘); ?> <!-- --><?php //echo $form->textField($model,‘img_path‘,array(‘size‘=>60,‘maxlength‘=>150)); ?> <?php echo CHtml::activeFileField($model,‘img_path‘,array(‘size‘=>60,‘maxlength‘=>255)); ?> <?php echo $form->error($model,‘img_path‘); ?> </div> <!--<div class="row"> <?php /*echo $form->labelEx($model,‘handle_img_path‘); */?> <?php /*echo $form->textField($model,‘handle_img_path‘,array(‘size‘=>60,‘maxlength‘=>150)); */?> <?php /*echo $form->error($model,‘handle_img_path‘); */?> </div>--> <!--<div class="row"> <?php /*echo $form->labelEx($model,‘add_time‘); */?> <?php /*echo $form->textField($model,‘add_time‘); */?> <?php /*echo $form->error($model,‘add_time‘); */?> </div>--> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? ‘新增‘ : ‘更新‘); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->
在protect目录下新建文件夹:images/upfile 这样就可以上传图片了。
二、图片处理
下载image扩展:http://www.yiiframework.com/extension/image#hh1
按其文档进行相应的配置,将解压后后的文件夹image放在exetension下,将helper放在protected下,在main.php中配置如下:
‘import‘=>array( ‘application.models.*‘, ‘application.components.*‘, ‘application.helpers.*‘, ), // application components ‘components‘=>array( ‘image‘=>array( ‘class‘=>‘application.extensions.image.CImageComponent‘, // GD or ImageMagick ‘driver‘=>‘GD‘, // ImageMagick setup path ‘params‘=>array(‘directory‘=>‘/opt/local/bin‘), ),
配置完毕后,打开上面controller下的红色的注释部分,这样就可以对图片进行处理了。
标签:style blog http color os io 文件 数据
原文地址:http://www.cnblogs.com/webclz/p/3919409.html