ChatGPT解决这个技术问题 Extra ChatGPT

PHP read_exif_data 和调整方向

如果方向关闭,我正在使用以下代码旋转上传的 jpeg 图像。我只遇到从 iPhone 和 Android 上传的图像的问题。

if(move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path . $newfilename)){
            chmod($upload_path . $newfilename, 0755);
            $exif = exif_read_data($upload_path . $newfilename);
            $ort = $exif['IFD0']['Orientation'];
            switch($ort)
            {

                case 3: // 180 rotate left
                    $image->imagerotate($upload_path . $newfilename, 180, -1);
                    break;


                case 6: // 90 rotate right
                    $image->imagerotate($upload_path . $newfilename, -90, -1);
                    break;

                case 8:    // 90 rotate left
                    $image->imagerotate($upload_path . $newfilename, 90, -1);
                    break;
            }
            imagejpeg($image, $upload_path . $newfilename, 100);
            $success_message = 'Photo Successfully Uploaded';
        }else{
            $error_count++;
            $error_message = 'Error: Upload Unsuccessful<br />Please Try Again';
        }

我从 jpeg 读取 EXIF 数据的方式有问题吗?它没有像预期的那样旋转图像。

这就是我运行 var_dump($exif); 时发生的情况。

array(41) {
    ["FileName"]=> string(36) "126e7c0efcac2b76b3320e6187d03cfd.JPG"
    ["FileDateTime"]=> int(1316545667)
    ["FileSize"]=> int(1312472)
    ["FileType"]=> int(2)
    ["MimeType"]=> string(10) "image/jpeg"
    ["SectionsFound"]=> string(30) "ANY_TAG, IFD0, THUMBNAIL, EXIF"
    ["COMPUTED"]=> array(8) {
        ["html"]=> string(26) "width="2048" height="1536""
        ["Height"]=> int(1536)
        ["Width"]=> int(2048)
        ["IsColor"]=> int(1)
        ["ByteOrderMotorola"]=> int(1)
        ["ApertureFNumber"]=> string(5) "f/2.8"
        ["Thumbnail.FileType"]=> int(2)
        ["Thumbnail.MimeType"]=> string(10) "image/jpeg" }
        ["Make"]=> string(5) "Apple"
        ["Model"]=> string(10) "iPhone 3GS"
        ["Orientation"]=> int(6)
        ["XResolution"]=> string(4) "72/1"
            ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["Software"]=> string(5) "4.3.5" ["DateTime"]=> string(19) "2011:09:16 21:18:46" ["YCbCrPositioning"]=> int(1) ["Exif_IFD_Pointer"]=> int(194) ["THUMBNAIL"]=> array(6) { ["Compression"]=> int(6) ["XResolution"]=> string(4) "72/1" ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["JPEGInterchangeFormat"]=> int(658) ["JPEGInterchangeFormatLength"]=> int(8231) } ["ExposureTime"]=> string(4) "1/15" ["FNumber"]=> string(4) "14/5" ["ExposureProgram"]=> int(2) ["ISOSpeedRatings"]=> int(200) ["ExifVersion"]=> string(4) "0221" ["DateTimeOriginal"]=> string(19) "2011:09:16 21:18:46" ["DateTimeDigitized"]=> string(19) "2011:09:16 21:18:46" ["ComponentsConfiguration"]=> string(4) "" ["ShutterSpeedValue"]=> string(8) "3711/949" ["ApertureValue"]=> string(9) "4281/1441" ["MeteringMode"]=> int(1) ["Flash"]=> int(32) ["FocalLength"]=> string(5) "77/20" ["SubjectLocation"]=> array(4) { [0]=> int(1023) [1]=> int(767) [2]=> int(614) [3]=> int(614) } ["FlashPixVersion"]=> string(4) "0100" ["ColorSpace"]=> int(1) ["ExifImageWidth"]=> int(2048) ["ExifImageLength"]=> int(1536) ["SensingMethod"]=> int(2) ["ExposureMode"]=> int(0) ["WhiteBalance"]=> int(0) ["SceneCaptureType"]=> int(0) ["Sharpness"]=> int(1) }
