Home > php教程 > php手册 > ThinkPHP多图上传带缩略图功能

ThinkPHP多图上传带缩略图功能

WBOY
Release: 2016-06-07 11:42:30
Original
1197 people have browsed it

功能简介:
支持多图上传并生成缩略图
可自定义缩略图数量与尺寸
文件默认保存在 “./data/attachment/photo/年月/日/” 目录下

访问地址:http://localhost/upload/photo

如有问题请各位大侠海涵并指正,谢谢~

效果:
ThinkPHP多图上传带缩略图功能

首先新建一个控制器文件,命名为:UploadController.class.php,
位置:application\Home\Controller\UploadController.class.php

控制器代码:<?php <br /> <br> /**<br>  * 图片上传控制器<br>  * @author    Jiekii <jiekii><br>  * @website    http://jiekii.com<br>  * @date        2014-02-21<br> **/<br> <br> namespace Home\Controller;<br> <br> class UploadController extends HomeController {<br>     public function photo() {<br>         if(IS_POST) {<br>             //设置文件保存目录<br>             $baseDir = './data/';<br>             $attachDir = './attachment/photo/'.date('Ym').'/';<br>             $subDir = date('d');<br>             $saveName = date('His').strtolower(random(16));<br> <br>             //上传类配置信息<br>             $config = array(<br>                 'maxSize' => 2097152,<br>                 'exts' => array('jpg', 'jpeg', 'png', 'gif'),<br>                 'rootPath' => $baseDir,<br>                 'savePath' => $attachDir,<br>                 'subName' => array('date', 'd'),<br>                 'saveName' => $saveName,<br>                 'hash' => false<br>             );<br> <br>             //初始化上传类<br>             $upload = new \Think\Upload($config);<br> <br>             //检查是否选择图片<br>             $inputName = 'photo';<br>             $total = 0;<br>             $data = array();<br>             foreach($_FILES[$inputName] as $key => $value) {<br>                 foreach($value as $k => $v) {<br>                     $data[$k][$key] = $v;<br>                     if($key == 'name' && $v) {<br>                         $total++;<br>                     }<br>                 }<br>             }<br> <br>             if(!$total) {<br>                 $this->error('请先选择要上传的图片!');<br>             }<br> <br>             $uploadSuccess = $uploadFailure = 0;<br>             $result = array();<br> <br>             //缩略图列表,数组为空则不生成缩略图<br>             //键为缩略图文件名后缀,例如:20140221abc_a.jpg<br>             //值为缩略图宽/高<br>             $thumbList = array(<br>                 'a' => array(150, 150),<br>                 'c' => array(250, 250),<br>                 'm' => array(500, 500)<br>             );<br> <br>             //初始化缩略图类<br>             if(!empty($thumbList)) {<br>                 $image = new \Think\Image();<br>             }<br> <br>             foreach($data as $key => $value) {<br>                 if(!$value['name']) continue;<br> <br>                 //如果多图则从第二张开始设置新的文件名<br>                 if($key >= 1) {<br>                     $upload->saveName = date('His').strtolower(random(16));<br>                 }<br> <br>                 //开始上传<br>                 $file = $upload->upload(array($inputName => $value));<br> <br>                 //上传成功<br>                 if(!empty($file)) {<br>                     $uploadSuccess++;<br> <br>                     //缩略图<br>                     if(!empty($thumbList)) {<br>                         $path = $baseDir.$file[$inputName]['savepath'].$upload->saveName;<br>                         $fileExt = $file[$inputName]['ext'];<br>                         $filePath = $path.'.'.$fileExt;<br> <br>                         //生成缩略图,按照原图的比例<br>                         foreach($thumbList as $thumbName => $thumbSize) {<br>                             if(!$thumbName || empty($thumbSize)) continue;<br> <br>                             $image->open($filePath);<br>                             $image->thumb($thumbSize[0], $thumbSize[1])->save($path.'_'.$thumbName.'.'.$fileExt);<br>                         }<br>                     }<br>                 } else {<br>                     $uploadFailure++;<br>                 }<br>                 $result[] = array($upload->getError(), $file);<br>             }<br> <br>             //成功提示<br>             if($uploadSuccess) {<br>                 $this->success($uploadSuccess.'张图片上传成功!');<br>             } else {<br>                 $this->error('上传失败!');<br>             }<br>         } else {<br>             $value = array(<br>                 'meta_title' => '上传照片'<br>             );<br>             $this->assign($value)->display();<br>         }<br>     }<br> }</jiekii>html代码:<form> <br>     <p>图片1:<input></p> <br>     <p>图片2:<input></p> <br>     <p>图片3:<input></p> <br>     <p><button>确定上传</button></p> <br> </form>

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template