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 超链接 抓取实现代码
Jun 29 PHP
php二维数组排序方法(array_multisort usort)
Dec 25 PHP
php function用法如何递归及return和echo区别
Mar 07 PHP
PHP中的Memcache详解
Apr 05 PHP
PHP清除字符串中所有无用标签的方法
Dec 01 PHP
Yii多表联合查询操作详解
Jun 02 PHP
[原创]PHPCMS遭遇会员投稿审核无效的解决方法
Jan 11 PHP
Zend Framework数据库操作技巧总结
Feb 18 PHP
thinkPHP框架可添加js事件的分页类customPage.class.php完整实例
Mar 16 PHP
Yii2结合Workerman的websocket示例详解
Sep 10 PHP
php适配器模式简单应用示例
Oct 23 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
Apr 06 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
10个简化PHP开发的工具
2014/12/25 PHP
php中输出json对象的值(实现方法)
2018/03/07 PHP
jquery控制listbox中项的移动并排序的实现代码
2010/09/28 Javascript
jQuery.lazyload+masonry改良图片瀑布流代码
2014/06/20 Javascript
禁止iframe页面的所有js脚本如alert及弹出窗口等
2014/09/03 Javascript
jQuery实现冻结表头的方法
2015/03/09 Javascript
JavaScript将数字转换成大写中文的方法
2015/03/23 Javascript
js判断手机号是否正确并返回的实现代码
2017/01/17 Javascript
JS实现的随机排序功能算法示例
2017/06/09 Javascript
vue配置请求本地json数据的方法
2018/04/11 Javascript
微信小程序支付PHP代码
2018/08/23 Javascript
详解写好JS条件语句的5条守则
2019/02/28 Javascript
详解Bootstrap 学习(一)入门
2019/04/12 Javascript
JS匿名函数内部this指向问题详析
2019/05/10 Javascript
详解基于Vue的支持数据双向绑定的select组件
2019/09/02 Javascript
JavaScript 判断数据类型的4种方法
2020/09/11 Javascript
js禁止查看源文件屏蔽Ctrl+u/s、F12、右键等兼容IE火狐chrome
2020/10/01 Javascript
[36:37]2014 DOTA2华西杯精英邀请赛5 24 VG VS iG
2014/05/25 DOTA
在IIS服务器上以CGI方式运行Python脚本的教程
2015/04/25 Python
利用Python读取文件的四种不同方法比对
2017/05/18 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
2019/04/15 Python
Django将默认的SQLite更换为MySQL的实现
2019/11/18 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
2019/12/04 Python
详解css3使用transform出现字体模糊的解决办法
2020/10/16 HTML / CSS
html5中的一些标签学习(心得)
2016/10/18 HTML / CSS
丝芙兰波兰:Sephora.pl
2018/03/25 全球购物
remote接口和home接口主要作用
2013/05/15 面试题
小学音乐教学反思
2014/02/05 职场文书
护士节策划方案
2014/05/19 职场文书
政风行风整改报告
2014/11/06 职场文书
教师业务学习材料
2014/12/16 职场文书
奖励申请报告范文
2015/05/15 职场文书
2015年社区工会工作总结
2015/05/26 职场文书
该怎么书写道歉信?
2019/07/03 职场文书
Python手拉手教你爬取贝壳房源数据的实战教程
2021/05/21 Python
SQL Server使用PIVOT与unPIVOT实现行列转换
2022/05/25 SQL Server