Thinkphp3.2上传图片并生成缩略图代码改进的
程序员文章站
2022-04-16 10:09:22
...
新发布的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;
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();
}
}
新发布的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;
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元
上一篇: jquery如何判断是否存在class
下一篇: JS暴虐查找法_基础知识