Yii文件(图片)上传时相关的方法

来源:互联网 发布:小米 免费网络短信 编辑:程序博客网 时间:2024/06/08 05:31

文件图片相关的方法:

1.路径:

[php] view plain copy
  1. /** 
  2.  * 根据优先级(新上传的缩略图->原来的缩略图->文章中第一张图片->默认图片)获取上传路径 
  3.  * @param object $objModel WebsiteInfomation表模型对象 
  4.  * @param object $file 用url获取的CUploadedFile的实例 
  5.  * @return string $oldThumbPicUrl 原本保存的缩略图路径 
  6.  * 
  7.  */  
  8. public function getUploadUrl( $objModel$file$oldThumbPicUrl=null ){  
  9.     $fileDir = 'upfile/article/thumbpic/'.date('Ymd').'/';  //设置目录名  
  10.     // 如果上传了缩略图,就使用上传的缩略图  
  11.     ifis_object$file )&&get_class( $file ) === 'CUploadedFile' ){  
  12.         if( !is_dir$fileDir ) ){  
  13.             if( !mkdir$fileDir ) ){  
  14.                 throw new CHttpException( 502, '目录创建失败.' );  
  15.             }  
  16.         }  
  17.         $objModel->ThumbPicUrl = '/'.$fileDir.time().'_'.rand( 0, 9999 ).'.'.$file->extensionName;   //定义文件保存的目录名称和文件名称  
  18.     // 如果没有上传缩略图,就使用文章中的第一张图片作为缩略图  
  19.     }else{  
  20.         preg_match ( "<img.*src=[\"](.*?)[\"].*?>"$objModel->Content, $match );  
  21.         // 文章中有图片,并且没有选择上传(新的)图片,则使用文章内容的第一张图片  
  22.         if ( !empty$match ) && empty$oldThumbPicUrl ) ){  
  23.             $objModel->ThumbPicUrl = $match[1];  
  24.         //如果文章中也没有图片,则使用默认的图片  
  25.         }else{  
  26.             if ( !$oldThumbPicUrl ) //当没有上传新的缩略图且文章中没有图片,原来的图片没有被更改掉,则不应该替换成默认图片  
  27.                 $objModel->ThumbPicUrl = '/upfile/article/thumbpic/noPic.jpg';  
  28.         }  
  29.     }  
  30. }  


2.base64图片处理:

[php] view plain copy
  1. /** 
  2.  * 还原base64数据流图片保存到制定目录中并写入url路径 
  3.  * @param string $xstr 内容 采集 于 content 
  4.  * @param string $oriweb 网址 一般写null 
  5.  * @return string 
  6.  * 
  7.  */  
  8. public function replaceimg( $xstr$oriweb ){  
  9.     $basedir = 'upfile/article/contentpic/'.date('Ymd').'/';    //设置目录名(保存路径)  
  10.     if( !is_dir$basedir ) ){  
  11.         if( !mkdir$basedir ) ){  
  12.             throw new CHttpException(502,'目录创建失败.');  
  13.         }  
  14.     }  
  15.     //匹配图片的src且只匹配data/base64数据流(目的是只替换base64数据流为图片url路径)  
  16.     preg_match_all( '#<img.*?src="([^"]*)"[^>]*>#i'$xstr$match );  
  17.     foreach$match[1] as $imgurl ){  
  18.         if ( substr$imgurl, 0 , 10) == 'data:image' ){  
  19.             $imgurl = $imgurl;  
  20.             if(is_int(strpos($imgurl'http'))){  
  21.                 $arcurl = $imgurl;  
  22.             } else {  
  23.                 $arcurl = $oriweb.$imgurl;  
  24.             }  
  25.             $img = file_get_contents$arcurl );  
  26.             if( !empty$img ) ) {  
  27.                 //保存图片到服务器  
  28.                 $expData = explode';'$arcurl );  
  29.                 $postfix = explode'/'$expData[0] );  
  30.                 ifstrstr$postfix[0], 'image' ) )  
  31.                     $postfix = $postfix[1] == 'jpeg' ? 'jpg' : $postfix[1]; // 获取后缀名  
  32.                 $fileimgname = time()."-".rand( 1000, 9999 ).".".$postfix;  // 文件名+后缀  
  33.                 $filecachs=$basedir."/".$fileimgname;   //目录+文件+后缀  
  34.                 $fanhuistr = file_put_contents$filecachs$img );   
  35.                 $saveimgfile = "/".$basedir.$fileimgname;  
  36.                 $xstr=str_replace$imgurl$saveimgfile$xstr );  
  37.             }  
  38.         }  
  39.     }  
  40.     //返回替换后的Content  
  41.     return $xstr;  

原创粉丝点击