龚哥哥 - 山里男儿 爱生活、做自己!
PHP图片上传(简约版)
发表于 2016-5-5 | 浏览(7847) | PHP
<?php

/**
 * 图片处理类
 * @author  Devil
 * @version v_0.0.1
 */
class Images
{
    private $path;
    private $zoom;

    /**
     * [__construct 构造方法]
     * @param [string]  $path [图片存储路径]
     * @param [int]     $zoom [压缩程度]
     */
    public function __construct($path = '', $zoom = 60)
    {
        // 参数校验
        if(empty($path)) exit('param path no');

        // 属性赋值
        $this->path = $path;
        $this->zoom = $zoom;
    }

    /**
     * [ImagesReturn 数据返回]
     * @param [mixed]   $msg  [错误信息/数据]
     * @param [int]     $code [状态]
     * @return[array]         [返回数据]
     */
    private function ImagesReturn($msg = '', $code = 0)
    {
        return array('msg'=>$msg, 'code'=>$code);
    }

    /**
     * [FormSave form图片资源存储]
     * @param [string] $resources   [图片临时资源]
     * @param [string]  $name       [图片名称(可空)]
     * @param [string]  $path       [自定义图片路径(可空)]
     * @return[array]               [图片名称]
     */
    public function FormSave($resources, $name = '', $path = '')
    {
        // 参数校验
        if(empty($resources['tmp_name']) || empty($resources['type'])) return $this->ImagesReturn('参数错误', -1);

        // 文件名生成
        $file_name = empty($name) ? $this->FileNewName() : $name;

        // 图片后缀名
        $suffix = $this->GetSuffix($resources['type']);
        if(empty($suffix)) $this->ImagesReturn('图片后缀名有误', -2);

        // 图片存储
        $file_name = $file_name.'.'.$suffix;
        $path = empty($path) ? $this->path : $path;
        if(move_uploaded_file($resources['tmp_name'], $path.$file_name)) return $this->ImagesReturn($file_name);

        // 返回错误
        return $this->ImagesReturn('图片存储失败', -100);
    }

    /**
     * [GetSuffix 获取图片的后缀名]
     * @param [string] $type [图片资源类型]
     * @return[string]       [后缀名]
     */
    private function GetSuffix($type)
    {
        $img_all = array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif');

        $key = array_search($type, $img_all);
        if($key === false) return '';
        return $key;
    }

    /**
     * [FileNewName 生成文件名]
     */
    private function FileNewName()
    {
        $name = date('YmdHis');
        for($i=0; $i<6; $i++) $name .= rand(0, 9);
        return $name;
    }

    /**
     * [Compression 图片缩放存储]
     * @param [string]  $images [图片资源]
     * @param [string]  $name   [图片名称(可空)]
     * @param [string]  $path   [自定义图片路径(可空)]
     * @param [int]     $w      [宽度(可空)]
     * @param [int]     $h      [高度(默认原始尺寸或以宽度自动计算)]
     * @param [int]     $zoom   [压缩值(默认60)]
     * @return[array]           [图片信息]
     */
    public function Compression($images, $name = '', $path = '', $w = 0, $h = 0, $zoom = 0)
    {
        // 参数校验
        if(empty($images)) return $this->ImagesReturn('参数错误', -1);

        // 获取图片信息
        $info = @getimagesize($images);
        if($info == false) return $this->ImagesReturn('图片有误', -2);

        // 文件名生成
        $file_name = empty($name) ? $this->FileNewName() : $name;

        // 图片后缀名
        //$suffix = $this->GetSuffix($info['mime']);
        $suffix = strrchr($images, '.'); 
        if(empty($suffix)) return $this->ImagesReturn('图片后缀名有误', -3);

        // 取得尺寸的比例值
        $proportion = empty($w) ? 0 : $w/$info[0];

        // 新的宽度
        $new_width = empty($w) ? $info[0] : $w;

        // 如果没有自定义高度则根据宽度计算高度
        $new_height = empty($h) ? (($w < $info[0] && !empty($proportion)) ? intval($proportion*$info[1]) : (($w > $info[0] && !empty($proportion)) ? intval($proportion*$info[1]) : $info[1])) : $h;

        // 新建一个彩色图像
        $new_img = imagecreatetruecolor($new_width, $new_height);

        // 图片资源
        $img = $this->ImageFrom($images, $new_img, $info['mime']);
        if(!$img) return $this->ImagesReturn('图片资源获取失败', -4);

        // 缩放图片
        imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);

