• 5-1图像类的封装


      1 <?php
      2 /**
      3  * Image.php
      4  * 
      5  * description 图像类
      6  */
      7 
      8 namespace ImoocLib;
      9 
     10 require_once 'GDBasic.php';
     11 
     12 class Image extends GDBasic
     13 {
     14     protected $_width;
     15     protected $_height;
     16     protected $_im;
     17     protected $_type;
     18     protected $_mime;
     19     protected $_real_path;
     20 
     21     
     22     public function __construct($file)
     23     {
     24         //检查GD库
     25         self::check();
     26         $imageInfo = $this->createImageByFile($file);
     27         $this->_width = $imageInfo['width'];
     28         $this->_height = $imageInfo['height'];
     29         $this->_im = $imageInfo['im'];
     30         $this->_type = $imageInfo['type'];
     31         $this->_real_path = $imageInfo['real_path'];
     32         $this->_mime = $imageInfo['mime'];
     33     }
     34 
     35 
     36     /**
     37      * 根据文件创建图像
     38      * @param $file
     39      * @return array
     40      * @throws Exception
     41      */
     42     public function createImageByFile($file)
     43     {
     44         //检查文件是否存在
     45         if(!file_exists($file))
     46         {
     47             throw new Exception('file is not exits');
     48         }
     49 
     50         //获取图像信息
     51         $imageInfo = getimagesize($file);
     52         $realPath = realpath($file);
     53         if(!$imageInfo)
     54         {
     55             throw new Exception('file is not image file');
     56         }
     57 
     58         switch($imageInfo[2])
     59         {
     60             case IMAGETYPE_GIF:
     61                 $im = imagecreatefromgif($file);
     62                 break;
     63             case IMAGETYPE_JPEG:
     64                 $im = imagecreatefromjpeg($file);
     65                 break;
     66             case IMAGETYPE_PNG:
     67                 $im = imagecreatefrompng($file);
     68                 break;
     69             default:
     70                 throw  new Exception('image file must be png,jpeg,gif');
     71         }
     72 
     73         return array(
     74             'width'     => $imageInfo[0],
     75             'height'    => $imageInfo[1],
     76             'type'      => $imageInfo[2],
     77             'mime'      => $imageInfo['mime'],
     78             'im'        => $im,
     79             'real_path' => $realPath,
     80         );
     81 
     82     }
     83 
     84     /**
     85      * 缩略图
     86      * @param  int $width 缩略图高度
     87      * @param  int $height 缩略图宽度
     88      * @return $this
     89      * @throws Exception
     90      */
     91     public function resize($width, $height)
     92     {
     93         if(!is_numeric($width) || !is_numeric($height))
     94         {
     95             throw new Exception('image width or height must be number');
     96         }
     97         //根据传参的宽高获取最终图像的宽高
     98         $srcW = $this->_width;
     99         $srcH = $this->_height;
    100 
    101         if($width <= 0 || $height <= 0)
    102         {
    103             $desW = $srcW;//缩略图高度
    104             $desH = $srcH;//缩略图宽度
    105         }
    106         else
    107         {
    108             $srcP = $srcW / $srcH;//宽高比
    109             $desP = $width / $height;
    110 
    111             if($width > $srcW)
    112             {
    113                 if($height > $srcH)
    114                 {
    115                     $desW = $srcW;
    116                     $desH = $srcH;
    117                 }
    118                 else
    119                 {
    120                     $desH = $height;
    121                     $desW = round($desH * $srcP);
    122                 }
    123             }
    124             else
    125             {
    126                 if($desP > $srcP)
    127                 {
    128                     $desW = $width;
    129                     $desH = round($desW / $srcP);
    130                 }
    131                 else
    132                 {
    133                     $desH = $height;
    134                     $desW = round($desH * $srcP);
    135                 }
    136             }
    137         }
    138 
    139         //PHP版本小于5.5
    140         if(version_compare(PHP_VERSION, '5.5.0', '<'))
    141         {
    142             $desIm = imagecreatetruecolor($desW, $desH);
    143             if(imagecopyresampled($desIm, $this->_im, 0, 0, 0, 0, $desW, $desH, $srcW, $srcH))
    144             {
    145                 imagedestroy($this->_im);
    146                 $this->_im = $desIm;
    147                 $this->_width = imagesx($this->_im);
    148                 $this->_height = imagesy($this->_im);
    149             }
    150         }
    151         else
    152         {
    153             if($desIm = imagescale($this->_im, $desW, $desH))
    154             {
    155                 $this->_im = $desIm;
    156                 $this->_width = imagesx($this->_im);
    157                 $this->_height = imagesy($this->_im);
    158             }
    159 
    160         }
    161 
    162         return $this;
    163     }
    164 
    165 
    166     /**
    167      * 根据百分比生成缩略图
    168      * @param int $percent 1-100
    169      * @return Image
    170      * @throws Exception
    171      */
    172     public function resizeByPercent($percent)
    173     {
    174         if(intval($percent) <= 0)
    175         {
    176             throw new Exception('percent must be gt 0');
    177         }
    178 
    179         $percent = intval($percent) > 100 ? 100 : intval($percent);
    180 
    181         $percent = $percent / 100;
    182 
    183         $desW = $this->_width * $percent;
    184         $desH = $this->_height * $percent;
    185         return $this->resize($desW, $desH);
    186     }
    187 
    188 
    189     /**
    190      * 图像旋转
    191      * @param $degree
    192      * @return $this
    193      */
    194     public function rotate($degree)
    195     {
    196         $degree = 360 - intval($degree);
    197         $back = imagecolorallocatealpha($this->_im,0,0,0,127);
    198         $im = imagerotate($this->_im,$degree,$back,1);
    199         imagesavealpha($im,true);
    200         imagedestroy($this->_im);
    201         $this->_im = $im;
    202         $this->_width = imagesx($this->_im);
    203         $this->_height = imagesy($this->_im);
    204         return $this;
    205     }
    206 
    207 
    208     /**
    209      * 生成水印
    210      * @param file $water 水印图片
    211      * @param int $pct   透明度
    212      * @return $this
    213      */
    214     public function waterMask($water ='',$pct = 60 )
    215     {
    216         //根据水印图像文件生成图像资源
    217         $waterInfo = $this->createImageByFile($water);
    218         imagecopymerge();
    219         //销毁$this->_im
    220         $this->_im = $waterInfo['im'];
    221         $this->_width = imagesx($this->_im);
    222         $this->_height = imagesy($this->_im);
    223         return $this;
    224 
    225     }
    226 
    227 
    228 
    229     /**
    230      * 图片输出
    231      * @return bool
    232      */
    233     public function show()
    234     {
    235         header('Content-Type:' . $this->_mime);
    236         if($this->_type == 1)
    237         {
    238             imagegif($this->_im);
    239             return true;
    240         }
    241 
    242         if($this->_type == 2)
    243         {
    244             imagejpeg($this->_im, null, 80);
    245             return true;
    246         }
    247 
    248         if($this->_type == 3)
    249         {
    250             imagepng($this->_im);
    251             return true;
    252         }
    253     }
    254 
    255     /**
    256      * 保存图像文件
    257      * @param $file
    258      * @param null $quality
    259      * @return bool
    260      * @throws Exception
    261      */
    262     public function save($file, $quality = null)
    263     {
    264         //获取保存目的文件的扩展名
    265         $ext = pathinfo($file, PATHINFO_EXTENSION);
    266         $ext = strtolower($ext);
    267         if(!$ext || !in_array($ext, array('jpg', 'jpeg', 'gif', 'png')))
    268         {
    269             throw new Exception('image save file must be jpg ,png,gif');
    270         }
    271 
    272         if($ext === 'gif')
    273         {
    274             imagegif($this->_im, $file);
    275             return true;
    276         }
    277         if($ext === 'jpeg' || $ext === 'jpg')
    278         {
    279             if($quality > 0)
    280             {
    281                 if($quality < 1)
    282                 {
    283                     $quality = 1;
    284                 }
    285                 if($quality > 100)
    286                 {
    287                     $quality = 100;
    288                 }
    289 
    290                 imagejpeg($this->_im, $file, $quality);
    291             }
    292             else
    293             {
    294                 imagejpeg($this->_im, $file);
    295             }
    296             return true;
    297         }
    298 
    299         if($ext === 'png')
    300         {
    301             imagepng($this->_im, $file);
    302             return true;
    303         }
    304 
    305     }
    306 }

    调用

    require_once './lib/Image.php';
    
    $image = new ImoocLibImage('../image/b.png');
    //$image->resize(400,200)->save('../image/b_400_200.png');
    $image->resizeByPercent(50)->rotate(1800)->show();
  • 相关阅读:
    Windows下安装Redis服务、搭建简单Redis主从复制
    windows下Redis主从复制配置(报错:Invalid argument during startup: unknown conf file parameter : slaveof)
    C#设计模式之控制反转即依赖注入-微软提供的Unity
    C#设计模式之控制反转即依赖注入-Spring.NET
    MongoDB 性能优化
    MongoDB 副本集和C#交互,简单测试
    分布式文档存储数据库(MongoDB)副本集配置
    文件比对工具(Beyond Compare)
    C# 通过WebService方式 IIS发布网站 上传文件到服务器
    3_11_MSSQL课程_ 视图和临时表
  • 原文地址:https://www.cnblogs.com/kay-learning/p/8973238.html
Copyright © 2020-2023  润新知