【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时,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 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仍然流行的原因是其易用性、灵活性和强大的生态系统。1)易用性和简单语法使其成为初学者的首选。2)与web开发紧密结合,处理HTTP请求和数据库交互出色。3)庞大的生态系统提供了丰富的工具和库。4)活跃的社区和开源性质使其适应新需求和技术趋势。

PHP和Python都是高层次的编程语言,广泛应用于Web开发、数据处理和自动化任务。1.PHP常用于构建动态网站和内容管理系统,而Python常用于构建Web框架和数据科学。2.PHP使用echo输出内容,Python使用print。3.两者都支持面向对象编程,但语法和关键字不同。4.PHP支持弱类型转换,Python则更严格。5.PHP性能优化包括使用OPcache和异步编程,Python则使用cProfile和异步编程。

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器