请注意,即使不需要旋转,此代码也会重新压缩源图像。
我现在的问题是需要旋转的图像没有被旋转。
执行 var_dump($exif) 以查看 android 手机以旋转数据的方式产生了什么。
好的,我清理了那里的垃圾场。明显地。方向字段不在“IFD0”部分,它是 $exif['COMPUTED']['Orientation'],值为 6。
$exif['方向'];对我来说很好。与 $exif['some_section']['Orientation']; 相比,这可能是一个更好的选择。

F
Firze

基于 Daniel 的代码,我编写了一个函数,它可以在必要时简单地旋转图像,而不需要重新采样。

广东

function image_fix_orientation(&$image, $filename) {
    $exif = exif_read_data($filename);
    
    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;
            
            case 6:
                $image = imagerotate($image, 90, 0);
                break;
            
            case 8:
                $image = imagerotate($image, -90, 0);
                break;
        }
    }
}

一条线版(GD)

function image_fix_orientation(&$image, $filename) {
    $image = imagerotate($image, array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($filename)['Orientation'] ?: 0], 0);
}

ImageMagick

function image_fix_orientation($image) {
    if (method_exists($image, 'getImageProperty')) {
        $orientation = $image->getImageProperty('exif:Orientation');
    } else {
        $filename = $image->getImageFilename();
        
        if (empty($filename)) {
            $filename = 'data://image/jpeg;base64,' . base64_encode($image->getImageBlob());
        }
        
        $exif = exif_read_data($filename);
        $orientation = isset($exif['Orientation']) ? $exif['Orientation'] : null;
    }
    
    if (!empty($orientation)) {
        switch ($orientation) {
            case 3:
                $image->rotateImage('#000000', 180);
                break;
            
            case 6:
                $image->rotateImage('#000000', 90);
                break;
            
            case 8:
                $image->rotateImage('#000000', -90);
                break;
        }
    }
}

对于 Imagick,我使用 getImageOrientation() 来检索方向,然后在旋转图像后,我通过 $image->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); 设置正确的 Exif 方向值。
您有 WideImage 的解决方案吗?
在某些情况下,即使使用转换后的原始图像,imagick 函数 getImageOrientation() 也无法正常工作。上面的代码完美运行。
在第一个版本(GD)中,我应该在调用此函数的地方为 &$image 传递什么?
对于不了解如何从本地文件传递 &$image 参数的人,可以这样使用: $im = @imagecreatefromjpeg($local_filename); image_fix_orientation($im, $local_filename); if ($im) { imagejpeg($im, $local_filename);图像销毁($im); }
F
Firze

imagerotate 的文档引用了与您使用的第一个参数不同的类型:

图像资源,由图像创建函数之一返回,例如 imagecreatetruecolor()。

下面是一个使用这个函数的小例子:

function resample($jpgFile, $thumbFile, $width, $orientation) {
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($jpgFile);
    $height = (int) (($width / $width_orig) * $height_orig);
    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image   = imagecreatefromjpeg($jpgFile);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    // Fix Orientation
    switch($orientation) {
        case 3:
            $image_p = imagerotate($image_p, 180, 0);
            break;
        case 6:
            $image_p = imagerotate($image_p, 90, 0);
            break;
        case 8:
            $image_p = imagerotate($image_p, -90, 0);
            break;
    }
    // Output
    imagejpeg($image_p, $thumbFile, 90);
}

由于某种原因,android 4.1.2 创建的图像不需要旋转,只需通过“imagecreatefromjpen()”加载图像,然后用“imagejpeg()”将其保存回来。你知道为什么吗?
F
Firze

对于那些上传图片的人来说,它的功能更简单,它只是在必要时自动旋转。

