php封装的图片(缩略图)处理类完整实例


Posted in PHP onOctober 19, 2016

本文实例讲述了php封装的图片(缩略图)处理类。分享给大家供大家参考,具体如下:

<?php
//图片处理工具类
class Image{
    //属性
    private $thumb_width; //缩略图的宽
    private $thumb_height;
    //错误属性
    public $thumb_error;
    //构造方法
    public function __construct($width = 0,$height = 0){
      $this->thumb_width = ($width == 0) ? $GLOBALS['config']['admin_goods_thumb']['width'] : $width;
      $this->thumb_height = ($height == 0) ? $GLOBALS['config']['admin_goods_thumb']['height'] : $height;
    }
    /*
     * 制作缩略图
     * @param1 string $src,原图路径,/uploads/20150122101010abcdef.gif
     * @param2 string $path,缩略图保存路径/uploads/thumb_20150122101010abcdef.gif
     * @return 缩略图的名字
    */
    public function makeThumb($src,$path){
      //判断原图是否存在
      if(!file_exists($src)){
        $this->thumb_error = '原图不存在!';
        return false;
      }
      //打开原图资源
      //获取能够使用的后缀
      $ext = $this->getFunctionName($src); //gif
      //拼凑函数名
      $open = 'imagecreatefrom' . $ext;    //imagecreatefromgif
      $save = 'image' . $ext;          //imagegif
      //如果不清楚;echo $open,$save;exit;
      //可变函数打开原图资源
      $src_img = $open($src); //利用可变函数打开图片资源
      //imagecreatefromgif($src)
      //缩略图资源
      $dst_img = imagecreatetruecolor($this->thumb_width,$this->thumb_height);
      //背景色填充白色
      $dst_bg_color = imagecolorallocate($dst_img,255,255,255);
      imagefill($dst_img,0,0,$dst_bg_color);
      //宽高比确定宽高
      $dst_size = $this->thumb_width / $this->thumb_height;
      //获取原图数据
      $file_info = getimagesize($src);
      $src_size = $file_info[0]/$file_info[1];
      //求出缩略图宽和高
      if($src_size > $dst_size){
        //原图宽高比大于缩略图
        $width = $this->thumb_width;
        $height = round($width / $src_size);
      }else{
        $height = $this->thumb_height;
        $width = round($height * $src_size);
      }
      //求出缩略图起始位置
      $dst_x = round($this->thumb_width - $width)/2;
      $dst_y = round($this->thumb_height - $height)/2;
      //制作缩略图
      if(imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$width,$height,$file_info[0],$file_info[1])){
        //采样成功:保存,将文件保存到对应的路径下
        $thumb_name = 'thumb_' . basename($src);
        $save($dst_img,$path . '/' . $thumb_name);
        //保存成功
        return $thumb_name;
      }else{
        //采样失败
        $this->thumb_error = '缩略图采样失败!';
        return false;
      }
    }
    /*
     * 获取文件要调用的函数名
     * @param1 string $file,文件名字
     * @return 通过文件后缀名得到的函数字符串
    */
    private function getFunctionName($file){
      //得到文件的后缀
      $file_info = pathinfo($file);
      $ext = $file_info['extension']; //后缀:gif,png,jpg,jpeg,pjpeg
      //imagecreatefromgif,imagecreatefromjpeg,imagecreatefrompng
      //定义一个数组保存函数名
      $func = array(
        'gif' => 'gif',
        'png' => 'png',
        'jpg' => 'jpeg',
        'jpeg' => 'jpeg',
        'pjpeg' => 'jpeg'
      );
      //返回值
      return $func[$ext];
    }
}

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

PHP 相关文章推荐
php UTF8 文件的签名问题
Oct 30 PHP
使用PHP提取视频网站页面中的FLASH地址的代码
Apr 17 PHP
php获取post中的json数据的实现方法
Jun 08 PHP
VIM中设置php自动缩进为4个空格的方法详解
Jun 14 PHP
ThinkPHP实现多数据库连接的解决方法
Jul 01 PHP
PHP中Memcache操作类及用法实例
Dec 12 PHP
PHP实现的蚂蚁爬杆路径算法代码
Dec 03 PHP
浅谈PHP的$_SERVER[SERVER_NAME]
Feb 04 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
PHP封装的非对称加密RSA算法示例
May 28 PHP
PHP多个图片压缩成ZIP的方法
Aug 18 PHP
通过PHP设置BugFree获取邮箱通知
Apr 25 PHP
php封装的表单验证类完整实例
Oct 19 #PHP
php魔术方法功能与用法实例分析
Oct 19 #PHP
php封装的smartyBC类完整实例
Oct 19 #PHP
php封装的smarty类完整实例
Oct 19 #PHP
PHP内存缓存功能memcached示例
Oct 19 #PHP
PHP实现上传图片到 zimg 服务器
Oct 19 #PHP
php通过会话控制实现身份验证实例
Oct 18 #PHP
You might like
php5.3不能连接mssql数据库的解决方法
2014/12/27 PHP
理解php依赖注入和控制反转
2016/05/11 PHP
浅谈php使用curl模拟多线程发送请求
2019/03/08 PHP
PHP MVC框架中类的自动加载机制实例分析
2019/09/18 PHP
载入进度条 效果
2006/07/08 Javascript
用JavaScript页面不刷新时全选择,全删除(GridView)
2009/04/14 Javascript
用jQuery扩展自写的 UI导航
2010/01/13 Javascript
JS维吉尼亚密码算法实现代码
2010/11/09 Javascript
javascript仿php的print_r函数输出json数据
2013/09/13 Javascript
Jquery判断$(&quot;#id&quot;)获取的对象是否存在的方法
2013/09/25 Javascript
Jquery UI实现一次拖拽多个选中的元素操作
2020/12/01 Javascript
node.js 和HTML5开发本地桌面应用程序
2016/12/13 Javascript
基于 Vue 的树形选择组件的示例代码
2017/08/18 Javascript
inner join 内联与left join 左联的实例代码
2017/09/18 Javascript
BootStrap点击保存后实现模态框自动关闭的思路(模态框)
2017/09/26 Javascript
AngularJS使用ng-repeat遍历二维数组元素的方法详解
2017/11/11 Javascript
Vue运用transition实现过渡动画
2019/05/06 Javascript
es6中Promise 对象基本功能与用法实例分析
2020/02/23 Javascript
uniapp开发小程序实现滑动页面控制元素的显示和隐藏效果
2020/12/10 Javascript
[10:39]DOTA2上海特级锦标赛音乐会纪录片
2016/03/21 DOTA
Python zip()函数用法实例分析
2018/03/17 Python
实例讲解python中的序列化知识点
2018/10/08 Python
django解决跨域请求的问题详解
2019/01/20 Python
Python基于mysql实现学生管理系统
2019/02/21 Python
Pytorch修改ResNet模型全连接层进行直接训练实例
2019/09/10 Python
Python模块常用四种安装方式
2020/10/20 Python
python爬虫用scrapy获取影片的实例分析
2020/11/23 Python
PyCharm常用配置和常用插件(小结)
2021/02/06 Python
美国领先的户外服装与装备用品店:Moosejaw
2016/08/25 全球购物
斯洛伐克最大的婴儿食品和用品网上商店:Feedo.sk
2020/12/21 全球购物
高校优秀辅导员事迹材料
2014/05/07 职场文书
总经理岗位职责说明书
2014/07/30 职场文书
破坏寝室公物检讨书
2014/11/17 职场文书
朋友圈早安励志语录!
2019/07/08 职场文书
2019年幼儿园家长接送责任书
2019/10/29 职场文书
Python多线程 Queue 模块常见用法
2021/07/04 Python