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.MVC的模板标签系统(一)
Sep 05 PHP
phpmyadmin 常用选项设置详解版
Mar 07 PHP
MyEclipse常用配置图文教程
Sep 11 PHP
PHP使用ffmpeg给视频增加字幕显示的方法
Mar 12 PHP
php实现判断访问来路是否为搜索引擎机器人的方法
Apr 15 PHP
php简单统计字符串单词数量的方法
Jun 19 PHP
PHP的几个常用加密函数
Feb 03 PHP
Symfony2 session用法实例分析
Feb 04 PHP
php变量与JS变量实现不通过跳转直接交互的方法
Aug 25 PHP
php和asp语法上的区别总结
May 12 PHP
ThinkPHP框架结合Ajax实现用户名校验功能示例
Jul 03 PHP
php正则表达式使用方法整理集合
Jan 31 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魔术常量与魔术方法的详解
2013/06/13 PHP
Linux服务器下PHPMailer发送邮件失败的问题解决
2017/03/04 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
2018/05/30 PHP
php使用QueryList轻松采集js动态渲染页面方法
2018/09/11 PHP
JQuery 选择器 xpath 语法应用
2010/05/13 Javascript
怎样在JavaScript里写一个swing把数据插入数据库
2012/12/10 Javascript
js格式化时间和js格式化时间戳示例
2014/02/10 Javascript
javascript中键盘事件用法实例分析
2015/01/30 Javascript
jquery实现相册一下滑动两次的方法
2015/02/09 Javascript
javascript实现百度地图鼠标滑动事件显示、隐藏
2015/04/02 Javascript
微信内置浏览器私有接口WeixinJSBridge介绍
2015/05/25 Javascript
javascript跨域总结之window.name实现的跨域数据传输
2015/11/01 Javascript
JavaScript必知必会(七)js对象继承
2016/06/08 Javascript
详解XMLHttpRequest(一)同步请求和异步请求
2016/09/14 Javascript
js输入框使用正则表达式校验输入内容的实例
2017/02/12 Javascript
基于JQuery和原生JavaScript实现网页定位导航特效
2017/04/03 jQuery
angular.js实现购物车功能
2017/10/23 Javascript
js面向对象方式实现拖拽效果
2021/03/03 Javascript
windows下wxPython开发环境安装与配置方法
2014/06/28 Python
python数据结构之图深度优先和广度优先实例详解
2015/07/08 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
python3.6 实现AES加密的示例(pyCryptodome)
2018/01/10 Python
Django 登陆验证码和中间件的实现
2018/08/17 Python
详解python3安装pillow后报错没有pillow模块以及没有PIL模块问题解决
2019/04/17 Python
Python面向对象之Web静态服务器
2019/09/03 Python
Python 批量读取文件中指定字符的实现
2020/03/06 Python
使用phonegap获取位置信息的实现方法
2017/03/31 HTML / CSS
Hotels.com中国区:好订网
2016/08/18 全球购物
澳大利亚拥有最佳跳伞降落点和最好服务的跳伞项目运营商:Skydive Australia
2018/03/05 全球购物
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
毕业寄语大全
2014/04/09 职场文书
大学生实习鉴定评语
2014/04/25 职场文书
2015年结对帮扶工作总结
2015/05/04 职场文书
《红领巾真好》教学反思
2016/02/16 职场文书
制作能在nginx和IIS中使用的ssl证书
2021/06/21 Servers
MySQL安装失败的原因及解决步骤
2022/06/14 MySQL