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读取数据库信息的几种方法
May 24 PHP
php 获取一个月第一天与最后一天的代码
May 16 PHP
PHP目录函数实现创建、读取目录教程实例
Jan 13 PHP
PHP5函数小全(分享)
Jun 06 PHP
解析PHP跨站刷票的实现代码
Jun 18 PHP
解析wamp5下虚拟机配置文档
Jun 27 PHP
PHP实现采集抓取淘宝网单个商品信息
Jan 08 PHP
smarty高级特性之对象的使用方法
Dec 25 PHP
PHP封装返回Ajax字符串和JSON数组的方法
Feb 17 PHP
YII框架中使用memcache的方法详解
Aug 02 PHP
yii2.0整合阿里云oss删除单个文件的方法
Sep 19 PHP
实例讲解PHP验证邮箱是否合格
Jan 28 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
海贼王:最美的悬赏令!
2020/03/02 日漫
php Static关键字实用方法
2010/06/04 PHP
PHP中$_SERVER的详细参数与说明介绍
2013/10/26 PHP
[原创]php求圆周率的简单实现方法
2016/05/30 PHP
利用PHP如何统计Nginx日志的User Agent数据
2019/03/06 PHP
JavaScript 原型学习总结
2010/10/29 Javascript
使用apply方法实现javascript中的对象继承
2013/12/16 Javascript
javascript实现微信分享
2014/12/23 Javascript
如何编写高质量JS代码
2014/12/28 Javascript
window.location.hash知识汇总
2015/11/09 Javascript
jQuery.ajax 跨域请求webapi设置headers的解决方案
2016/08/08 Javascript
基于jQuery实现左侧菜单栏可折叠功能
2016/12/27 Javascript
vue数据双向绑定原理解析(get &amp; set)
2017/03/08 Javascript
AngularJS发送异步Get/Post请求方法
2018/08/13 Javascript
layui数据表格跨行自动合并的例子
2019/09/02 Javascript
小程序接入腾讯位置服务的详细流程
2020/03/03 Javascript
JavaScript常用8种数组去重代码实例
2020/09/09 Javascript
python操作CouchDB的方法
2014/10/08 Python
python多重继承实例
2014/10/11 Python
Python中Continue语句的用法的举例详解
2015/05/14 Python
详解Python中的from..import绝对导入语句
2016/06/21 Python
利用python将json数据转换为csv格式的方法
2018/03/22 Python
Python使用pickle模块储存对象操作示例
2018/08/15 Python
python程序快速缩进多行代码方法总结
2019/06/23 Python
Python常用模块sys,os,time,random功能与用法实例分析
2020/01/07 Python
Python request post上传文件常见要点
2020/11/20 Python
Window10上Tensorflow的安装(CPU和GPU版本)
2020/12/15 Python
css3实现蒙版弹幕功能
2019/06/18 HTML / CSS
英国虚拟主机服务商:eUKhost
2016/08/16 全球购物
瑞典轮胎在线:Tirendo.se
2018/06/21 全球购物
zooplus德国:便宜地订购动物用品、动物饲料、动物食品
2020/05/06 全球购物
中青班党性分析材料
2014/02/16 职场文书
《台湾的蝴蝶谷》教学反思
2014/02/20 职场文书
学校周年庆活动方案
2014/08/22 职场文书
大学团日活动总结书
2015/05/11 职场文书
浅谈vue2的$refs在vue3组合式API中的替代方法
2021/04/18 Vue.js