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 picture path to upload below, for example, before and after admin do not/* @param $base 64_img base64 bit picture * @param int $widt H thumbnail image * @return String * @throws Exception */Public Function do_upload ($dir, $base 64_img, $width =300) { $up _dir = './upload/'. $dir. " /";///stored in the current directory under the Upload folder if (!file_exists ($up _dir)) {mkdir ($up _dir,0777); } if (Preg_match ('/^ (data:\s*image\/(\w+); base64,)/', $base 64_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], ", $base 64_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 ("image upload failed"); }}else{//File type error throw new Exception (' Image upload type error '); }}else{//File Error throw new Exception ("File Error"); }} 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
<?phpclass 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 ("Upload success"); } catch (Exception $e) { $this->error_response ($e->getmessage ()); } } /** * Upload Avatar * /Public Function upload_face () { }}
CI Upload image