讲师中心 微信公众号
AI工具推荐 视频效率加速

手机照片不能生成缩略图问题以及解决方案

千芳同学_3365

千芳同学_3365

发布时间:2016-05-23 08:39:24

|

2209人浏览过

|

来源于php中文网

原创

跳至

//获取真实的图片类型
 list($width, $height, $type, $attr) = getimagesize($this->sur_file);
  switch($type) {  
    case 1 :  
      $img_type = 'gif';  
      break;  
    case 2 :  
      $img_type = 'jpeg';  
      break;  
    case 3 :  
      $img_type = 'png';  
      break;  
    case 15 :  
      $img_type = 'wbmp';  
      break;  
    default :  
      return false;  
  }

//下面把修改后的生成缩略图的类贴出来,供大家指正~
/**
 * php生成缩略图类
 * 修改者 点点细雨 
 * 文章出处 : http://blog.csdn.net/diandianxiyu_geek
 * 2014-07-23 解决了图片类型不能正常识别的问题
 */
class thumb {

  public $sur_file; //读取的原图片
  public $des_file; //生成目标图片
  public $tem_file; //临时图片
  public $tag;  //缩略标签  0,默认,按固定的高宽生成  1,按比列或固定最大长度生成  -1,按某个宽度或某个高度缩小
  public $resize_width;  //$tag为0时,目标文件宽
  public $resize_height;  //$tag为0时,目标文件高
  public $sca_max; //$tag为1时,<0$sca_max1时为最大长度(高或宽之中的最大值)
  public $type;  //图片类型
  public $width;  //原图片宽
  public $height;  //原图片高
  public $size;	 //原图大小

  //构造函数

  public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax) {
    $this->sur_file = $surpic;
    $this->resize_width = $reswid;
    $this->resize_height = $reshei;
    $this->tag = $mark;
    $this->sca_max = $scamax;
    list($width, $height, $type, $attr) = getimagesize($this->sur_file);
    switch ($type) {
      case 1 :
        $img_type = 'gif';
        break;
      case 2 :
        $img_type = 'jpeg';
        break;
      case 3 :
        $img_type = 'png';
        break;
      case 15 :
        $img_type = 'wbmp';
        break;
      default :
        return false;
    }
    $this->type = $img_type; //获取图片类型
    $this->init_img(); //初始化图片
    $this->des_file = $despic; //目标图片地址
    $this->width = $width;
    $this->height = $height;
    $this->size = filesize($surpic);
    $this->new_img();
    imagedestroy($this->tem_file);
  }

  //图片初始化函数
  private function init_img() {
    if ($this->type == 'jpeg') {
      $this->tem_file = imagecreatefromjpeg($this->sur_file);
    } elseif ($this->type == 'jpg') {
      $this->tem_file = imagecreatefromjpeg($this->sur_file);
    } elseif ($this->type == 'gif') {
      $this->tem_file = imagecreatefromgif($this->sur_file);
    } elseif ($this->type == 'png') {
      $this->tem_file = imagecreatefrompng($this->sur_file);
    } elseif ($this->type == 'bmp') {
      $this->tem_file = imagecreatefrombmp($this->sur_file); //bmp.php中包含
    }
  }

  //图片生成函数
  private function new_img() {
    $ratio = ($this->width) / ($this->height); //原图比例
    $resize_ratio = ($this->resize_width) / ($this->resize_height); //缩略后比例
    $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新图片
    imagealphablending($newimg, false); //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
    imagesavealpha($newimg, true);
    if ($this->tag == 0) { //按固定高宽截取缩略图
      $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新图片
      if ($ratio >= $resize_ratio) {//即等比例下,缩略图的高比原图长,因此高不变
        imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this->height) * $resize_ratio), $this->height);
      } elseif ($ratio < $resize_ratio) {//即等比例下,缩略图的宽比原图长,因此宽不变
        imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width) / $resize_ratio));
      }
    } elseif ($this->tag == 1) { //按固定比例或最大长度缩小
      if ($this->sca_max < 1) { //按比例缩小
        $newimg = imagecreatetruecolor((($this->width) * ($this->sca_max)), (($this->height) * ($this->sca_max))); //生成新图片
        imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width) * ($this->sca_max)), (($this->height) * ($this->sca_max)), $this->width, $this->height);
      } elseif ($this->sca_max > 1) { //按某个最大长度缩小
        if ($ratio >= 1) { //宽比高长
          $newimg = imagecreatetruecolor($this->sca_max, ($this->sca_max / $ratio)); //生成新图片
          imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max, ($this->sca_max / $ratio), $this->width, $this->height);
        } else {
          $newimg = imagecreatetruecolor(($this->sca_max * $ratio), $this->sca_max); //生成新图片
          imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max * $ratio), $this->sca_max, $this->width, $this->height);
        }
      }
    } elseif ($this->tag == -1) { //按某个宽度或某个高度缩小
      if ($resize_ratio >= 1) {//新高小于新宽,则图片按新宽度缩小
        $newimg = imagecreatetruecolor($this->resize_width, ($this->resize_width / $ratio)); //生成新图片
        imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, ($this->resize_width / $ratio), $this->width, $this->height);
      } elseif ($resize_ratio < 1) {//新宽小于新高,则图片按新高度缩小
        $newimg = imagecreatetruecolor(($this->resize_height * $ratio), $this->resize_height); //生成新图片
        imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height * $ratio), $this->resize_height, $this->width, $this->height);
      }
    }

    //输出新图片
    if ($this->type == 'jpeg' || $this->type == 'jpg') {
      imagejpeg($newimg, $this->des_file);
    } elseif ($this->type == 'gif') {
      imagegif($newimg, $this->des_file);
    } elseif ($this->type == 'png') {
      imagepng($newimg, $this->des_file);
    } elseif ($this->type == 'bmp') {
      imagebmp($newimg, $this->des_file); //bmp.php中包含
    }
  }

