国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

利用Yii框架實(shí)現(xiàn)圖片上傳

2024-05-04 21:50:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Yii框架實(shí)現(xiàn)圖片上傳的方法。分享給大家供大家參考,具體如下:

今天在網(wǎng)上看了下有關(guān)圖片上傳的教程,歷經(jīng)挫折才調(diào)試好,現(xiàn)在把相關(guān)代碼及其說(shuō)明貼出來(lái),以供初次使用的朋友們參考。

Model:

  1. classUploadextendsCActiveRecord { 
  2.  
  3.   public$image
  4.  
  5.   publicstaticfunctionmodel($className=__CLASS__) { 
  6.  
  7.     return$className
  8.  
  9.   } 
  10.  
  11.   publicfunctiontableName() { 
  12.  
  13.     return'{{resource}}' 
  14.  
  15.   } 
  16.  
  17.   publicfunctionrules() { 
  18.  
  19.     returnarray( 
  20.  
  21.       array('image','file','types'=>'jpg, gif, png'
  22.  
  23.     ); 
  24.  
  25.   } 
  26.  

注:resource為數(shù)據(jù)表,表前綴可在main.php內(nèi)設(shè)置,相信朋友們?cè)诳吹轿募蟼鲿r(shí)應(yīng)該熟悉了main.php位置在哪及運(yùn)作機(jī)制。

Controller:

  1. classUploadControllerextendsController { 
  2.  
  3.   publicfunctionactionIndex() { 
  4.  
  5.     $model=newUpload; 
  6.  
  7.     if(isset($_POST['Upload'])) { 
  8.  
  9.       $model->image=CUploadedFile::getInstance($model,'image'); 
  10.  
  11.       $ext=$model->image->getExtensionName(); 
  12.  
  13.       $fileName= uniqid() .'.'.$ext
  14.  
  15.       $model->image->saveAs('assets/'.$fileName); 
  16.  
  17.     } 
  18.  
  19.     $this->renderPartial('index',array('model'=>$model)); 
  20.  
  21.   } 
  22.  

注:saveAs里面是存放圖片上傳后的地址,追蹤下代碼可以發(fā)現(xiàn),該參數(shù)是move_uploaded_file函數(shù)的第二個(gè)參數(shù),一定得是文件名。

View:

'multipart/form-data'));

注:上面的SITE_URL為項(xiàng)目定義的常量,也就是項(xiàng)目的網(wǎng)址

相信經(jīng)過(guò)上述步驟,朋友們應(yīng)該可以上傳成功圖片,而且在項(xiàng)目下的assets目錄下找到上傳的圖片。因?yàn)榘l(fā)現(xiàn)yii沒(méi)有縮略圖的方法,于是把thinkphp縮略圖的方法整合了進(jìn)來(lái),把下面代碼保存為Image.php放在項(xiàng)目下的protected/extensions目錄下.

  1. classImageextendsCController { 
  2.  
  3.   /** 
  4.  
  5.    +---------------------------------------------------------- 
  6.  
  7.    * 取得圖像信息 
  8.  
  9.    * 
  10.  
  11.    +---------------------------------------------------------- 
  12.  
  13.    * @static 
  14.  
  15.    * @access public 
  16.  
  17.    +---------------------------------------------------------- 
  18.  
  19.    * @param string $image 圖像文件名 
  20.  
  21.    +---------------------------------------------------------- 
  22.  
  23.    * @return mixed 
  24.  
  25.    +---------------------------------------------------------- 
  26.  
  27.    */ 
  28.  
  29.   staticfunctiongetImageInfo($img) { 
  30.  
  31.     $imageInfo=getimagesize($img); 
  32.  
  33.     if($imageInfo!== false) { 
  34.  
  35.       $imageType=strtolower(substr(image_type_to_extension($imageInfo[2]), 1)); 
  36.  
  37.       $imageSize=filesize($img); 
  38.  
  39.       $info=array
  40.  
  41.         "width"=>$imageInfo[0], 
  42.  
  43.         "height"=>$imageInfo[1], 
  44.  
  45.         "type"=>$imageType
  46.  
  47.         "size"=>$imageSize
  48.  
  49.         "mime"=>$imageInfo['mime'
  50.  
  51.       ); 
  52.  
  53.       return$info
  54.  
  55.     }else
  56.  
  57.       returnfalse; 
  58.  
  59.     } 
  60.  
  61.   } 
  62.  
  63.   /** 
  64.  
  65.    +---------------------------------------------------------- 
  66.  
  67.    * 生成縮略圖 
  68.  
  69.    +---------------------------------------------------------- 
  70.  
  71.    * @static 
  72.  
  73.    * @access public 
  74.  
  75.    +---------------------------------------------------------- 
  76.  
  77.    * @param string $image 原圖 
  78.  
  79.    * @param string $type 圖像格式 
  80.  
  81.    * @param string $thumbname 縮略圖文件名 
  82.  
  83.    * @param string $maxWidth 寬度 
  84.  
  85.    * @param string $maxHeight 高度 
  86.  
  87.    * @param string $position 縮略圖保存目錄 
  88.  
  89.    * @param boolean $interlace 啟用隔行掃描 
  90.  
  91.    +---------------------------------------------------------- 
  92.  
  93.    * @return void 
  94.  
  95.    +---------------------------------------------------------- 
  96.  
  97.    */ 
  98.  
  99.   staticfunctionthumb($image,$thumbname,$type='',$maxWidth=200,$maxHeight=50,$interlace=true) { 
  100.  
  101.     // 獲取原圖信息 
  102.  
  103.     $info= Image::getImageInfo($image); 
  104.  
  105.     if($info!== false) { 
  106.  
  107.       $srcWidth=$info['width']; 
  108.  
  109.       $srcHeight=$info['height']; 
  110.  
  111.       $type=emptyempty($type) ?$info['type'] :$type
  112.  
  113.       $type=strtolower($type); 
  114.  
  115.       $interlace=$interlace? 1 : 0; 
  116.  
  117.       unset($info); 
  118.  
  119.       $scale= min($maxWidth/$srcWidth,$maxHeight/$srcHeight);// 計(jì)算縮放比例 
  120.  
  121.       if($scale>= 1) { 
  122.  
  123.         // 超過(guò)原圖大小不再縮略 
  124.  
  125.         $width=$srcWidth
  126.  
  127.         $height=$srcHeight
  128.  
  129.       }else
  130.  
  131.         // 縮略圖尺寸 
  132.  
  133.         $width= (int) ($srcWidth*$scale); 
  134.  
  135.         $height= (int) ($srcHeight*$scale); 
  136.  
  137.       } 
  138.  
  139.       // 載入原圖 
  140.  
  141.       $createFun='ImageCreateFrom'. ($type=='jpg'?'jpeg':$type); 
  142.  
  143.       if(!function_exists($createFun)) { 
  144.  
  145.         returnfalse; 
  146.  
  147.       } 
  148.  
  149.       $srcImg=$createFun($image); 
  150.  
  151.       //創(chuàng)建縮略圖 
  152.  
  153.       if($type!='gif'&& function_exists('imagecreatetruecolor')) 
  154.  
  155.         $thumbImg= imagecreatetruecolor($width,$height); 
  156.  
  157.       else 
  158.  
  159.         $thumbImg= imagecreate($width,$height); 
  160.  
  161.        //png和gif的透明處理 by luofei614 
  162.  
  163.       if('png'==$type){ 
  164.  
  165.         imagealphablending($thumbImg, false);//取消默認(rèn)的混色模式(為解決陰影為綠色的問(wèn)題) 
  166.  
  167.         imagesavealpha($thumbImg,true);//設(shè)定保存完整的 alpha 通道信息(為解決陰影為綠色的問(wèn)題) 
  168.  
  169.       }elseif('gif'==$type){ 
  170.  
  171.         $trnprt_indx= imagecolortransparent($srcImg); 
  172.  
  173.          if($trnprt_indx>= 0) { 
  174.  
  175.             //its transparent 
  176.  
  177.             $trnprt_color= imagecolorsforindex($srcImg,$trnprt_indx); 
  178.  
  179.             $trnprt_indx= imagecolorallocate($thumbImg,$trnprt_color['red'],$trnprt_color['green'],$trnprt_color['blue']); 
  180.  
  181.             imagefill($thumbImg, 0, 0,$trnprt_indx); 
  182.  
  183.             imagecolortransparent($thumbImg,$trnprt_indx); 
  184.  
  185.        } 
  186.  
  187.       } 
  188.  
  189.       // 復(fù)制圖片 
  190.  
  191.       if(function_exists("ImageCopyResampled")) 
  192.  
  193.         imagecopyresampled($thumbImg,$srcImg, 0, 0, 0, 0,$width,$height,$srcWidth,$srcHeight); 
  194.  
  195.       else 
  196.  
  197.         imagecopyresized($thumbImg,$srcImg, 0, 0, 0, 0,$width,$height,$srcWidth,$srcHeight); 
  198.  
  199.       // 對(duì)jpeg圖形設(shè)置隔行掃描 
  200.  
  201.       if('jpg'==$type||'jpeg'==$type
  202.  
  203.         imageinterlace($thumbImg,$interlace); 
  204.  
  205.       // 生成圖片 
  206.  
  207.       $imageFun='image'. ($type=='jpg'?'jpeg':$type); 
  208.  
  209.       $imageFun($thumbImg,$thumbname); 
  210.  
  211.       imagedestroy($thumbImg); 
  212.  
  213.       imagedestroy($srcImg); 
  214.  
  215.       return$thumbname
  216.  
  217.     } 
  218.  
  219.     returnfalse; 
  220.  
  221.   } 
  222.  

