• 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()
        {
    
        }
    }
    
  • 相关阅读:
    c++ 在window下创建窗口的基本步骤
    visual studio 2015 安装MSDN全称Microsoft Developer Network 安装离线的MSDN
    interp1一维数据插值在matlab中的用法
    Win32控制台、Win32项目、MFC项目、CLR控制台、CLR空项目、空项目区别
    C# 中的延时的方法。
    C#入门——Console.Write()与Console.WriteLine()
    php发送短信验证码
    来自联想、百度的团队,带着颠覆的理想,做短信服务平台
    python发送短信验证码
    uperTextView-从未如此惊艳!一个超级的TextView
  • 原文地址:https://www.cnblogs.com/brady-wang/p/9058604.html
Copyright © 2020-2023  润新知