二进制流图片上传类-分享

 
就不github了 ………
 
<?php

//================   ImageBinaryStream   ==========================

/** * Class ImageBInaryStream */Class ImageBinaryStream{    /**     * @var array     */    private $config  = [        ‘path‘=>‘‘,        ‘width‘=>0,        ‘height‘=>0,        ‘type‘=>[],        ‘size‘=>1024*5    ];

/**     * @var string     */    private $errorsize= ‘Img Size Wrongful‘;    /**     * @var string     */    private $errorType= ‘Img Type Wrongful‘;    /**     * @var string     */    private $errorwidth= ‘Img Width Wrongful‘;    /**     * @var string     */    private $errorheight =  ‘Img Height Wrongful‘;

protected $moudle = ‘auto‘;

protected $rootfile = ‘static/uploads‘;

/**     * CONSTRUCT     * ImageBInaryStream constructor.     * @param array $config  //CONFIG     * @param string $moudle //FILE NAME     */    public function __construct($config=[],$moudle="")    {        $path = dirname(realpath($_SERVER[‘SCRIPT_FILENAME‘]));//PATH        $this->config = array_merge($this->config,$config);

if(!empty($moudle)){            $this->moudle = $moudle;        }

if(empty($this->config[‘path‘])){            $this->config[‘path‘] = $path;        }

}

/**     * @return \think\response\Json     */    public function run()    {        header(‘Content-type:text/html;charset=utf-8‘);        $base64_image_content = $_POST[‘img‘];        $vali = $this->validate($base64_image_content);        if(!$vali[‘code‘]){            return json($vali,400);        }        $save =$this->save($base64_image_content,$vali[‘data‘]);        if(!$save[‘code‘]){            return json($save,400);        }        return json($save,200);    }

/**     * @param $base64_image_content     * @param $vali     * @return array     */    protected function save($base64_image_content,$vali)    {        if (preg_match(‘/^(data:\s*image\/(\w+);base64,)/‘, $base64_image_content, $result)){            $type = $result[2];            $new_file = $this->path.DIRECTORY_SEPARATOR.$this->rootfile.DIRECTORY_SEPARATOR.$this->moudle.DS.date(‘Ymd‘,time())."/";            if(!file_exists($new_file))            {                //Check to see if there is a folder, if not created, and given the maximum permissions                mkdir($new_file, 0700,true);            }            $new_file = $this->rootfile.‘/‘.$this->moudle.‘/‘.date(‘Ymd‘,time()).‘/‘.time().".{$type}";            if (file_put_contents($new_file, base64_decode(str_replace($result[1], ‘‘, $base64_image_content))))            {                $fileAttr = $vali;                $filesize = $fileAttr[‘size‘].‘kb‘;                $data = ["url"=>$new_file,"width"=> $fileAttr[‘width‘],"height"=> $fileAttr[‘height‘],‘size‘=>$filesize];                return $this->msg_array(1,‘success‘,$data);            }else{                return $this->msg_array(0,$this->NotFound,[]);            }        }        return $this->msg_array(0,‘Base64Data Error‘,[]);    }

/**     * @return array     */    protected function validate($base64_image_content)    {        if(!$base64_image_content){            return $this->msg_array(0,"Not Image");        };        $fileAttr = getimagesize($base64_image_content);  //ATTR        preg_match(‘/^(data:\s*image\/(\w+);base64,)/‘, $base64_image_content, $result);  //IMG BASE64        $filesize  = round(strlen(base64_decode(str_replace($result[1], ‘‘, $base64_image_content)))/1024); //KB        $data = ["width"=> $fileAttr[1],"height"=> $fileAttr[0],‘size‘=>$filesize];// fileData        if($this->WH_validate([$this->width,$this->height])){            if($fileAttr[0] != $this->height){                return $this->msg_array(0,$this->errorheight);            }            if($fileAttr[0] != $this->width){                return $this->msg_array(0,$this->errorwidth);            }        }

if($filesize > $this->size){            return $this->msg_array(0,$this->errorsize);        }

preg_match(‘/^[a-z]+[\/]([a-z]+)/‘,‘image/jpeg‘,$result);        if(!in_array($result[1],$this->type)){            return $this->msg_array(0,$this->errorType);        }

return $this->msg_array(1,"success",$data);    }

/**     * @param array $arr     * @return bool     */    private function WH_validate($arr=[])    {        foreach ($arr as $key){            if(empty($key)){                return false;            }        }        return true;    }

/**     * @param $name     * @return mixed     */    public function __get($name)    {        // TODO: Implement __get() method.        return $this->config[$name];    }

/**     * @param $name     * @param $value     * @return mixed     */    public function __set($name, $value)    {        // TODO: Implement __set() method.        return $this->config[$name]=$value;    }

/**     * @param $name     * @return bool     */    public function __isset($name)    {        // TODO: Implement __isset() method.        return isset($this->config[$name]);    }

/**     * @param int $code     * @param string $msg     * @param array $data     * @param string $url     * @param int $wait     * @return array     */    private function msg_array($code = 1,$msg="",$data=[],$url="",$wait= 3)    {        $result = [            ‘code‘ => $code,            ‘msg‘  => $msg,            ‘data‘ => $data,            ‘url‘  => $url,            ‘wait‘ => $wait,        ];        return $result;    }

}
时间: 2024-08-05 13:57:48

二进制流图片上传类-分享的相关文章

艾恩ASP无组件上传类(上传组件)说明文档(from www.sysoft.cc)

艾恩ASP无组件上传类(上传组件)说明文档2010-1-18 By Anlige一.简介自从接触ASP就开始接触上传,看过一些上传类,但是总感觉封装的还是不够简单,因此自己尝试写一个能够用最少最简单的代码实现各种上传方式的上传类.在学校期间就开始写,一点点的完善.优化,到现在的版本,现在的版本能适应各种上传方式.上传类的主要的功能如下:1.自由设置最大上传大小.单文件最大上传大小2.自由设置允许上传的文件类型3.可设置文本的编码,以适应各种上传环境4.内置进度条,a用户可选择开启和关闭5.多种错

asp无惧上传类2.2上传文件的同时,通过 Request.QueryString将参数传递到保存页面中

先转一段文字,对不对再评论 在后台asp程序中,以前获取表单提交的ASCII 数据,非常的容易.但是如果 需要获取上传的文件,就必须使用Request对象的BinaryRead方法来读取.BinaryRead方法是对当前输入流进行指定字节数的二进制读取,有点需要注意的 是,一旦使用BinaryRead 方法后,再也不能使用Request.Form 或  Request.QueryString 集合了.结合Request对象的TotalBytes属性,可以将 所有表单提交的数据全部变成二进制,不过

图片上传类与加密解密

php图片上传类 <?php //引入config_app全局变量 include ini_get("yaf.library") ."/../config/application/global.php"; include 'Image.php'; include 'Aes.php'; //获取链接过来的参数 $format = isset($_GET['__format'])?$_GET['__format']:"json"; switch

无惧上传类修改版ASP

<%OPTION EXPLICIT%><%class clsUp '文件上传类'------------------------Dim Form,FileDim AllowExt_ '允许上传类型(白名单)Dim NoAllowExt_ '不允许上传类型(黑名单)Private oUpFileStream '上传的数据流Private isErr_ '错误的代码,0或true表示无错Private ErrMessage_ '错误的字符串信息Private isGetData_ '指示是否

实例讲解ThinkPHP的UploadFile文件上传类的详细用法

ThinkPHP文件上传自带了上传类,使用起来非常方便,我们将以一个文件上传实例来讲解ThinkPHP上传类的实际用法,上传类使用时有详细的中文注释,可以非常方便的告诉你类的一些方法或变量的使用方法.FileAction.class.php PHP Code复制内容到剪贴板 <?php class FileAction extends Action{ function index(){ $file=M('file'); $list=$file->select(); $this->assi

一个能防止改名木马漏洞的无组件上传类

现在流行的asp上传组件除了无惧的化境之外,最多的可能就是ewebEditor 和Fckeditor的上传是,但是经过测试都很难防止改名为gif和asp文件上传,在FckEditor中改名后的asp木马不能直接上传,系统会检测到 <%等字符而拒绝,但是经过修改后的asp木马再改名为gif后却可以顺利上传,如在文件前端加上许多空行,或对木马进行加密处理.当然有人会认为木马传到服务器后会被杀掉,但是做过免杀的木马却会漏网.基于这些原因,本人开发了一个可以从根本上解决这个问题的无组件上传类.经过测试常

ajax结合文件上传类进行多文件的单个上传

今天做项目的时候碰见一个问题:之前一个同事离职之前做了一个网站,有一个上传商品详细图片的功能,当时已经完成,但是由于后期程序的有更改以及更改的程序员的水平也是参差不齐,最后导致程序bug很多,由于当时用的是一个框架,最终也没找到说明文档,后来我就重新写了一个结合ajax上传文件的upload.classs.php虽然界面欠缺美观,但是通俗易懂好维护. //首先是页面. index.php <!DOCTYPE html> <html lang="en"> <

Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传

在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类. ① 下载编辑器(下载地址:http://ueditor.baidu.com/website/download.html),解压后放入项目根目录的 Data 目录并且将解压出来的目录重命名为 ueditor. 项目中的控制器 ./Application/Admin/Controller/BlogController.class.php

PHP图片上传类

前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也还有必要.一.控制器调用 public function upload_file() { if (IS_POST) { if (!empty($_FILES['Filedata'])) { import('Org.Upload', COMMON_PATH); $upload = new \Upload(); // 允许上传文件大小 $upload->allowMaxSize(C('ALLOW_UPLOAD_FILE_MAX