zoukankan      html  css  js  c++  java
  • 二进制流图片上传类-分享

     
    ------------------类 Class ………
     
    <?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 $module = 'auto';

    protected $rootfile = 'static/uploads';

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

    $this->config = array_merge($this->config,$config);

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

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

    }

    /**
    * @return
    hink esponseJson
    */
    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] == 'jpeg'? 'jpg':$result[2
    ];
    $new_file = $this->path.DIRECTORY_SEPARATOR.$this->rootfile.DIRECTORY_SEPARATOR.$this->module.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->module.'/'.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
    ;
    }

    }
     
    -------------------------------appliy:
     
    $module = 'brand';
    import('ImageBinaryStream',EXTEND_PATH,'PHP'
    );
    $image = new ImageBinaryStream(config('base64imageupload'),$module
    );
    return $image
    ->run();
  • 相关阅读:
    二叉树的建树,按层遍历,结点总数,页结点,深度以及三序非递归遍历二叉树,建立中序线索二叉树
    志愿者选拔(单调队列)
    重建二叉树(中后序求前序)
    New Year Table(几何)
    Hopscotch(细节)
    红黑树(中序二叉树)
    Linux-awk命令详解
    Python--常用模块
    Python--re模块
    Python--模块与包
  • 原文地址:https://www.cnblogs.com/q1104460935/p/7072969.html
Copyright © 2011-2022 走看看