CI Upload image

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.