        // 缩放程度
        $zoom = empty($zoom) ? (empty($this->zoom) ? 100 : $this->zoom) : $zoom;

        // 存储图片
        $file_name = $file_name.$suffix;
        $path = empty($path) ? $this->path : $path;
        switch($info['mime'])
        {
            case 'image/png':
                $state = imagepng($new_img, $path.$file_name);
                break;

            case 'image/gif':
                $state = imagegif($new_img, $path.$file_name);
                break;

            default:
                $state = imagejpeg($new_img, $path.$file_name, $zoom);
        }

        // 释放内容
        imagedestroy($img);
        imagedestroy($new_img);

        // 返回
        if($state) return $this->ImagesReturn($file_name);
        return $this->ImagesReturn($file_name);
        $this->ImagesReturn('文件储存失败', -100);
    }

    /**
     * [ImageFrom 图片资源获取]
     * @param [string] $images [原图片资源]
     * @param [string] $new_img[新的图片资源]
     * @param [string] $type   [图片类型]
     * @return[mixed]          [成功返回图象资源,失败返回 false]
     */
    private function ImageFrom($images, $new_img, $type)
    {
        // 参数校验
        if(empty($images) || empty($type)) return false;

        // 图片资源获取
        switch($type)
        {
            case 'image/png':
                $img = imagecreatefrompng($images);

                // png保留透明背景
                imagealphablending($new_img, false);
                imagesavealpha($new_img, true);
                imagesavealpha($img, true);
                break;

            case 'image/gif':
                $img = imagecreatefromgif($images);
                break;

            default:
                case 'image/jpeg':
                $img = imagecreatefromjpeg($images);
        }
        return $img;
    }
}
?>

阅读全文

PHP图片上传
发表于 2015-8-31 | 浏览(5332) | PHP
<?php

/**
 * 图片上传驱动
 * @author  Devil
 * @version 1.0.0
 */
class ImageLibrary
{
    private $i_path;
    private $i_type;
    private $i_is_new_name;
    private $i_quality;
    private $i_data;

    /**
     * [__construct 构造方法]
     * @param [array] $parameters [参数列表]
     */
    private function __construct($parameters)
    {
        /* 检测是否支持gd */
        $this->IsGD();

        /* 图片类型 */
        $this->i_type = empty($parameters['type']) ? $this->GetImageType() : $parameters['type'];

        /* 图片名称是否使用新名称, 则使用原图片名称 */
        $this->i_is_new_name = (isset($parameters['is_new_name']) && is_bool($parameters['is_new_name'])) ? $parameters['is_new_name'] : true;

        /* jpg图片的质量值, 值越大质量越好 */
        $this->i_quality = max(75, isset($parameters['quality']) ? intval($parameters['quality']) : 75);
    }

    /**
     * [Instance 静态方法实例化本类]
     * @return [object] [实例对象]
     */
    public static function Instance($parameters = array())
    {
        static $object = null;
        if(!is_object($object)) $object = new self($parameters);
        return $object;
    }

    /**
     * [SetParameters 参数设置]
     * @param [array] $parameters [参数列表]
     */
    public function SetParameters($parameters = array())
    {
        if(empty($parameters)) return;
        $this->__construct($parameters);
    }

    /**
     * [Is_GD 检查是否支持gd库]
     */
    private function IsGD()
    {
        if(!isset(gd_info()['GD Version']) || empty(gd_info()['GD Version'])) Api_Return(L('code_412'), 412);
    }

    /**
     * [GetImageType 获取图片后缀类型]
     * @return [array] [图片后缀类型]
     */
    private function GetImageType()
    {
        return array(
            'gif'   => 'image/gif',
            'png'   => 'image/png',
            'jpg'   => 'image/jpeg',
            'bmp'   => 'image/bmp'
        );
    }

