找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1065|回复: 0
打印 上一主题 下一主题

PHP生成图片缩略图类示例

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-14 05:28:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:
class App_image_helper {
  protected $;
  protected $;
  protected $;
  protected $;
  protected $;
  static  $
    = array(
      'jpeg' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png',
      'wbmp' => 'image/wbmp',
    );
  /**
   * 根据文件名,初始化图片,
   * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用
   * App_image_helper constructor.
   *
   * @param $fileName
   */
  public function __construct($fileName) {
    $this->$fileName;
    list($this->, $this->, $this->) = $this->getImageInfo($this->);
    $this->$this->getImageResource($this->);
  }
  /**
   * 根据图片路径获取相关宽、高、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->) {
        case self:['jpeg']:
          $image = imagecreatefromjpeg($fileName);
          break;
        case self:['gif']:
          $image = imagecreatefromgif($fileName);
          break;
        case self:['png']:
          $image = imagecreatefrompng($fileName);
          break;
        case self:['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->$this->) {
      if ( $width ) {
        $result = array($width, intval($width * $this->$this->));
      } elseif ( $percent ) {
        $result = array(intval($this->* $percent), intval($this->* $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->) : false;
    $size = $this->getSizeByScale(($percentOrWidth , 0, 0, 0, 0, $size[0], $size[1], $this->, $this->);
      } else {
        imagecopyresized($thumb, $this->, 0, 0, 0, 0, $size[0], $size[1], $this->, $this->);
      }
      $result = imagejpeg($thumb, $fileName, $quality);
    }
    return $result;
  }
}
PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/
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 生成文字png图片的代码
  • 使用PHP生成图片的缩略图的方法
  • PHP Imagick完美实现图片裁切、生成缩略图、添加水印
  • PHP使用内置函数生成图片的方法详解
  • php使用高斯算法实现图片的模糊处理功能示例
  • php解析base64数据生成图片的方法
  • php生成二维码图片方法汇总
  • PHP生成图片验证码功能示例
  • PHP实现随机生成水印图片功能
  • PHP基于GD库实现的生成图片缩略图函数示例
  • php生成条形码的图片的实例详解
  • PHP实现生成模糊图片的方法示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端