二、问题分析以及解决方案
【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}

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

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

热门话题

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

在html5中,width的意思是宽度,width属性定义元素内容区的宽度,在内容区外面可以增加内边距、边框和外边距,只需要给元素设置“元素{width:数值}”即可。

Vue中如何对图片进行压缩和格式转换?在前端开发中,经常会遇到需要对图片进行压缩和格式转换的需求。特别是在移动端的开发中,为了提高页面加载速度和节省用户流量,对图片进行压缩和格式转换是很关键的。而在Vue框架中,我们可以通过一些工具库来实现对图片的压缩和格式转换。使用compressor.js库进行压缩compressor.js是一款用于压缩图片的JavaS

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

本篇文章带大家解读vue源码,来介绍一下Vue2中为什么可以使用 this 访问各种选项中的属性,希望对大家有所帮助!

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。
