PHP生成图片缩略图类示例

 更新时间:2017年01月12日 10:22:08   作者:lx_3278@126  
这篇文章主要介绍了PHP生成图片缩略图类,可实现针对图形文件的属性获取、转换、文件生成等功能,非常简便实用,需要的朋友可以参考下

本文实例讲述了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;
  }
}

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img

在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo

在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • php使用simple_html_dom解析HTML示例

    php使用simple_html_dom解析HTML示例

    这篇文章主要介绍了php使用simple_html_dom解析HTML的方法,实例分析了php针对dom节点操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • PHP动态规划解决0-1背包问题实例分析

    PHP动态规划解决0-1背包问题实例分析

    这篇文章主要介绍了PHP动态规划解决0-1背包问题,实例分析了背包问题的原理与实现技巧,需要的朋友可以参考下
    2015-03-03
  • 为何说PHP引用是个坑,要慎用

    为何说PHP引用是个坑,要慎用

    在PHP 中引用的意思是用不同的名字访问同一个变量内容。与在C语言中的指针不同:例如不能对引用做指针运算,引用并不是实际的内存地址。下面这篇文章主要给大家介绍了关于为何说PHP引用是个坑,要慎用的相关资料,需要的朋友可以参考下。
    2018-04-04
  • PHP获取中国时间(上海时区时间)及美国时间的方法

    PHP获取中国时间(上海时区时间)及美国时间的方法

    这篇文章主要介绍了PHP获取中国时间(上海时区时间)及美国时间的方法,涉及php时区选择及日期时间相关操作技巧,非常简单实用,需要的朋友可以参考下
    2017-02-02
  • 用穿越火线快速入门php面向对象

    用穿越火线快速入门php面向对象

    php目前已经成为国内的主流web developer的首选开发语言,其强大的面向对象容易使初学者找不到思绪,一头雾水,借助此文希望对初学者有所帮助,假设Crossfire用php来开发
    2012-02-02
  • PHP过滤黑名单关键字的方法

    PHP过滤黑名单关键字的方法

    这篇文章主要介绍了PHP过滤黑名单关键字的方法,通过保存过滤关键字到txt文件,再读取txt文件内容进行正则匹配替换来实现过滤功能,需要的朋友可以参考下
    2014-12-12
  • php实现根据url自动生成缩略图的方法

    php实现根据url自动生成缩略图的方法

    这篇文章主要介绍了php实现根据url自动生成缩略图的方法,详细讲述了相关库的使用、配置文件写法及类文件的用法示例,需要的朋友可以参考下
    2014-09-09
  • php防止sql注入简单分析

    php防止sql注入简单分析

    这篇文章主要介绍了php防止sql注入的方法,简单分析了通过stripslashes及mysql_real_escape_string函数进行字符转移处理的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • php生成xml简单实例代码

    php生成xml简单实例代码

    使用PHP DOMDocument创建动态XML文件
    2009-12-12
  • php实现的一段简单概率相关代码

    php实现的一段简单概率相关代码

    这篇文章主要介绍了php实现的一段简单概率相关代码,通过数字的运算分析概率统计的原理与使用技巧,需要的朋友可以参考下
    2016-05-05

最新评论