简体   繁体   English

PHP imagecreatefromjpeg保持方向

[英]PHP imagecreatefromjpeg while keeping orientation

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. 我正在尝试从iPhone拍摄照片并将其上传到Web服务器。

My files are uploading fine, However the problem i am running into is all of my images rotate 90 degrees to the left. 我的文件上传正常,但是我遇到的问题是我所有图像向左旋转90度。

My Image upload process 我的图片上传过程

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

Creating a new image and uploading it to my web directory. 创建一个新图像并将其上传到我的Web目录。 I create a new image to remove all of the EXIF Data (GPS location, all of my personal information) 我创建了一个新图像以删除所有EXIF数据(GPS位置,我的所有个人信息)

The problem is that when i upload the image it does not save the file in portrait orientation (6). 问题是,当我上传图像时,它不会以纵向(6)保存文件。 It doesn't actually save ANY orientation information. 它实际上并没有保存任何方向信息。 This being an obvious side effect of imagecreatefromjpeg. 这是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? 我的问题是,在将方向数据保存到服务器后,是否可以将其简单地重新写入新图像中?

Thank you all for your time! 谢谢大家的时间!

You can read the exif information and use that to rotate or flip your image. 您可以阅读exif信息,并使用它来旋转或翻转图像。 Then you don't need the orientation data anymore. 然后,您不再需要方向数据。

Something like: 就像是:

$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);

Create Image maintains Aspect Ratio: 创建图像保持宽高比:

Answer is Here 答案在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM