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数据缓存技术
Feb 14 PHP
PHP提取中文首字母
Apr 09 PHP
发一个php简单的伪原创程序,配合商城采集用的
Oct 12 PHP
PHP 之 写时复制介绍(Copy On Write)
May 13 PHP
php从csv文件读取数据并输出到网页的方法
Mar 14 PHP
使用 PHPStorm 开发 Laravel
Mar 24 PHP
10条php编程小技巧
Jul 07 PHP
基于PHP实现简单的随机抽奖小程序
Jan 05 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
Mar 21 PHP
PHP简单字符串过滤方法示例
Sep 04 PHP
浅析PHP开发规范
Feb 05 PHP
Yii2 queue的队列使用详解
Jul 19 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获取url字符串截取路径的文件名和扩展名的函数
2010/01/22 PHP
php excel类 phpExcel使用方法介绍
2010/08/21 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
2015/06/10 PHP
人脸识别测颜值、测脸龄、测相似度微信接口
2016/04/07 PHP
php 使用curl模拟登录人人(校内)网的简单实例
2016/06/06 PHP
深入理解JavaScript系列(11) 执行上下文(Execution Contexts)
2012/01/15 Javascript
jquery1.83 之前所有与异步列队相关的模块详细介绍
2012/11/13 Javascript
js获取通过ajax返回的map型的JSONArray的方法
2014/01/09 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
js简单实现点击左右运动的方法
2015/04/10 Javascript
基于javascript实现仿百度输入框自动匹配功能
2016/01/03 Javascript
Javascript将JSON日期格式化
2016/08/23 Javascript
JS声明式函数与赋值式函数实例分析
2016/12/13 Javascript
vue.js路由跳转详解
2017/08/28 Javascript
JS通过ajax + 多列布局 + 自动加载实现瀑布流效果
2019/05/30 Javascript
[03:40]DOTA2英雄梦之声_第01期_炼金术士
2014/06/23 DOTA
详解Python安装scrapy的正确姿势
2018/06/26 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
2019/04/12 Python
10 行Python 代码实现 AI 目标检测技术【推荐】
2019/06/14 Python
Pymysql实现往表中插入数据过程解析
2020/06/02 Python
Cole Haan官方网站:美国时尚潮流品牌
2017/12/06 全球购物
美国修容界大佬创建的个人美妆品牌:Kevyn Aucoin Beauty
2018/12/12 全球购物
英国的潮牌鞋履服饰商店:size?
2019/03/26 全球购物
货代行业个人求职简历的自我评价
2013/10/22 职场文书
自动化系在校本科生求职信
2013/10/23 职场文书
学生手册评语
2014/05/05 职场文书
拔河比赛口号
2014/06/10 职场文书
党员自我剖析材料
2014/08/31 职场文书
竞选学委演讲稿
2014/09/13 职场文书
办理房产过户的委托书
2014/09/14 职场文书
公司借款担保书
2015/09/22 职场文书
JavaScript实现复选框全选功能
2021/04/11 Javascript
详解Python为什么不用设计模式
2021/06/24 Python
微软Win11什么功能最惊艳? Windows11新功能特性汇总
2021/11/21 数码科技
PHP正则表达式之RCEService回溯
2022/04/11 PHP
MySQL创建管理子分区
2022/04/13 MySQL