当前位置:网站首页>getimagesize()函数获取图片宽高取反
getimagesize()函数获取图片宽高取反
2022-04-22 09:39:00 【Aring88】
背景:
手机摄像头朝下拍摄,上传后,getimagesize()获取图片信息会把宽高去反,面对这种情况有以下方法。
一、情况
exif_read_data()
- exif_read_data()函数是能获取图片详细摄像信息,包括GPS信息,所以以后图片上传前最好还是进行二次处理。
Array
(
[0] => 2304
[1] => 1728
[2] => 2
[3] => width="2304" height="1728"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
例如该图,getimagesize()获取到的图片宽高就取反了。
- 再通过exif_read_data()获取图片摄像信息
Array
(
[FileName] => phpvafVHu
[FileDateTime] => 1650523619
[FileSize] => 753179
[FileType] => 2
[MimeType] => image/jpeg
[SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, INTEROP
[COMPUTED] => Array
(
[html] => width="2304" height="1728"
[Height] => 1728
[Width] => 2304
[IsColor] => 1
[Thumbnail.FileType] => 2
[Thumbnail.MimeType] => image/jpeg
)
[ImageWidth] => 2304
[ImageLength] => 1728
[Orientation] => 8
其他信息无关这次分享
可以看到[Orientation]参数 ,参考php手册对该参数的描述
When the new update came out from Apple for iOS6 it provided the ability for iPad, iPod, and iPhones to be able to upload files from the device through Safari. Obviously this will open up an array of implementations where at one point it was just not possible.
The issue comes when a photo is uploaded it will be dependent on the location of the "button" when the photo was taken. Imagine if you will that you have your iPhone turned with the button at the top and you take a photo. The photo when uploaded to your server might be "upside down".
The following code will ensure that all uploaded photos will be oriented correctly upon upload:
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
What you do with the image resource from there is entirely up to you.
I hope that this helps you identify and orient any image that's uploaded from an iPad, iPhone, or iPod. Orientation for the photo is the key to knowing how to rotate it correctly.
得知,iphone,ipad等ios设备摄像头朝下拍摄时候,会有图片翻转点情况
二、解决
因为写在通用函数中,所以使用闭包,可以根据需要拆分
/**
*$img_source // 图片路径
* $deg // 图片反转角度
**/
$fun_imagerotate = function ($img_source,$deg){
$return_data = '';
$return_path = '';
$imge_info = @getimagesize($img_source);
// 如果图片格式为webp 则转换成jpeg
if ($imge_info[2]===18){
$img = new \Imagick($img_source);
$img->setImageFormat('jpeg');
$return_data = imagecreatefromjpeg($img_source);
$img_id = imagerotate($return_data,$deg,0);
imagejpeg($img_id,$img_source);
}else{
switch ($imge_info[2]) {
case 1:
$return_data =imagecreatefromgif($img_source);
$img_id = imagerotate($return_data,$deg,0);
$return_path = imagegif($img_id,$img_source);
break;
case 2:
$return_data = imagecreatefromjpeg($img_source);
$img_id = imagerotate($return_data,$deg,0);
$return_path = imagejpeg($img_id,$img_source);
break;
case 3:
$return_data = imagecreatefrompng($img_source);
$img_id = imagerotate($return_data,$deg,0);
$return_path = imagepng($img_id,$img_source);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
}
return $img_source;
};
$exif = exif_read_data($imgSrc);
if(isset($exif)&&!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$imgSrc = $fun_imagerotate($imgSrc,90);
break;
case 3:
$imgSrc = $fun_imagerotate($imgSrc,180);
break;
case 6:
$imgSrc = $fun_imagerotate($imgSrc,-90);
break;
}
}
- 其中包含两个思路
- 1,判断图片类型
- 2,旋转图片
- imagerotate(图片源,旋转角度,0)
- 所以需要获取图片数据
- 3,输出图片路径,覆盖原本图片路径
版权声明
本文为[Aring88]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43272542/article/details/124321385
边栏推荐
- Beyond iterm! Known as the next generation terminal artifact, I can't put it down after using it!
- L2-032 彩虹瓶 (25 分)(栈
- 杰理之AI Server【篇】
- ShardingSphere简介与分表使用
- Detailed explanation of p-type mos tube switch circuit and working principle - Kia MOS tube
- Zhezheng nail scanning code login
- Unity Editor Hierarchy下拉菜单扩展
- QT布局管理复习
- L3-004 肿瘤诊断 (30 分)(三维 bfs
- 视觉轮式里程计紧耦合
猜你喜欢

mySQL基础记录

Design example of large range continuous adjustable (0 ~ 45V) low power stabilized voltage power supply based on MOSFET control

APP优化及积分榜进阶下篇【MUI+Flask+MongoDB】

2022起重机司机(限桥式起重机)考试题库及在线模拟考试

项目实训-读报僵尸

Transform based deep learning target detection (Detr model)

在线CSV转YAML工具

Transformer模型中应用的各类位置编码

经典场效应管如何快速关断技巧-KIA MOS管

2022年熔化焊接与热切割操作证考试题模拟考试平台操作
随机推荐
Depth first search (I): middle order traversal of binary tree (force buckle)
MOS tube and MOS tube driving circuit case analysis - Kia MOS tube
拜登宣布再向乌克兰提供8亿美元安全援助
Kernel pwn 基础教程之 Heap Overflow
Qtabelwidget instance
Beyond iterm! Known as the next generation terminal artifact, powerful!
【Flutter 专题】125 图解自传 ACE_ICON.ttf 图标库
杰理之通常影响CPU性能测试结果的因素有:【篇】
L2-030 Icelander (25 points) (nearest public ancestor)
L3-002 special stack (30 points) (two points stack)
MOS tube driving circuit and precautions - Kia MOS tube
Review of QT layout management
Aardio - [library] webp picture conversion
How to realize synchronous monitoring of etcd
APP优化及积分榜进阶下篇【MUI+Flask+MongoDB】
L3-004 肿瘤诊断 (30 分)(三维 bfs
push()与pop()的使用
在销量压力下,国产手机开始降价了,但还没有放下最后的面子
视觉轮式里程计紧耦合
Computer dust removal and mechanical hard disk installation records