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

千芳同学_3365

千芳同学_3365

2016-05-23

2229人浏览

原创

跳至

//获取真实的图片类型
 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时,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 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 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 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
}

                   

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

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

相关专题

更多
Uvicorn 报错与问题解决教程
Uvicorn 报错与问题解决教程

本专题汇总了Uvicorn服务器常见问题与报错解决的教程合集,涵盖启动失败、端口占用、模块导入错误、事件循环冲突及热重载失效等典型故障的排查思路。同时整理了多进程与Worker配置、SSL/HTTPS证书报错、跨域与代理配置等实战解决方案,助你快速定位并解决Uvicorn运行中的各类问题。

2026.11.02

0

14

Uvicorn使用教程
Uvicorn使用教程

本专题汇总了Uvicorn使用教程与启动运行指南,涵盖其作为Python ASGI服务器的定位与特点,以及命令行启动(uvicorn main:app --reload)、参数配置(host/port/workers)、程序内嵌运行与Gunicorn结合部署等核心用法。同时整理了热重载调试、日志配置与生产环境部署优化等技巧,助你快速掌握Uvicorn的运行与配置。

2026.11.02

0

17

Uvicorn安装配置教程
Uvicorn安装配置教程

本专题汇总了Uvicorn的安装配置教程与部署指南,涵盖通过pip安装Uvicorn、启动FastAPI/Starlette应用、开发模式热重载(--reload)及多进程工作模式配置等核心操作。同时整理了结合Gunicorn的生产部署方案、HTTPS与反向代理配置及性能调优技巧,助你快速完成Uvicorn服务的安装与上线部署。

2026.11.02

0

17

Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

100

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

80

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

240

10

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

140

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

140

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

100

15

热门下载

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

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.1万人学习