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 相关文章推荐
PHP5中MVC结构学习
Oct 09 PHP
Win2000+Apache+MySql+PHP4+PERL安装使用小结
Oct 09 PHP
PHP 进程锁定问题分析研究
Nov 24 PHP
基于php实现长连接的方法与注意事项的问题
May 10 PHP
基于PHP CURL用法的深入分析
Jun 09 PHP
PHP借助phpmailer发送邮件
May 11 PHP
php导出生成word的方法
Dec 25 PHP
微信红包随机生成算法php版
Jul 21 PHP
PHP入门教程之数组用法汇总(创建,删除,遍历,排序等)
Sep 11 PHP
php文件管理基本功能简单操作
Jan 16 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
Aug 01 PHP
如何用PHP实现分布算法之一致性哈希算法
May 26 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
php访问数组最后一个元素的函数end()用法
2015/03/18 PHP
Thinkphp关闭缓存的方法
2015/06/26 PHP
TNC vs IO BO3 第二场2.13
2021/03/10 DOTA
javascript十个最常用的自定义函数(中文版)
2009/09/07 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
javascript开发随笔二 动态加载js和文件
2011/11/25 Javascript
同域jQuery(跨)iframe操作DOM(示例代码)
2013/12/13 Javascript
js捕捉键盘事件和按键键值的方法
2016/10/10 Javascript
JavaScript实现时钟滴答声效果
2017/01/29 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
jQuery控制input只能输入数字和两位小数的方法
2019/05/16 jQuery
Python collections模块实例讲解
2014/04/07 Python
Python中使用装饰器来优化尾递归的示例
2016/06/18 Python
对命令行模式与python交互模式介绍
2018/05/12 Python
Python之list对应元素求和的方法
2018/06/28 Python
python计算两个数的百分比方法
2018/06/29 Python
python3 打开外部程序及关闭的示例
2018/11/06 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
2019/01/29 Python
Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例
2019/10/17 Python
在Windows上安装和配置 Jupyter Lab 作为桌面级应用程序教程
2020/04/22 Python
opencv 查找连通区域 最大面积实例
2020/06/04 Python
python切割图片的示例
2020/11/12 Python
教师实习期自我鉴定
2013/10/06 职场文书
模具设计与制造专业推荐信
2014/02/16 职场文书
校园联欢晚会主持词
2014/03/17 职场文书
班级年度安全计划书
2014/05/01 职场文书
幼儿园辞职书
2015/02/26 职场文书
搬迁通知
2015/04/20 职场文书
学生乘坐校车安全责任书
2015/05/11 职场文书
2015年行政执法工作总结
2015/05/23 职场文书
2016大学军训通讯稿
2015/11/25 职场文书
JavaScript实现淘宝商品图切换效果
2021/04/29 Javascript
tp5使用layui实现多个图片上传(带附件选择)的方法实例
2021/11/17 PHP
草系十大最强宝可梦,纸片人上榜,榜首大家最熟悉
2022/03/18 日漫
navicat 连接Ubuntu虚拟机的mysql的操作方法
2022/04/02 MySQL
python热力图实现的完整实例
2022/06/25 Python