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

简介: 【本文原创,谢绝转载】 一、出现的问题 这几天做了手机上传照片并裁出缩略图的接口的测试,发现不管怎么,生成的缩略图都是一片漆黑。:-( 然后就把这个缩略图类单拿出来进行测试,发现只要是手机拍出来的照片都不能进行缩略图的处理。

本文原创,谢绝转载

一、出现的问题

这几天做了手机上传照片并裁出缩略图的接口的测试,发现不管怎么,生成的缩略图都是一片漆黑。:-(
然后就把这个缩略图类单拿出来进行测试,发现只要是手机拍出来的照片都不能进行缩略图的处理。。。。

二、问题分析以及解决方案


经过群里的请教,发现问题可能是出现在文件的类型的判断上,因为png图片自带一个透明的图层,导致不能直接转换成jpg的文件,而手机排出的照片扩展名是jpg.
所以,得出的结论是 手机拍出的是jpg扩展名的png图片

由于扩展名是可以随意修改的,不是很能保证文件的信息的准确性,所以我们采用了 getimagesize 函数进行文件类型的获取。

//获取真实的图片类型
 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_max<1时为缩小比例;$sca_max>1时为最大长度(高或宽之中的最大值)
    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
}




目录
相关文章
|
4月前
|
Java 程序员 PHP
PHP对象和类
PHP对象和类
47 0
|
2月前
|
SQL 存储 安全
PHP 与现代 Web 应用的安全挑战与解决方案
随着 Web 应用的发展,PHP 作为一种广泛使用的服务器端脚本语言,面临着越来越复杂的安全挑战。本文探讨了当前 PHP 开发中常见的安全问题,并提供了相应的解决方案,帮助开发者构建更安全可靠的 Web 应用。 【7月更文挑战第8天】
56 1
|
5天前
|
PHP
PHP中的面向对象编程:理解类与对象
本文将深入探讨PHP中面向对象编程的核心概念——类与对象。通过实例讲解,帮助读者更好地理解如何在PHP中运用OOP编写更高效、可维护的代码。
20 9
|
30天前
|
PHP 开发者
PHP中的面向对象编程:掌握类与对象的精髓
探索PHP的面向对象编程世界,本文将带你了解如何通过创建和操作类来实例化对象。我们将深入讲解类的声明、构造函数的使用以及继承和多态性的概念。准备好,让我们一起在代码的海洋中航行,揭开PHP对象编程的神秘面纱!
|
2月前
|
JavaScript
vue 项目/备案网页/ip网页打包成 apk 安装到平板/手机(含vue项目跨域代理打包成apk后无法访问接口的解决方案)
vue 项目/备案网页/ip网页打包成 apk 安装到平板/手机(含vue项目跨域代理打包成apk后无法访问接口的解决方案)
65 0
|
2月前
|
PHP
如何在PHP中创建一个自定义的观察者类?
【7月更文挑战第2天】如何在PHP中创建一个自定义的观察者类?
18 0
|
2月前
|
PHP
PHP中,你可以使用class关键字来定义一个类
【7月更文挑战第2天】PHP中,你可以使用class关键字来定义一个类
19 0
|
3月前
|
PHP
PHP地方门户分类信息网站源码讯客分类信息系统源码(含手机版)
1.上传程序到网站根目录,访问http://域名/install/index.php 进行安装,不要直接打开网址,先直接安装; 2.安装完成后 后台恢复数据即可 默认帐号密码都是admin http://域名/admin/ 3.不要删除任何文件,因为删除文件或者修改代码可能造成错误
145 0
|
4月前
|
缓存 NoSQL PHP
【PHP 开发专栏】Redis 作为 PHP 缓存的解决方案
【4月更文挑战第30天】本文探讨了Redis作为PHP缓存的优势,如高性能、丰富数据结构、数据持久化和分布式支持。通过安装配置Redis、选择PHP客户端、执行读写操作及制定缓存策略实现缓存。应用场景包括页面、数据和会话缓存。但需注意数据一致性、过期时间、容量和安全问题,以确保应用稳定和安全。Redis能有效提升PHP应用响应速度和处理能力。
121 2
|
4月前
|
监控 网络协议 数据安全/隐私保护
手机socket套接字75秒超时问题及解决方案
手机socket套接字75秒超时问题及解决方案
45 0