如何處理/關閉 PHP 自動轉向圖片的功能

Photo By Mae Mu


在 Lumen 中

某些圖片會自動轉向

會發生這種情況通常是單眼等較專業相機拍出來的照片

EXIF 中出現 Orientation 或是 Orientation 不是 0

影像處理的 lib 就會自動依照 Orientation 的值做對應的轉向

如果 Orientation 是 6 則是右轉 90 度,就會變成直立的照片

如果 Orientation 是 8 則是反方向轉 90 度

後來發現這似乎不是 Lumen 或套件的問題

問題出在 GD or iMagick

翻了文件 GD 似乎無法針對這部分去做設定

iMagick 可以,但我不想為了這個特別去裝肥肥的 iMagick 啊


如果想避免這個情況

由於無法單一清除 Orientation meta 資訊

最簡單的應該是一開始就把 EXIF 資訊給抹除掉


不然可以參考以下的方式,雖然有點繞

但可以 work 就好 XD

與其說關閉轉向功能

這邊實作的是,他轉幾度,我就轉幾度回來 orz...

    // 如果照片中 exif 有 Orientation 資訊,則反轉
public function fixOrientation($imgObj, $image) {

$exif = @exif_read_data($image);
if (!$exif) {
return;
}

$orientation = ($exif && isset($exif['Orientation'])) ? $exif['Orientation'] : NULL;
switch($orientation) {
case 6: // rotate 90 degrees CW
$imgObj->rotate(-90);
break;
case 8: // rotate 90 degrees CCW
$imgObj->rotate(90);
break;
}
}


    $img = Image::make($image)->resize($max, $max, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });
    $this->fixOrientation($img, $image);
    $img->save($destinationPath.$name);



參考文章

How to stop PHP iMagick auto-rotating images based on EXIF 'orientation' data


Comments

Popular posts from this blog

CentOS7 如何新增/移除 Rich Rule for firewalld

mac 安裝 mtr

Howto use Postman test Cors