# PHP识别相片是否是颠倒的，并且重新摆正相片

**URL:** <https://www.shiqidu.com/t/topic/559>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2019 年3 月 1 日 03:23 UTC](https://www.shiqidu.com/t/topic/559 "2019-03-01T03:23:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2019 年3 月 1 日 03:23 UTC](https://www.shiqidu.com/t/topic/559/1 "2019-03-01T03:23:33Z")

</div>

从Safari上传图片时有可能你服务器得到的照片是倒立的（取决于拍照按钮的位置），下面的代码可以确保所有上传的照片都是正确位置。

```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;
    }
}

```
