0

0

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

PHP中文网

PHP中文网

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

|

2189人浏览过

|

来源于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
}

                   

Batch GPT
Batch GPT

使用AI批量处理数据、自动执行任务

下载

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

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

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

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号