yii framework 图片上传与缩略显示
时间:2010-06-03 来源:chalgcn
有关文件上传,Yii提供了一个很重要的类 CUploadedFile
在控制器里写一个图像上传的动作
public function actionUploadPhoto()
{
$model=$this->loadModel();
if(isset(Yii::app()->user->id))
{
if(isset($_POST['UserProfile']))
{
$model->attributes=$_POST['UserProfile'];
$form=CUploadedFile::getInstance($model, 'image');
if($form!==null)
{
$model->image=UserModule::imageName($form->name);
if($model->save())
{
$form->saveAs(UserModule::imageDir().$model->image);
Yii::app()->user->setFlash('uploadMessage', '图片上传成功');
$this->refresh();
}
}
}
$this->render('uploadimage', array('model'=>$model));
}
}
注意这里有一个UserModule下的imageName()方法,它是生成了一个随机的图片名存放到目录里。
接着缩略,
有关图像缩略图的生成,关系到一个header(),它必须在所有的html文件发送到浏览器之间执行。因为在我的别的控制器里有一个beforeAction()
public function beforeAction($action)
{
$this->layout=$this->module->layout;
return true;
}
好像它会关联到其它的控制器
这样在我的图像缩略的控制器里面也要写一个beforeAction()
public function beforeAction($action)
{
$this->layout='column1';
return true;
}
column1是一个空白文件或根本就不存在,
要是不为图像缩略专门建立一个控制器也可以,功能会实现,就是利用$_GET将图像名传入到处理缩略图像的文件中去,但那样会很死,它不会和yii发生什么关系了,一系列的参数要么写死,要么用$_GET传入。
好,我们建立一个图像缩略控制器
class ImageController extends Controller
{
private $_model;
public function beforeAction($action)
{
$this->layout='column1';
return true;
}
public function actionThumb()
{
$model = $this->loadModel();
$this->render('thumb', array('model'=>$model));
}
/**
* Returns the data model
* If the data model is not found, an HTTP exception will be raised.
*/
public function loadModel()
{
if($this->_model===null)
{
if(isset(Yii::app()->user->id))
{
$criteria=new CDbCriteria;
$criteria->select='image';
$criteria->condition='user_id=:user_id';
$criteria->params=array(':user_id'=>Yii::app()->user->id);
$this->_model=UserProfile::model()->find($criteria);
}
if($this->_model===null)
{
throw new CHttpException(404, '您请求的页面不存在');
}
}
return $this->_model;
}
}
不作解释,很简单:
它渲染了一个视图文件,它才是重点
<?php
$thumb=new Thumbnail(UserModule::IMAGE_WIDTH,UserModule::IMAGE_HEIGHT);
$thumb->loadFile(UserModule::imageDir().$model->image);
header('Content-Type: '.$thumb->getMime());
$thumb->buildThumb();
?>
一定要注意它的路径问题,是文件夹的路径,而不是URL,那么去UserModule下看一下imageDir()
public static function imageDir()
{
return basename(Yii::app()->basePath).'/modules/user/image/';
}
我们可能注意到了,图像上传过程中也用到了这个imageDir()。
同时,那个动作里面视图的文件路径也要写好,它默认会到views/image/下寻找视图文件。
这个Thumbnail是一个在models文件夹下的图像处理类
<?php
class Thumbnail
{
/*
* 最大的宽度像素值
*/
private $maxWidth;
/*
* 最大的高度像素值
*/
private $maxHeight;
/*
* 是否需要按比例缩放
*/
private $scale;
/*
* 是否放大以填充缩略图
*/
private $inflate;
/*
* 可以接收的MIME值
*/
private $types;
/*
* 存放根据不同的图像类型的函数名如imagecreatefromjpeg
*/
private $imgLoaders;
/*
* 同上,如imagejpeg
*/
private $imgCreators;
/*
* 源图像
*/
private $source;
/*
* 源图像的宽度
*/
private $sourceWidth;
/*
* 源图像的高度
*/
private $sourceHeight;
/*
* 源图像的MIME值
*/
private $sourceMime;
/*
* 缩略图像
*/
private $thumb;
/*
* 缩略图宽度
*/
private $thumbWidth;
/*
* 缩略图高度
*/
private $thumbHeight;
/*
* 构造函数
* param int 缩略图的最大宽度
* param int 缩略图的最大高度
* param boolean 是否按比例缩放
* param boolean 是否放大以填充缩略图
*/
public function __construct($maxWidth, $maxHeight, $scale=true, $inflate=true)
{
$this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight;
$this->scale = $scale;
$this->inflate = $inflate;
$this->types = array('image/jpeg', 'image/png','image/gif');
$this->imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif',
);
$this->imgCreators = array(
'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/gif' => 'imagegif',
);
}
public function loadFile($image)
{
if(!$dims = @getimagesize($image))
{
throw new Exception('找不到图像');
}
if(in_array($dims['mime'], $this->types))
{
$loader = $this->imgLoaders[$dims['mime']];
$this->source = $loader($image);
$this->sourceWidth = $dims[0];
$this->sourceHeight = $dims[1];
$this->sourceMime = $dims['mime'];
$this->initThumb();
return true;
}
else
{
throw new Exception('不支持'.$dims['mime'].'图像类型');
}
}
public function loadData($image, $mime)
{
if( in_array($mime, $this->types) )
{
if($this->source = @imagecreatefromstring($image))
{
$this->sourceWidth = imagesx($this->source);
$this->sourceHeight = imagesy($this->source);
$this->sourceMime = $mime;
$this->initThumb();
return true;
}
else
{
throw new Exception('不能从字符串中载入图像');
}
}
else
{
throw new Exception('不支持'.$mime.'图片类型');
}
}
private function initThumb()
{
if( $this->scale )
{
if($this->sourceWidth > $this->sourceHeight)
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
}
else if ( $this->sourceWidth < $this->sourceHeight )
{
$this->thumbHeight = $this->maxHeight;
$this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));
}
else
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
}
else
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
if( $this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == false)
{
$this->thumb = $this->source;
}
else
{
imagecopyresampled( $this->thumb, $this->source, 0, 0, 0, 0,
$this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);
}
}
public function buildThumb($file = null)
{
$creator = $this->imgCreators[$this->sourceMime];
if(isset($file))
{
return $creator($this->thumb, $file);
}
else
{
return $creator($this->thumb);
}
}
public function getMime()
{
return $this->sourceMime;
}
public function getThumbWidth()
{
return $this->thumbWidth;
}
public function getThumbHeight()
{
return $this->thumbHeight;
}
}
?>
在控制器里写一个图像上传的动作
public function actionUploadPhoto()
{
$model=$this->loadModel();
if(isset(Yii::app()->user->id))
{
if(isset($_POST['UserProfile']))
{
$model->attributes=$_POST['UserProfile'];
$form=CUploadedFile::getInstance($model, 'image');
if($form!==null)
{
$model->image=UserModule::imageName($form->name);
if($model->save())
{
$form->saveAs(UserModule::imageDir().$model->image);
Yii::app()->user->setFlash('uploadMessage', '图片上传成功');
$this->refresh();
}
}
}
$this->render('uploadimage', array('model'=>$model));
}
}
注意这里有一个UserModule下的imageName()方法,它是生成了一个随机的图片名存放到目录里。
接着缩略,
有关图像缩略图的生成,关系到一个header(),它必须在所有的html文件发送到浏览器之间执行。因为在我的别的控制器里有一个beforeAction()
public function beforeAction($action)
{
$this->layout=$this->module->layout;
return true;
}
好像它会关联到其它的控制器
这样在我的图像缩略的控制器里面也要写一个beforeAction()
public function beforeAction($action)
{
$this->layout='column1';
return true;
}
column1是一个空白文件或根本就不存在,
要是不为图像缩略专门建立一个控制器也可以,功能会实现,就是利用$_GET将图像名传入到处理缩略图像的文件中去,但那样会很死,它不会和yii发生什么关系了,一系列的参数要么写死,要么用$_GET传入。
好,我们建立一个图像缩略控制器
class ImageController extends Controller
{
private $_model;
public function beforeAction($action)
{
$this->layout='column1';
return true;
}
public function actionThumb()
{
$model = $this->loadModel();
$this->render('thumb', array('model'=>$model));
}
/**
* Returns the data model
* If the data model is not found, an HTTP exception will be raised.
*/
public function loadModel()
{
if($this->_model===null)
{
if(isset(Yii::app()->user->id))
{
$criteria=new CDbCriteria;
$criteria->select='image';
$criteria->condition='user_id=:user_id';
$criteria->params=array(':user_id'=>Yii::app()->user->id);
$this->_model=UserProfile::model()->find($criteria);
}
if($this->_model===null)
{
throw new CHttpException(404, '您请求的页面不存在');
}
}
return $this->_model;
}
}
不作解释,很简单:
它渲染了一个视图文件,它才是重点
<?php
$thumb=new Thumbnail(UserModule::IMAGE_WIDTH,UserModule::IMAGE_HEIGHT);
$thumb->loadFile(UserModule::imageDir().$model->image);
header('Content-Type: '.$thumb->getMime());
$thumb->buildThumb();
?>
一定要注意它的路径问题,是文件夹的路径,而不是URL,那么去UserModule下看一下imageDir()
public static function imageDir()
{
return basename(Yii::app()->basePath).'/modules/user/image/';
}
我们可能注意到了,图像上传过程中也用到了这个imageDir()。
同时,那个动作里面视图的文件路径也要写好,它默认会到views/image/下寻找视图文件。
这个Thumbnail是一个在models文件夹下的图像处理类
<?php
class Thumbnail
{
/*
* 最大的宽度像素值
*/
private $maxWidth;
/*
* 最大的高度像素值
*/
private $maxHeight;
/*
* 是否需要按比例缩放
*/
private $scale;
/*
* 是否放大以填充缩略图
*/
private $inflate;
/*
* 可以接收的MIME值
*/
private $types;
/*
* 存放根据不同的图像类型的函数名如imagecreatefromjpeg
*/
private $imgLoaders;
/*
* 同上,如imagejpeg
*/
private $imgCreators;
/*
* 源图像
*/
private $source;
/*
* 源图像的宽度
*/
private $sourceWidth;
/*
* 源图像的高度
*/
private $sourceHeight;
/*
* 源图像的MIME值
*/
private $sourceMime;
/*
* 缩略图像
*/
private $thumb;
/*
* 缩略图宽度
*/
private $thumbWidth;
/*
* 缩略图高度
*/
private $thumbHeight;
/*
* 构造函数
* param int 缩略图的最大宽度
* param int 缩略图的最大高度
* param boolean 是否按比例缩放
* param boolean 是否放大以填充缩略图
*/
public function __construct($maxWidth, $maxHeight, $scale=true, $inflate=true)
{
$this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight;
$this->scale = $scale;
$this->inflate = $inflate;
$this->types = array('image/jpeg', 'image/png','image/gif');
$this->imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif',
);
$this->imgCreators = array(
'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/gif' => 'imagegif',
);
}
public function loadFile($image)
{
if(!$dims = @getimagesize($image))
{
throw new Exception('找不到图像');
}
if(in_array($dims['mime'], $this->types))
{
$loader = $this->imgLoaders[$dims['mime']];
$this->source = $loader($image);
$this->sourceWidth = $dims[0];
$this->sourceHeight = $dims[1];
$this->sourceMime = $dims['mime'];
$this->initThumb();
return true;
}
else
{
throw new Exception('不支持'.$dims['mime'].'图像类型');
}
}
public function loadData($image, $mime)
{
if( in_array($mime, $this->types) )
{
if($this->source = @imagecreatefromstring($image))
{
$this->sourceWidth = imagesx($this->source);
$this->sourceHeight = imagesy($this->source);
$this->sourceMime = $mime;
$this->initThumb();
return true;
}
else
{
throw new Exception('不能从字符串中载入图像');
}
}
else
{
throw new Exception('不支持'.$mime.'图片类型');
}
}
private function initThumb()
{
if( $this->scale )
{
if($this->sourceWidth > $this->sourceHeight)
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
}
else if ( $this->sourceWidth < $this->sourceHeight )
{
$this->thumbHeight = $this->maxHeight;
$this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));
}
else
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
}
else
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
if( $this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == false)
{
$this->thumb = $this->source;
}
else
{
imagecopyresampled( $this->thumb, $this->source, 0, 0, 0, 0,
$this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);
}
}
public function buildThumb($file = null)
{
$creator = $this->imgCreators[$this->sourceMime];
if(isset($file))
{
return $creator($this->thumb, $file);
}
else
{
return $creator($this->thumb);
}
}
public function getMime()
{
return $this->sourceMime;
}
public function getThumbWidth()
{
return $this->thumbWidth;
}
public function getThumbHeight()
{
return $this->thumbHeight;
}
}
?>
相关阅读 更多 +