    /**
     * [Initialization 基本信息初始化]
     * @param  [array]   $data [临时图片数据]
     * @param  [string]  $path [图片保存路径]
     * @return [boolean]       [true或false]
     */
    private function Initialization($data, $path)
    {
        if(empty($data)) return false;

        /* 图片保存地址 */
        $path .= (substr($path, -1) == '/') ? '' : '/';
        $this->i_path = empty($path) ? '/tmp/image' : $path;

        /* 设置存储路径是否存在 */
        if(!is_dir($this->i_path)) @mkdir($this->i_path, 0777, true);
        if(!@opendir($this->i_path)) exit('dir not access');

        /* 初始化返回数据 */
        $this->i_data = array();

        return true;
    }

    /**
     * [GetNewFileName 获取随机名称]
     * @return [string] [新的名称]
     */
    private function GetNewFileName()
    {
        return date('YmdHis').str_shuffle(rand());
    }

    /**
     * [SaveBaseImages base64图片存储]
     * @param  [array] $data  [需要存储的base数据, 一维数组]
     * @param  [string] $path [存储路径]
     * @return [array]        [返回图片地址, 一维数组]
     */
    public function SaveBaseImages($data, $path)
    {
        if(!$this->Initialization($data, $path)) return null;

        for($i=0; $i<count($data); $i++)
        {
            if(empty($data[$i])) continue;

            $temp_img = str_replace(array("\n", '\n', ' '), '', $data[$i]);
            $img_info = @getimagesize('data://application/octet-stream;base64,'.$temp_img);
            if(empty($img_info['mime'])) continue;

            $type = $this->Is_ImageType($img_info['mime']);
            if($type == 'no') continue;

            $file_name = $this->GetNewFileName().'.'.$type;
            if(file_put_contents($this->i_path.$file_name, base64_decode($temp_img)) !== false) $this->i_data[] = $file_name;
        }
        return $this->i_data;
    }

    /**
     * [SaveBinaryImages 二进制图片保存]
     * @param  [array]  $data [二进制图片数据, 一维数组]
     * @param  [string] $path [存储路径]
     * @return [array]        [返回图片地址, 一维数组]
     */
    public function SaveBinaryImages($data, $path)
    {
        if(!$this->Initialization($data, $path)) return null;

        for($i=0; $i<count($data); $i++)
        {
            if(empty($data[$i])) continue;

            $img_info = getimagesizefromstring($data[$i]);
            if(empty($img_info['mime'])) continue;

            $type = $this->Is_ImageType($img_info['mime']);
            if($type == 'no') continue;

            $file_name = $this->GetNewFileName().'.'.$type;
            if(file_put_contents($this->i_path.$file_name, $data) !== false) $this->i_data[] = $file_name;
        }
        return $this->i_data;
    }