#function new_img() end
}

                   

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
Laper
Laper Hot

Laper是专为编剧、导演和制片人推出的 AI 原生剧本创作工具。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

蛙蛙写作

一款AI论文写作工具,主要用于超级AI智能写作助手,适合需要提升相关任务效率的用户。

LibLibAI
LibLibAI Hot

一款AI视频创作工具,主要用于国内领先的AI创意平台,以海量模型、低门槛操作与“创作-分享-商业化”生态,让小白与专业创作者都能高效实现图文乃至视频创意表达,适合需要提升相关任务效率的用户。

音述AI
音述AI Hot

一款AI音频处理工具,主要用于音述AI是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

VibeKnow
VibeKnow Hot

一款AI视频创作工具,主要用于全球首个AI知识视频创作平台,文档、文章、网页,一键生成视频,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

相关专题

更多
Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

0

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

0

2026.09.23

Buffalo框架零基础入门教程
Buffalo框架零基础入门教程

本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。

0

2026.09.23

Conan创建软件包配方指南
Conan创建软件包配方指南

本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。

0

2026.09.22

Conan二进制包配置指南
Conan二进制包配置指南

本专题介绍Conan根据操作系统、编译器、架构和构建类型生成二进制包的方法,讲解Profile、Settings、Options及Package ID的作用,帮助管理不同平台和编译环境下的包版本。

0

2026.09.22

Conan私有仓库搭建教程
Conan私有仓库搭建教程

本专题系统的讲解Conan私有仓库的搭建流程,涵盖仓库服务部署、存储目录配置、用户认证、权限划分和远程地址添加,并介绍内部C++依赖包的上传、下载及版本维护方法。

0

2026.09.22

loomy官网入口地址合集
loomy官网入口地址合集

本专题汇总了 Loomy 桌面 AI 助理的官方入口地址合集及使用指南。提供 macOS 与 Windows 客户端下载 。Loomy 是讯飞推出的桌面级 AI 工作搭子,支持文件整理、数据分析、网页操作及通过飞书/钉钉远程操控电脑,助你高效完成本地办公任务 。

0

2026.09.22

NumPy常见函数使用方法
NumPy常见函数使用方法

本专题整理 NumPy 常见函数使用方法相关教程,覆盖函数大全、参数用法、数组运算、统计聚合、排序处理、where 条件筛选、linspace 创建数列等常用场景,帮助读者快速掌握 NumPy 函数调用思路和实际数据处理技巧。

0

2026.09.22

NumPy性能优化版本更新与常见报错排查
NumPy性能优化版本更新与常见报错排查

本专题整理 NumPy 性能优化、版本更新与常见报错排查相关教程,覆盖向量化计算、广播性能、内存布局、NumPy 2.0 升级、版本兼容冲突、安装导入报错、dtype 溢出、矩阵运算异常和 broadcasting 报错修复,帮助读者系统掌握 NumPy 性能调优与问题定位方法。

20

2026.09.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn