Home  >  Article  >  php教程  >  UploadFile类多缩略图存不同目录

UploadFile类多缩略图存不同目录

WBOY
WBOYOriginal
2016-06-07 11:44:24992browse

发现现在的系统自带的upload类可以创建多个缩略图,但是只能存于同一个文件夹下,我看了一下源文件代码,发现只要修改几个地方就可以实现存放不同文件夹下了。
private function save($file) {<br>         $filename = $file['savepath'].$file['savename'];<br>         if(!$this->uploadReplace && is_file($filename)) {<br>             // 不覆盖同名文件<br>             $this->error    =    '文件已经存在!'.$filename;<br>             return false;<br>         }<br>         // 如果是图像文件 检测文件格式<br>         if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) {<br>             $info   = getimagesize($file['tmp_name']);<br>             if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){<br>                 $this->error = '非法图像文件';<br>                 return false;                <br>             }<br>         }<br>         if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {<br>             $this->error = '文件上传保存错误!';<br>             return false;<br>         }<br>         if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) {<br>             $image =  getimagesize($filename);<br>             if(false !== $image) {<br>                 //是图像文件生成缩略图<br>                 $thumbWidth        =    explode(',',$this->thumbMaxWidth);<br>                 $thumbHeight    =    explode(',',$this->thumbMaxHeight);<br>                 $thumbPrefix    =    explode(',',$this->thumbPrefix);<br>                 $thumbSuffix    =   explode(',',$this->thumbSuffix);<br>                 $thumbFile        =    explode(',',$this->thumbFile);<br>                 $thumbPath      =   $this->thumbPath?:dirname($filename).'/';  //我修改的地方<br>                 $thumbExt       =   $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名<br>                 // 生成图像缩略图<br>                 import($this->imageClassPath);<br>                 for($i=0,$len=count($thumbWidth); $i                     if(!empty($thumbFile[$i])) {<br>                         $thumbname  =   $thumbFile[$i];<br>                     }else{<br>                         $prefix     =   isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0];<br>                         $suffix     =   isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0];<br>                         $thumbname  =   $prefix.basename($filename,'.'.$file['extension']).$suffix;<br>                     }<br>                     if(1 == $this->thumbType){<br>                         Image::thumb2($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); //修改的地方<br>                     }else{<br>                         Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); //修改的地方<br>                     }<br>                     <br>                 }<br>                 if($this->thumbRemoveOrigin) {<br>                     // 生成缩略图之后删除原图<br>                     unlink($filename);<br>                 }<br>             }<br>         }<br>         if($this->zipImags) {<br>             // TODO 对图片压缩包在线解压<br> <br>         }<br>         return true;<br>     }如果改后会出现什么问题,还请告知~~

我后来发现改成数组参数更方便哈!
配置文件设置成这样:$upload->thumbPath=array($targetFolder_s,$targetFolder_m,$targetFolder_b);系统的源类文件只要修改:
if(1 == $this->thumbType){<br>                         Image::thumb2($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);<br>                     }else{<br>                         Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);<br>                     }改成:$thumbPath后加个[$i]if(1 == $this->thumbType){<br>                         Image::thumb2($filename,$thumbPath[$i].$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);<br>                     }else{<br>                         Image::thumb($filename,$thumbPath[$i].$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);<br>                     }就可以了!
还有一个问题是,如果存放路径是动态获取的,比如按时间创建目录,则需要先创建目录,不然存放不了。

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn