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 正则匹配函数体
Aug 25 PHP
第六章 php目录与文件操作
Dec 30 PHP
PHP Directory 函数的详解
Mar 07 PHP
解析php函数method_exists()与is_callable()的区别
Jun 21 PHP
php不用正则验证真假身份证
Nov 06 PHP
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
Nov 04 PHP
ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法
Nov 04 PHP
ThinkPHP模型详解
Jul 27 PHP
浅谈php和js中json的编码和解码
Oct 24 PHP
php lcg_value与mt_rand生成0~1随机小数的效果对比分析
Apr 05 PHP
Laravel下生成验证码的类
Nov 15 PHP
PHP7 错误处理机制修改
Mar 09 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实现的购物车类实例
2015/06/17 PHP
PHP中header用法小结
2016/05/23 PHP
php注册系统和使用Xajax即时验证用户名是否被占用
2017/08/31 PHP
javascript SocialHistory 检查访问者是否访问过某站点
2008/08/02 Javascript
用js实现的模拟jquery的animate自定义动画(2.5K)
2010/07/20 Javascript
jQuery学习总结之元素的相对定位和选择器(持续更新)
2011/04/26 Javascript
JS动态修改iframe高度和宽度的方法
2015/04/01 Javascript
JavaScript框架Angular和React深度对比
2017/11/20 Javascript
javaScript 连接打印机,打印小票的实例
2017/12/29 Javascript
vue中element-ui表格缩略图悬浮放大功能的实例代码
2018/06/26 Javascript
js console.log打印对象时属性缺失的解决方法
2019/05/23 Javascript
解决mui框架中switch开关通过js控制开或者关状态时小圆点不动的问题
2019/09/03 Javascript
梳理一下vue中的生命周期
2020/12/30 Vue.js
[50:29]2014 DOTA2华西杯精英邀请赛 5 24 DK VS iG
2014/05/26 DOTA
[51:06]2018DOTA2亚洲邀请赛3月29日 小组赛A组 KG VS Liquid
2018/03/30 DOTA
python读写csv文件并增加行列的实例代码
2019/08/01 Python
django ajax发送post请求的两种方法
2020/01/05 Python
Python3标准库之dbm UNIX键-值数据库问题
2020/03/24 Python
windows python3安装Jupyter Notebooks教程
2020/04/13 Python
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
2020/04/19 Python
Matplotlib 绘制饼图解决文字重叠的方法
2020/07/24 Python
python 通过pip freeze、dowload打离线包及自动安装的过程详解(适用于保密的离线环境
2020/12/14 Python
Python学习之time模块的基本使用
2021/01/17 Python
be2台湾单身男女交友:全球网路婚姻介绍的领导品牌
2019/10/11 全球购物
J2EE是技术还是平台还是框架
2016/08/14 面试题
校园自助餐厅的创业计划书
2013/12/26 职场文书
乌鸦喝水教学反思
2014/02/07 职场文书
上课迟到检讨书
2014/02/19 职场文书
跟单业务员岗位职责
2014/03/08 职场文书
产品开发计划书
2014/04/27 职场文书
飞机制造技术专业求职信
2014/07/27 职场文书
初中优秀学生评语
2014/12/29 职场文书
2015毕业生简历自我评价
2015/03/02 职场文书
2015年党员岗位承诺书
2015/04/27 职场文书
解决Golang中goroutine执行速度的问题
2021/05/02 Golang
vue项目中的支付功能实现(微信支付和支付宝支付)
2022/02/18 Vue.js