再在項(xiàng)目下的protected/config/main.php中import字段加上

  1. // autoloading model and component classes 
  2.  
  3.   'import'=>array
  4.  
  5.     'application.models.*'
  6.  
  7.     'application.components.*'
  8.  
  9.     'application.extensions.*',  #加上此行,意思為自動(dòng)載入 
  10.  
  11.   ), 

再上面的Controller加上

  1. publicfunctionactionIndex() { 
  2.  
  3.     $model=newUpload; 
  4.  
  5.     if(isset($_POST['Upload'])) { 
  6.  
  7.       $model->image=CUploadedFile::getInstance($model,'image'); 
  8.  
  9.       $ext=$model->image->getExtensionName(); 
  10.  
  11.       $fileName= uniqid() .'.'.$ext
  12.  
  13.       $model->image->saveAs('assets/'.$fileName); 
  14.  
  15.       // 生成縮略圖 
  16.  
  17.       Image::thumb('assets/'.$fileName,'assets/'. uniqid() .'.'.$ext); 
  18.  
  19.     } 
  20.  
  21.     $this->renderPartial('index',array('model'=>$model)); 
  22.  

這次就完整了。

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 政和县| 抚松县| 林周县| 吉林省| 紫金县| 饶阳县| 井研县| 常宁市| 博乐市| 前郭尔| 溧阳市| 漳平市| 出国| 宁陕县| 高雄市| 织金县| 玉山县| 福建省| 桦甸市| 紫金县| 巴彦县| 凤翔县| 南陵县| 渭源县| 洛宁县| 行唐县| 潼南县| 鸡东县| 柳林县| 顺昌县| 汶上县| 潼南县| 仁化县| 剑阁县| 孟州市| 读书| 嘉定区| 万宁市| 房产| 扎赉特旗| 扶沟县|