function image_fix_orientation($filename) {
    $exif = exif_read_data($filename);
    if (!empty($exif['Orientation'])) {
        $image = imagecreatefromjpeg($filename);
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;

            case 6:
                $image = imagerotate($image, 90, 0);
                break;

            case 8:
                $image = imagerotate($image, -90, 0);
                break;
        }

        imagejpeg($image, $filename, 90);
    }
}

将此答案制成一个简单的作曲家包,可以在 github 上找到(只有一种方法的类):github.com/diversen/image-auto-rotate
您使用了错误的度数值。在案例 6 中,您需要 90 度,在案例 8 中,您需要 -90 度。
非常有用的功能,如果有人看到此警告 Illegal IFD size,您可以使用 @ operator eg$exif = @exif_read_data($filename);
@user462990 此功能运行良好,但仅适用于本地提供的图像。将如何传递图像 url?我在 s3 上有一张图像,我需要它来操纵方向。
D
David Vielhuber

为什么没有人考虑镜像案例 2、4、5、7? exif定向土地还有4种情况:

https://i.stack.imgur.com/BFqgu.gif

这是一个采用文件名的完整解决方案:

function __image_orientate($source, $quality = 90, $destination = null)
{
    if ($destination === null) {
        $destination = $source;
    }
    $info = getimagesize($source);
    if ($info['mime'] === 'image/jpeg') {
        $exif = exif_read_data($source);
        if (!empty($exif['Orientation']) && in_array($exif['Orientation'], [2, 3, 4, 5, 6, 7, 8])) {
            $image = imagecreatefromjpeg($source);
            if (in_array($exif['Orientation'], [3, 4])) {
                $image = imagerotate($image, 180, 0);
            }
            if (in_array($exif['Orientation'], [5, 6])) {
                $image = imagerotate($image, -90, 0);
            }
            if (in_array($exif['Orientation'], [7, 8])) {
                $image = imagerotate($image, 90, 0);
            }
            if (in_array($exif['Orientation'], [2, 5, 7, 4])) {
                imageflip($image, IMG_FLIP_HORIZONTAL);
            }
            imagejpeg($image, $destination, $quality);
        }
    }
    return true;
}

优秀的解决方案。这是一个很好的观点,因为许多用户上传镜像图像并且他们的最终图像存在问题。
这个问题是非常错误的。 “PHP 团队”做到了一半?这似乎是一个解决方案。
B
Bernhard Barker

以防万一有人遇到这个。据我所知,上面的一些 switch 语句是错误的。

根据信息 here,它应该是:

switch ($exif['Orientation']) {
    case 3:
        $image = imagerotate($image, -180, 0);
        break;
    case 6:
        $image = imagerotate($image, 90, 0);
        break;
    case 8:
        $image = imagerotate($image, -90, 0);
        break;
} 

他们确实是不正确的。我编辑了它们,它们现在应该是正确的。
至少案例 6 对我来说仍然是错误的
C
Cat

值得一提的是,如果您从命令行使用 ImageMagick,您可以使用 -auto-orient 选项,该选项将根据现有的 EXIF 方向数据自动旋转图像。

convert -auto-orient /tmp/uploadedImage.jpg /save/to/path/image.jpg

请注意:如果 EXIF 数据在此过程之前已被剥离,则它不会像描述的那样工作。


B
Brad Root

我讨厌加入另一组方向值,但根据我使用上面列出的任何值的经验,当直接从 iPhone 上传纵向拍摄时,我总是会得到颠倒的图像。这是我最终得到的 switch 语句。

switch ($exif['Orientation']) {
        case 3:
            $image = imagerotate($image, -180, 0);
            break;

        case 6:
            $image = imagerotate($image, -90, 0);
            break;

        case 8:
            $image = imagerotate($image, 90, 0);
            break;
    }

M
MD. ABU TALHA

在这里,我正在解释整个事情,我使用 Laravel 并使用 Image Intervention Package。

