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的控制语句
Oct 09 PHP
PHP简洁函数小结
Aug 12 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
Jun 29 PHP
PHP判断文章里是否有图片的简单方法
Jul 26 PHP
PHP检测用户是否关闭浏览器的方法
Feb 14 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
Jun 30 PHP
php简单统计中文个数的方法
Sep 30 PHP
Yii2下session跨域名共存的解决方案
Feb 04 PHP
Yii框架实现图片上传的方法详解
May 20 PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
Sep 26 PHP
PHP大文件切割上传并带进度条功能示例
Jul 01 PHP
php引用和拷贝的区别知识点总结
Sep 23 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
php常用文件操作函数汇总
2014/11/22 PHP
Zend Framework教程之Zend_Controller_Plugin插件用法详解
2016/03/07 PHP
功能强大的PHP POST提交数据类
2016/07/15 PHP
阿里云Win2016安装Apache和PHP环境图文教程
2018/03/11 PHP
浅谈Javascript中的Function与Object
2015/01/26 Javascript
总结JavaScript中布尔操作符||与&amp;&amp;的使用技巧
2015/11/17 Javascript
获取阴历(农历)和当前日期的js代码
2016/02/15 Javascript
domReady的实现案例
2016/11/23 Javascript
javascript 闭包详解及简单实例应用
2016/12/31 Javascript
BootstrapTable refresh 方法使用实例简单介绍
2017/02/20 Javascript
JS实现的添加弹出层并完成锁屏操作示例
2017/04/07 Javascript
详谈ES6中的迭代器(Iterator)和生成器(Generator)
2017/07/31 Javascript
ES6 迭代器(Iterator)和 for.of循环使用方法学习(总结)
2018/02/08 Javascript
require.js 加载过程与使用方法介绍
2018/10/30 Javascript
Layui选项卡制作历史浏览记录的方法
2019/09/28 Javascript
webpack4 optimization使用总结
2019/11/10 Javascript
[05:13]TI4 中国战队 机场出征!!
2014/07/07 DOTA
跟老齐学Python之有容乃大的list(4)
2014/09/28 Python
Django实现图片文字同时提交的方法
2015/05/26 Python
使用50行Python代码从零开始实现一个AI平衡小游戏
2018/11/21 Python
浅谈python图片处理Image和skimage的区别
2019/08/04 Python
pytorch在fintune时将sequential中的层输出方法,以vgg为例
2019/08/20 Python
Tensorflow训练模型越来越慢的2种解决方案
2020/02/07 Python
Python opencv相机标定实现原理及步骤详解
2020/04/09 Python
Python实现ElGamal加密算法的示例代码
2020/06/19 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
2020/12/24 Python
基于Html5实现的react拖拽排序组件示例
2018/08/13 HTML / CSS
HTML5里的placeholder属性使用实例和美化显示效果的方法
2014/04/23 HTML / CSS
Canvas实现贝赛尔曲线轨迹动画的示例代码
2019/04/25 HTML / CSS
优秀党员主要事迹
2014/01/19 职场文书
师范学院教师自荐书
2014/01/31 职场文书
护理专业自荐信范文
2014/02/26 职场文书
教学改革实施方案
2014/03/31 职场文书
报告会主持词
2014/04/02 职场文书
财务内勤岗位职责
2014/04/17 职场文书
环境工程专业自荐信范文
2014/06/24 职场文书