getimagesize()函数获取图片宽高取反

背景:
手机摄像头朝下拍摄,上传后,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,输出图片路径,覆盖原本图片路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值