ministryofweb/image-fix-exif-orientation

根据存储在Exif数据中的图像方向自动旋转图像。支持GD和ImageMagick/IMagick。

1.0.0 2021-03-08 10:25 UTC

This package is auto-updated.

Last update: 2024-09-08 18:17:53 UTC


README

此库会根据存储在Exif数据中的图像方向自动旋转图像。支持GD和ImageMagick/IMagick。

要求

  • PHP 7.3+ 编译时需启用 --enable-exif
  • 至少需要GD或IMagick扩展中的一个

许可证

此库采用MIT许可证。

安装

composer require mjaschen/image-fix-exif-orientation

用法

以下示例也位于 examples 子目录中,可以直接从那里运行。

<?php

declare(strict_types=1);

use MinistryOfWeb\ImageFixExifOrientation\Fixer\Gd;
use MinistryOfWeb\ImageFixExifOrientation\Fixer\ImageMagick;
use MinistryOfWeb\ImageFixExifOrientation\Image;
use MinistryOfWeb\ImageFixExifOrientation\OrientationFixer;
use MinistryOfWeb\ImageFixExifOrientation\Output\Filesystem;
use MinistryOfWeb\ImageFixExifOrientation\Output\ImageString;

// please initialize Composer vendor directory first (by running `composer install`)
require_once dirname(__DIR__) . '/vendor/autoload.php';

// (1) load existing image from file system; it's possible to initialize an Image
// instance from a string as well
$image = new Image(dirname(__DIR__) . '/tests/fixtures/Landscape_3.jpg');
//$image = Image::fromString($imageData);

// (2) initialize Fixer class, can be either GD oder ImageMagick,
// depending on what image manipulation extension is preferred.
$fixer = new Gd(90);
//$fixer = new ImageMagick(90);

// (3) initialize output class, in this case we write to a temporary file
// in the file system; getting the image data as a string is also possible
$output = new Filesystem(tempnam(sys_get_temp_dir(), 'ministryofweb-exif-fix') . '.jpg');
//$output = new ImageString();

// (4) initialize the fixer
$fixer = new OrientationFixer($fixer, $output);

// (5) fix the image
$fixer->fix($image);

echo 'Fixed image: ' . $output->getFile() . PHP_EOL;

开发

使用以下命令运行单元测试

./vendor/bin/phpunit

有一些测试是用测试图像运行的,并创建了修正版本。可以在运行测试后通过用网络浏览器打开 tests/fixtures/fixtures.html 来检查这些图像。

有两组图像(横向,纵向),每组提供九种可能的Exif方向值(0-8)的图像各一张。所以总共有18张测试图像。

测试图像

测试使用Dave Perret的图像(https://github.com/recurser/exif-orientation-examples)。它们采用MIT许可证。