PHP imagecreatefromjpeg保持方向 [英] PHP imagecreatefromjpeg while keeping orientation

查看:95
本文介绍了PHP imagecreatefromjpeg保持方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的图片上传网站上工作.我正在尝试从iPhone拍摄照片并将其上传到Web服务器.

I have been working on my image upload website. I am trying to take pictures from my IPhone and upload them to my web server.

我的文件可以很好地上传,但是我遇到的问题是我所有的图像都向左旋转90度.

My files are uploading fine, However the problem i am running into is all of my images rotate 90 degrees to the left.

我的图片上传过程

$imageObject = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);
imagejpeg($imageObject, $target_file, 75);

创建一个新图像并将其上传到我的Web目录.我创建了一个新图像,以删除所有EXIF数据(GPS位置,我的所有个人信息)

Creating a new image and uploading it to my web directory. I create a new image to remove all of the EXIF Data (GPS location, all of my personal information)

问题在于,当我上传图像时,它不会以纵向(6)保存文件.它实际上并没有保存任何方向信息.这是imagecreatefromjpeg的明显副作用.但是我所有的肖像风格图像都保存为横向格式.

The problem is that when i upload the image it does not save the file in portrait orientation (6). It doesn't actually save ANY orientation information. This being an obvious side effect of imagecreatefromjpeg. But all of my portrait style images save as landscape format.

我的问题是,在将定向数据保存到服务器后,是否可以将其简单地重新写入新图像中?

My question is, is there any way for me to simply re-write the orientation Data into the NEW image after it is saved to my server?

谢谢大家的光临!

推荐答案

您可以阅读exif信息并将其用于旋转或翻转图像.然后,您不再需要方向数据.

You can read the exif information and use that to rotate or flip your image. Then you don't need the orientation data anymore.

类似的东西:

$imageObject = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);

# Get exif information
$exif = exif_read_data($_FILES["fileToUpload"]["tmp_name"]);
# Add some error handling

# Get orientation
$orientation = $exif['Orientation'];

# Manipulate image
switch ($orientation) {
    case 2:
        imageflip($imageObject, IMG_FLIP_HORIZONTAL);
        break;
    case 3:
        $imageObject = imagerotate($imageObject, 180, 0);
        break;
    case 4:
        imageflip($imageObject, IMG_FLIP_VERTICAL);
        break;
    case 5:
        $imageObject = imagerotate($imageObject, -90, 0);
        imageflip($imageObject, IMG_FLIP_HORIZONTAL);
        break;
    case 6:
        $imageObject = imagerotate($imageObject, -90, 0);
        break;
    case 7:
        $imageObject = imagerotate($imageObject, 90, 0);
        imageflip($imageObject, IMG_FLIP_HORIZONTAL);
        break;
    case 8:
        $imageObject = imagerotate($imageObject, 90, 0); 
        break;
}

# Write image
imagejpeg($imageObject, $target_file, 75);

这篇关于PHP imagecreatefromjpeg保持方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