PHP生成图片缩略图类示例


Posted in PHP onJanuary 12, 2017

本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:

class App_image_helper {
  protected $imgFileName;
  protected $imgWidth;
  protected $imgHeight;
  protected $imgMime;
  protected $imgResource;
  static  $imgMineList
    = array(
      'jpeg' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png',
      'wbmp' => 'image/wbmp',
    );
  /**
   * 根据文件名,初始化图片,
   * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用
   * App_image_helper constructor.
   *
   * @param $fileName
   */
  public function __construct($fileName) {
    $this->imgFileName = $fileName;
    list($this->imgWidth, $this->imgHeight, $this->imgMime) = $this->getImageInfo($this->imgFileName);
    $this->imgResource = $this->getImageResource($this->imgFileName);
  }
  /**
   * 根据图片路径获取相关宽、高、MIME类型信息
   *
   * @param $fileName
   *
   * @return array|null
   */
  protected function getImageInfo($fileName) {
    $result = null;
    if ( is_file($fileName) ) {
      $tmpImageInfo = getimagesize($fileName);
      if ( $tmpImageInfo ) {
        $result = array($tmpImageInfo[0], $tmpImageInfo[1], $tmpImageInfo['mime']);
      }
    }
    return $result;
  }
  /**
   * 将图片文件转为资源类类型
   *
   * @param $fileName
   *
   * @return null|resource
   */
  protected function getImageResource($fileName) {
    $image = null;
    if ( is_file($fileName) ) {
      switch ($this->imgMime) {
        case self::$imgMineList['jpeg']:
          $image = imagecreatefromjpeg($fileName);
          break;
        case self::$imgMineList['gif']:
          $image = imagecreatefromgif($fileName);
          break;
        case self::$imgMineList['png']:
          $image = imagecreatefrompng($fileName);
          break;
        case self::$imgMineList['wbmp']:
          $image = imagecreatefromwbmp($fileName);
          break;
        default:
          break;
      }
    }
    return $image;
  }
  /**
   * 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片
   *
   * @param int $width
   * @param int $percent
   *
   * @return array|null
   */
  protected function getSizeByScale($width = 360, $percent = 1) {
    $result = null;
    if ( $this->imgWidth && $this->imgHeight ) {
      if ( $width ) {
        $result = array($width, intval($width * $this->imgHeight / $this->imgWidth));
      } elseif ( $percent ) {
        $result = array(intval($this->imgWidth * $percent), intval($this->imgHeight * $percent));
      }
    }
    return $result;
  }
  /**
   * 外调
   *
   * @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比
   * @param null $fileName
   * @param int $quality
   * @param bool $reSample    重新采样图片,默认是
   *
   * @return bool
   */
  public function createImage($percentOrWidth = 1, $fileName = null, $quality = 75, $reSample = true) {
    $result = false;
    $fileName ? header('Content-Type: ' . $this->imgMime) : false;
    $size = $this->getSizeByScale(($percentOrWidth <= 1) ? null : $percentOrWidth, $percentOrWidth);
    if ( $size ) {
      $thumb = imagecreatetruecolor($size[0], $size[1]);
      if ( $reSample ) {
        imagecopyresampled($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
      } else {
        imagecopyresized($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
      }
      $result = imagejpeg($thumb, $fileName, $quality);
    }
    return $result;
  }
}
PHP 相关文章推荐
实现php加速的eAccelerator dll支持文件打包下载
Sep 30 PHP
php正则校验用户名介绍
Jul 19 PHP
关于php连接mssql:pdo odbc sql server
Jul 20 PHP
PHP 之Section与Cookie使用总结
Sep 14 PHP
九个你必须知道而且又很好用的php函数和特点
Aug 08 PHP
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
Aug 28 PHP
PHP SOCKET编程详解
May 22 PHP
php实现基于openssl的加密解密方法
Sep 30 PHP
php封装db类连接sqlite3数据库的方法实例
Dec 19 PHP
PHP面向对象之里氏替换原则简单示例
Apr 08 PHP
使用laravel的Eloquent模型如何获取数据库的指定列
Oct 17 PHP
PHP程序守护进程化实现方法详解
Jul 16 PHP
php+redis实现多台服务器内网存储session并读取示例
Jan 12 #PHP
[原创]PHPCMS遭遇会员投稿审核无效的解决方法
Jan 11 #PHP
YII2 实现多语言配置的方法分享
Jan 11 #PHP
laravel5.2实现区分前后台用户登录的方法
Jan 11 #PHP
PHP全功能无变形图片裁剪操作类与用法示例
Jan 10 #PHP
php实现36进制与10进制转换功能示例
Jan 10 #PHP
php获取当前url地址的方法小结
Jan 10 #PHP
You might like
ThinkPHP模板中数组循环实例
2014/10/30 PHP
C# WinForm中实现快捷键自定义设置实例
2015/01/23 PHP
PHP中实现接收多个name相同但Value不相同表单数据实例
2015/02/03 PHP
PHP查询附近的人及其距离的实现方法
2016/05/11 PHP
PHP实现支付宝即时到账功能
2016/12/21 PHP
IE6下出现JavaScript未结束的字符串常量错误的解决方法
2010/11/21 Javascript
初窥JQuery(二) 事件机制(1)
2010/11/25 Javascript
jQuery load方法用法集锦
2011/12/06 Javascript
找出字符串中出现次数最多的字母和出现次数精简版
2012/11/07 Javascript
Javascript自定义排序 node运行 实例
2013/06/05 Javascript
通过action传过来的值在option获取进行验证的方法
2013/11/14 Javascript
node.js使用require()函数加载模块
2014/11/26 Javascript
五种js判断是否为整数类型方式
2015/12/03 Javascript
JavaScript的设计模式经典之建造者模式
2016/02/24 Javascript
Bootstrap基本插件学习笔记之标签切换(17)
2016/12/08 Javascript
BootStrapTable服务器分页实例解析
2016/12/20 Javascript
javascript过滤数组重复元素的实现方法
2017/05/03 Javascript
jQuery+PHP+Mysql实现抽奖程序
2020/04/12 jQuery
Ionic学习日记实现验证码倒计时
2018/02/08 Javascript
解决Vue.js应用回退或刷新界面时提示用户保存修改问题
2019/11/24 Javascript
详细分析JavaScript中的深浅拷贝
2020/09/17 Javascript
Python利用递归实现文件的复制方法
2018/10/27 Python
python3.6使用urllib完成下载的实例
2018/12/19 Python
详解Python装饰器
2019/03/25 Python
python requests更换代理适用于IP频率限制的方法
2019/08/21 Python
OpenCV python sklearn随机超参数搜索的实现
2020/01/17 Python
Python文件读写w+和r+区别解析
2020/03/26 Python
Pyinstaller打包Scrapy项目的实现步骤
2020/09/22 Python
python 实现aes256加密
2020/11/27 Python
美国知名的摄影器材销售网站:Adorama
2017/02/01 全球购物
ECCO爱步官方旗舰店:丹麦鞋履品牌
2018/01/02 全球购物
Weblogic和WebSphere不同特点
2012/05/09 面试题
亲属关系公证书
2014/04/08 职场文书
全国爱眼日活动总结
2015/02/27 职场文书
2015年污水处理厂工作总结
2015/05/26 职场文书
5行Python代码实现一键批量扣图
2021/06/29 Python