首先,我得到我的图像并将其发送到我的另一个函数以调整大小和其他一些功能,如果我们不需要这个,你可以跳过......

使用我的控制器中的方法获取文件,

 public  function getImageFile(Request $request){
    $image = $request->image;
    $this->imageUpload($image);
}

现在,我将其发送以调整大小并获取图像名称和扩展名...

public function  imageUpload($file){
    ini_set('memory_limit', '-1');
    $directory = 'uploads/';
    $name = str_replace([" ", "."], "_", $file->getClientOriginalName()) . "_";
    $file_name = $name . time() . rand(1111, 9999) . '.' . $file->getClientOriginalExtension();
    //path set
    $img_url = $directory.$file_name;
    list($width, $height) = getimagesize($file);
    $h = ($height/$width)*600;
    Image::make($file)->resize(600, $h)->save(public_path($img_url));
    $this->image_fix_orientation($file,$img_url);
    return $img_url;
}

现在我调用我的图像方向功能,

 public function image_fix_orientation($file,$img_url ) {
    $data = Image::make($file)->exif();
    if (!empty($data['Orientation'])) {
        $image = imagecreatefromjpeg($file);
        switch ($data['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;

            case 6:
                $image = imagerotate($image, -90, 0);
                break;

            case 8:
                $image = imagerotate($image, 90, 0);
                break;
        }

        imagejpeg($image, $img_url, 90);
    }

}

就这样...


S
Sebastian Viereck

这是受@user462990 启发的我的 PHP 7 函数:

/**
 * @param string $filePath
 *
 * @return resource|null
 */
function rotateImageByExifOrientation(string $filePath)
{
    $result = null;

    $exif = exif_read_data($filePath);
    if (!empty($exif['Orientation'])) {
        $image = imagecreatefromjpeg($filePath);
        if (is_resource($image)) {
            switch ($exif['Orientation']) {
                case 3:
                    $result = imagerotate($image, 180, 0);
                    break;

                case 6:
                    $result = imagerotate($image, -90, 0);
                    break;

                case 8:
                    $result = imagerotate($image, 90, 0);
                    break;
            }
        }
    }

    return $result;
}

用法:

    $rotatedFile = rotateImageByExifOrientation($absoluteFilePath);
    if (is_resource($rotatedFile)) {
        imagejpeg($rotatedFile, $absoluteFilePath, 100);
    }

G
G.P.W.

jhead -autorot jpegfile.jpg

也是解决此问题的有用方法。

jhead 是 Linux 中的标准程序(使用 'sudo apt-get install jhead' 安装),此选项仅在需要时查看方向并正确无损地旋转图像。然后它还会正确更新 EXIF 数据。

通过这种方式,您可以以简单的一次性方式处理一个 jpeg(或文件夹中的多个 jpeg),从而永久修复旋转问题。

例如: jhead -autorot *.jpg 将以 OP 在初始问题中要求的方式修复整个 jpeg 图像文件夹。

虽然从技术上讲它不是 PHP,但我确实阅读了这个线程,然后改用我的 jhead 建议,从 PHP system() 调用中调用以实现我所追求的结果,这与 OP 一致:旋转图像,以便任何软件(如'fbi ' 在 Raspbian 中)可以正确显示它们。

鉴于此,我认为其他人可能会从了解 jhead 解决此问题的容易程度中受益,并在此处发布信息仅用于提供信息的目的 - 因为之前没有人提到过它。


您的答案被标记为低质量,因为它很短。尝试更深入地解释您的解决方案。
c
c0ld

我还使用了 orientate() 形式的干预,它完美无缺。

    $image_resize = Image::make($request->file('photo'));
    $image_resize->resize(1600, null,function ($constraint)
    {
        $constraint->aspectRatio();
    });
    $filename = $this->checkFilename();

    $image_resize->orientate()->save($this->photo_path.$filename,80);

D
Damien Bezborodow

Intervention Image 有一个方法orientate()

$img = Image::make('foo.jpg')->orientate();