Home > php教程 > php手册 > Thinkphp3.2上传图片并生成缩略图代码改进的

Thinkphp3.2上传图片并生成缩略图代码改进的

WBOY
Release: 2016-06-07 11:41:41
Original
912 people have browsed it

新发布的3.2版本上传图片没有带缩略图功能,我改了下代码,可以生成缩略图
新发布的3.2版本上传图片没有带缩略图功能,我改了下代码,可以生成缩略图。现在附上代码和说明
把这2个类放在hinkPHP\Library\Think\下
UploadFile.class.php
UploadImage.class.php

添加新闻的代码如下
public function add(){

if(IS_POST){
//获取表单元素,这里可以改的
$data ['title'] = I('post.title','','htmlspecialchars');
$data['author'] = I('post.author','','htmlspecialchars');
$data['content'] = I('post.content','','htmlspecialchars');
$data['inputtime'] = time();
$data['tid'] = I('post.tid','0');
$data['status'] =1;
Thinkphp3.2上传图片并生成缩略图代码改进的
if (isset($_FILES['pic'])){
//文件上传配置,可以生成缩略图
$date11 =date('Y-m-d');
$upload = new \Think\UploadFile();// 实例化上传类
$upload->maxSize = 3000000 ;// 设置附件上传大小
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->allowTypes = array('image/jpg','image/jpeg','image/pjpeg','image/png','image/gif');
$upload->savePath = './Public/Uploads/News/'.$date11.'/';// 设置附件上传目录
$upload->saveRule = 'uniqid';

//缩略图配置
$upload->thumb = true;
$upload->thumbMaxWidth = '440';
$upload->thumbMaxHeight = '580';
$upload->thumbPrefix = 's_';
$upload->thumbRemoveOrigin = false;

//开始上传
if($upload->upload()){
$info = $upload->getUploadFileInfo();
//大图
$data['newspic'] = '/Public/Uploads/News/'.$date11.'/'.$info[0]['savename'];
}
}
//入库操作
}else{
$this->display();
}

}

附件 上传图片生成缩略图.rar ( 8.44 KB 下载:609 次 )

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