ci上传图片
上传 图片 CI
2023-09-14 09:12:15 时间
o_upload.php
<?php /** * Created by PhpStorm. * User: brady * Date: 2018/3/15 * Time: 14:10 */ class O_upload extends MY_Model { protected $table = ''; public function __construct() { parent::__construct(); } /** * @param $dir 图片路径,以upload下面为准 比如 admin 前后都不要 / * @param $base64_img base64位的图片 * @param int $width 缩略图片 * @return string * @throws Exception */ public function do_upload($dir,$base64_img,$width=300) { $up_dir = './upload/'.$dir."/";//存放在当前目录的upload文件夹下 if(!file_exists($up_dir)){ mkdir($up_dir,0777); } if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){ $type = $result[2]; if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){ $new_file = $up_dir.date('YmdHis_').rand(1000,9999).'.'.$type; if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){ $last_file = $this->resize_img($new_file,$width); unlink($new_file); $img_path = substr($last_file,1); return $img_path; }else{ throw new Exception("图片上传失败"); } }else{ //文件类型错误 throw new Exception('图片上传类型错误'); } }else{ //文件错误 throw new Exception("文件错误"); } } public function resize_img($file,$size) { $config['image_library'] = 'gd2'; $config['source_image'] = $file; $config['create_thumb'] = TRUE; $config['thumb_marker'] = "_".$size; $config['maintain_ratio'] = TRUE; $config['width'] = $size; $this->load->library('image_lib', $config); $this->image_lib->resize(); if ( ! $this->image_lib->resize()){ throw new Exception($this->image_lib->display_errors()); } else { $type = pathinfo($file,PATHINFO_EXTENSION ); $len = strlen($type) + 1; $last_file = substr($file,0,-$len); return $last_file."_".$size.".".$type; } } }
image.php
<?php class Images extends MY_Controller { public function __construct() { parent::__construct(); } public function upload() { $this->load->model("O_upload"); try{ $img_path = $this->O_upload->do_upload('article_list'); $this->success_response("上传成功"); }catch(Exception $e){ $this->error_response($e->getMessage()); } } /** * 上传头像 */ public function upload_face() { } }
相关文章
- 【Mongo】uploadify插件帮助实现批量上传
- 过waf实战之文件上传bypass总结
- jquery+html5实现单张图片上传预览
- JavaWeb-SpringBoot_(上)腾讯云点播服务之视频的上传-demo
- HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)
- 文件上传控件bootstrap-fileinput的使用
- 补习系列(11)-springboot 文件上传原理
- JavaScript - input 上传图片 并展示 (食用简单)
- vue+element 递归上传图片
- 文件上传自动生成日期文件夹
- 使用Servlet上传多张图片——Dao层(BaseDao.java)
- 使用ueditor实现多图片上传案例——Dao层(IShoppingDao)
- 使用ueditor实现多图片上传案例——Dao层(IBaseDaoUtil)
- 使用ueditor实现多图片上传案例——实体类(Shopping.java)
- FileUpload实现文件上传(包含多文件)
- 第二百七十三节,Tornado框架-文件上传
- jsch上传文件到服务器
- uni-app+thinkphp: 单图片文件上传(hbuilderx 3.7.3)
- 上传图片更新
- SpringMVC+BUI实现文件上传(附详解,源码下载)
- spring boot单元测试之四:单元测试中测试文件上传(spring boot 2.4.3)
- ckeditor 实现图片上传以及预览(亲测有效)
- git 配置,代码提交到仓库,不小心将node_modules上传,处理操作。.gitignore 删除已提交的文件
- 文件上传漏洞的修复
- OkHttp协议介绍以及文件下载和上传+OkHttp协议封装+OkHttp拦截器____SpringBoot——集成Okhttp3
- 使用commons-fileupload进行上传
- php实现base64图片上传方式实例代码
- 微信上传图片
- 一个端到端的基于 form 表单的文件上传程序,包含客户端和服务器端
- 面试:GET 请求能上传图片吗,已解决
- sftp上传文件和下载文件