    /**
     * [GetOriginal 获取临时图片文件的原图]
     * @param  [array]  $data [临时图片数据]
     * @param  [string] $path [存储路径]
     * @return [string]       [返回图片地址]
     */
    public function GetOriginal($data, $path)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if(move_uploaded_file($data['tmp_name'], $this->i_path.$file_name)) return $file_name;
        return '';
    }

    /**
     * [GetBinaryCompress 获取指定图片路径的压缩图]
     * @param  [string] $file  [图片地址]
     * @param  [string] $path  [存储路径]
     * @param  [int]    $width [设定图片宽度]
     * @param  [int]    $height[指定图片高度, 不指定则高度按照比例自动计算]
     * @return [string]        [返回图片地址]
     */
    public function GetBinaryCompress($file, $path, $width = 0, $height = 0)
    {
        if(!$this->Initialization($file, $path) || is_array($file) || !file_exists($file)) return '';

        $img_info = pathinfo($file);
        if(empty($img_info['basename'])) return '';

        $type = $this->Is_ImageType($img_info['extension']);
        if($type == 'no') return '';

        $file_name = ($this->i_is_new_name) ? $this->GetNewFileName().'.'.$type : $img_info['basename'];

        if($this->ImageCompress($width, $height, $file, $type, $this->i_path.$file_name)) return $file_name;

        return '';
    }

    /**
     * [GetCompress 获取临时图片文件的压缩图]
     * @param  [array]  $data  [临时图片数据]
     * @param  [string] $path  [存储路径]
     * @param  [int]    $width [设定图片宽度]
     * @param  [int]    $height[指定图片高度, 不指定则高度按照比例自动计算]
     * @return [atring|空字符串] [返回图片存储的名称]
     */
    public function GetCompress($data, $path, $width = 0, $height = 0)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name)) return $file_name;
        return '';
    }

    /**
     * [GetCompressCut 临时图像裁剪]
     * @param [array]   $data       [图像临时数据]
     * @param [string]  $path       [图像存储地址]
     * @param [int]     $width      [指定存储宽度]
     * @param [ing]     $height     [指定存储高度]
     * @param [ing]     $src_x      [裁剪x坐标]
     * @param [ing]     $src_y      [裁剪y坐标]
     * @param [ing]     $src_width  [裁剪区域宽度]
     * @param [ing]     $src_height [裁剪区域高度]
     * @return [atring|空字符串] [返回图片存储的名称]
     */
    function GetCompressCut($data, $path, $width = 0, $height = 0, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name, $src_x, $src_y, $src_width, $src_height)) return $file_name;
        return '';
    }

    /**
     * [ImageCompress 图片压缩]
     * @param  [int]    $width [指定图片宽度]
     * @param  [int]    $height[指定图片高度]
     * @param  [string] $file  [原图片地址]
     * @param  [string] $type  [类型]
     * @param  [string] $path  [新图片地址]
     * @return [boolean]       [成功true, 失败false]
     */
    private function ImageCompress($width, $height, $file, $type, $path, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
    {
        /* 获取图片原本尺寸 */
        list($w, $h) = getimagesize($file);

        /* 尺寸计算 */
        $new_width = ($width > 0 && $w > $width) ? $width : $w;
        $new_height = ($width > 0 && $w > $width) ? (round($h/($w/$width))) : $h;
        if($width > 0 && $height > 0) $new_width = $width;
        if($height > 0) $new_height = $height;

        /* url创建一个新图象 */
        switch($type)
        {
            case 'gif':
                $src_im = @imagecreatefromgif($file);
                break;
            case 'png':
                $src_im = @imagecreatefrompng($file);
                break;
            default:
                $src_im = @imagecreatefromjpeg($file);
        }
        if(!$src_im) return;

        /* 新建一个真彩色图像 */
        $dst_im = imagecreatetruecolor($new_width, $new_height);

        /* 是否裁剪图片 */
        if($src_width > 0 && $src_height > 0)
        {
            /* 新建拷贝大小的真彩图像 */
            $cpd_im = imagecreatetruecolor($src_width, $src_height);

            /* 拷贝图片 */
            imagecopy($cpd_im, $src_im, 0, 0, $src_x, $src_y, $src_width, $src_height);

            /* 图片缩放 */
            $s = imagecopyresampled($dst_im, $cpd_im, 0, 0, 0, 0, $new_width, $new_height, $src_width, $src_height);
        } else {
            /* 图片缩放 */
            $s = imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
        }

        if($s)
        {
            switch($type)
            {
                case 'png':
                    imagepng($dst_im, $path);
                    break;
                case 'gif':
                    imagegif($dst_im, $path);
                    break;
                default:
                    imagejpeg($dst_im, $path, $this->i_quality);
            }
        }
        imagedestroy($dst_im);
        imagedestroy($src_im);

        return $s;
    }

    /**
     * [Is_ImageType 验证后缀名是否合法]
     * @param  [string] $image_type [图片后缀类型]
     * @return [string]             [后缀名或no]
     */
    private function Is_ImageType($image_type)
    {
        if(empty($image_type)) return 'no';
        if(array_key_exists($image_type, $this->i_type)) return $image_type;

        foreach($this->i_type as $key=>$val)
        {
            if($val == $image_type) return $key;
        }
        return 'no';
    }
}
?>

阅读全文

TOP