ci上传图片
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() { } }
相关推荐
songshijiazuaa 2020-09-24
bbccaaa 2020-07-28
newcome 2020-04-29
Web全栈笔记 2020-05-03
vs00ASPNET 2020-04-29
jinxiutong 2020-04-23
cuiwenjie 2020-04-18
chouliqingke 2020-02-13
breakpoints 2020-01-29
poplpsure 2019-12-28
nxcjh 2020-01-03
tiankele0 2020-01-05
H女王范儿 2019-12-29
且听真言 2019-12-21
前端架构师 2019-12-19
webgm 2019-10-25