本文原创,谢绝转载

一、出现的问题

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


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


经过群里的请教,发现问题可能是出如今文件的类型的推断上,由于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
}

【PHP缩略图类】手机照片不能生成缩略图问题以及解决方式的更多相关文章

  1. PHP 图片处理工具类(添加水印与生成缩略图)

    =================ImageTool.class.php================= <?php class ImageTool { private $imagePath; ...

  2. 不自动生成Android Dependencies的解决方式

    今天遇到的奇怪问题是网上下载的demo导入第三方包运行后Android: NoClassDefFoundError的错误,原因是第三方的jar包并没有打包进apk里,运行是肯定要出错的. 网上百度了N ...

  3. .NET 等宽、等高、等比例、固定宽高生成缩略图 类

    #region 根据原图片生成等比缩略图 /// <summary> /// 根据源图片生成缩略图 /// </summary> /// <param name=&quo ...

  4. spring boot:实现图片文件上传并生成缩略图(spring boot 2.3.1)

    一,为什么要给图片生成缩略图? 1, 用户上传的原始图片如果太大,不能直接展示在网站页面上, 因为不但流费server的流量,而且用户打开时非常费时间, 所以要生成缩略图. 2,服务端管理图片要注意的 ...

  5. 使用nginx生成缩略图

    nginx中可以使用 --with-http_image_filter_module 这个模块,今天发现在github上发现国人开发的一款模块 模块同时支持 Nginx 和 tengine 本ngin ...

  6. adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式

    在android应用开发无处不在SQLite数据库的身影.那么在开发中怎么使用adb命令操作数据库的功能呢? 以下我们将完整的介绍与数据库操作相关的命令集及当手机缺少sqlite3的时候的多种解决方式 ...

  7. 解决mybatis中 数据库column 和 类的属性名property 不一致的两种方式

    解决方式way1:resultMap (1)studentMapper.xml <!-- 当数据库的字段名 和 类的属性名 不一致的时候的解决方式:2种 way1--> <selec ...

  8. php 图片操作类,支持生成缩略图,添加水印,上传缩略图

    <?php class Image {     //类开始     public $originimage = ""; //源图片文件地址     public $image ...

  9. PHP生成缩略图的一个方法类(转)

    //使用如下类就可以生成图片缩略图 class resizeimage { //图片类型 var $type; //实际宽度 var $width; //实际高度 var $height; //改变后 ...

随机推荐

  1. socket 编程基础

    一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...

  2. Java学习----对象与对象之间的关系

    1.依赖 对象之间最弱的一种关联方式,是临时性的关联.代码中一般指由局部变量,函数参数,返回值建立的对于其他对象的调用关系. public class A { // 方法一 public void t ...

  3. Ubuntu 12.04 下安装配置 JDK 7(tar)

    第一步:下载jdk-7u45-linux-i586.tar.gz 到Orcale的JDK官网下载JDK7的tar包 第二步:解压安装 tar -zxvf ./jdk-7u45-linux-i586.t ...

  4. Zephyr-开发流程

    开发流程 前提1:检查你的Linux主机满足入门指南中规定的最低要求. 具体请参考 :  物联网操作系统-Zephyr 前提2: 确保SDK的环境变量和zephyr项目的环境变量. 终端执行: $ e ...

  5. IIS短文件名漏洞修补方法之一改注册表一个注意项

    1)1.png 为漏洞存在没有做任何修复的时候的扫描 修复:2) 修改注册表键值: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSy ...

  6. Node.js事件发射器

    在Node很多对象发出事件,例如net.Server每个同级连接到它,一个fs.readStream发出打开文件事件时,每次都发出一个事件. 它发出事件的所有对象都是events.EventEmitt ...

  7. 使用单调队列优化的 O(nm) 多重背包算法

    我搜索了一下,找到了一篇很好的博客,讲的挺详细:链接. 解析 多重背包的最原始的状态转移方程: 令 c[i] = min(num[i], j / v[i]) f[i][j] = max(f[i-1][ ...

  8. Google研究员Ilya Sutskever:成功训练LDNN的13点建议

    Google研究员Ilya Sutskever:成功训练LDNN的13点建议 摘要:本文由Ilya Sutskever(Google研究员.深度学习泰斗Geoffrey Hinton的学生.DNNre ...

  9. 零零碎碎搞了一天最后发现是ruby版本问题

    查来查去查不到问题,后来在stackoverflow看到: http://stackoverflow.com/questions/22352838/ruby-gem-install-json-fail ...

  10. 关于Unity3D中的版本管理 .

    关于Unity3D中的版本管理 使用Unity3D也有一段时间了,由于团队一直使用SVN进行版本管理,现总结一下: (1) Unity3D的二进制资源必须加锁进行版本控制,因为它没办法merge: ( ...