Yii框架實(shí)現(xiàn)圖片上傳的方法詳解
本文實(shí)例講述了Yii框架實(shí)現(xiàn)圖片上傳的方法。分享給大家供大家參考,具體如下:
今天在網(wǎng)上看了下有關(guān)圖片上傳的教程,歷經(jīng)挫折才調(diào)試好,現(xiàn)在把相關(guān)代碼及其說明貼出來,以供初次使用的朋友們參考。
Model:
<?php
class Upload extends CActiveRecord {
public $image;
public static function model($className = __CLASS__) {
return $className;
}
public function tableName() {
return '{{resource}}';
}
public function rules() {
return array(
array('image', 'file', 'types'=>'jpg, gif, png')
);
}
}
注:resource為數(shù)據(jù)表,表前綴可在main.php內(nèi)設(shè)置,相信朋友們在看到文件上傳時(shí)應(yīng)該熟悉了main.php位置在哪及運(yùn)作機(jī)制。
Controller:
<?php
class UploadController extends Controller {
public function actionIndex() {
$model=new Upload;
if(isset($_POST['Upload'])) {
$model->image=CUploadedFile::getInstance($model,'image');
$ext = $model->image->getExtensionName();
$fileName = uniqid() . '.' . $ext;
$model->image->saveAs('assets/' . $fileName);
}
$this->renderPartial('index', array('model'=>$model));
}
}
注:saveAs里面是存放圖片上傳后的地址,追蹤下代碼可以發(fā)現(xiàn),該參數(shù)是move_uploaded_file函數(shù)的第二個(gè)參數(shù),一定得是文件名。
View:
<meta charset="utf-8">
<?php echo CHtml::form(SITE_URL . 'admin/upload/index','post',array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::activeFileField($model, 'image'); ?>
<?php echo CHtml::submitButton('提交');?>
<?php echo CHtml::endForm(); ?>
注:上面的SITE_URL為項(xiàng)目定義的常量,也就是項(xiàng)目的網(wǎng)址
相信經(jīng)過上述步驟,朋友們應(yīng)該可以上傳成功圖片,而且在項(xiàng)目下的assets目錄下找到上傳的圖片。因?yàn)榘l(fā)現(xiàn)yii沒有縮略圖的方法,于是把thinkphp縮略圖的方法整合了進(jìn)來,把下面代碼保存為Image.php放在項(xiàng)目下的protected/extensions目錄下
<?php
class Image extends CController {
/**
+----------------------------------------------------------
* 取得圖像信息
*
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $image 圖像文件名
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
static function getImageInfo($img) {
$imageInfo = getimagesize($img);
if ($imageInfo !== false) {
$imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
$imageSize = filesize($img);
$info = array(
"width" => $imageInfo[0],
"height" => $imageInfo[1],
"type" => $imageType,
"size" => $imageSize,
"mime" => $imageInfo['mime']
);
return $info;
} else {
return false;
}
}
/**
+----------------------------------------------------------
* 生成縮略圖
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $image 原圖
* @param string $type 圖像格式
* @param string $thumbname 縮略圖文件名
* @param string $maxWidth 寬度
* @param string $maxHeight 高度
* @param string $position 縮略圖保存目錄
* @param boolean $interlace 啟用隔行掃描
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
// 獲取原圖信息
$info = Image::getImageInfo($image);
if ($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type) ? $info['type'] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計(jì)算縮放比例
if ($scale >= 1) {
// 超過原圖大小不再縮略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 縮略圖尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 載入原圖
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
if(!function_exists($createFun)) {
return false;
}
$srcImg = $createFun($image);
//創(chuàng)建縮略圖
if ($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
//png和gif的透明處理 by luofei614
if('png'==$type){
imagealphablending($thumbImg, false);//取消默認(rèn)的混色模式(為解決陰影為綠色的問題)
imagesavealpha($thumbImg,true);//設(shè)定保存完整的 alpha 通道信息(為解決陰影為綠色的問題)
}elseif('gif'==$type){
$trnprt_indx = imagecolortransparent($srcImg);
if ($trnprt_indx >= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
$trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($thumbImg, 0, 0, $trnprt_indx);
imagecolortransparent($thumbImg, $trnprt_indx);
}
}
// 復(fù)制圖片
if (function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
// 對jpeg圖形設(shè)置隔行掃描
if ('jpg' == $type || 'jpeg' == $type)
imageinterlace($thumbImg, $interlace);
// 生成圖片
$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$imageFun($thumbImg, $thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
}
?>
再在項(xiàng)目下的protected/config/main.php中import字段加上
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.*', #加上此行,意思為自動載入
),
再上面的Controller加上
public function actionIndex() {
$model=new Upload;
if(isset($_POST['Upload'])) {
$model->image=CUploadedFile::getInstance($model,'image');
$ext = $model->image->getExtensionName();
$fileName = uniqid() . '.' . $ext;
$model->image->saveAs('assets/' . $fileName);
// 生成縮略圖
Image::thumb('assets/' . $fileName, 'assets/' . uniqid() . '.' . $ext);
}
$this->renderPartial('index', array('model'=>$model));
}
這次就完整了。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- Yii編程開發(fā)常見調(diào)用技巧集錦
- Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫關(guān)聯(lián)查詢排序功能代碼
- Yii2實(shí)現(xiàn)多域名跨域同步登錄退出
- Yii2下session跨域名共存的解決方案
- Yii基于數(shù)組和對象的Model查詢技巧實(shí)例詳解
- Yii學(xué)習(xí)總結(jié)之?dāng)?shù)據(jù)訪問對象 (DAO)
- Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能示例
- Yii框架分頁實(shí)現(xiàn)方法詳解
- Yii框架使用魔術(shù)方法實(shí)現(xiàn)跨文件調(diào)用功能示例
相關(guān)文章
PHP CURL采集百度搜尋結(jié)果圖片不顯示問題的解決方法
這篇文章主要介紹了PHP CURL采集百度搜尋結(jié)果圖片不顯示問題的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
ThinkPHP實(shí)現(xiàn)非標(biāo)準(zhǔn)名稱數(shù)據(jù)表快速創(chuàng)建模型的方法
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)非標(biāo)準(zhǔn)名稱數(shù)據(jù)表快速創(chuàng)建模型的方法,對于采用ThinkPHP操作非標(biāo)準(zhǔn)名稱數(shù)據(jù)表的情況非常實(shí)用,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
免費(fèi)的ip數(shù)據(jù)庫淘寶IP地址庫簡介和PHP調(diào)用實(shí)例
淘寶ip地址庫提供免費(fèi)的ip信息查詢,可查詢ip所在國家、區(qū)域、省份、城市及運(yùn)營商等信息。不需要認(rèn)證不需要注冊,唯一的小缺陷就是限制美妙查詢頻率不能高于10次。2014-04-04

