forked from leftypol/leftypol
GdMediaHandler.php: bring in orientation detection, reducing abstraction by 1 layer
This commit is contained in:
parent
7145742d8a
commit
8b6e2f5434
1 changed files with 16 additions and 11 deletions
|
@ -13,7 +13,7 @@ class GdMediaHandler implements MediaHandler {
|
||||||
|
|
||||||
|
|
||||||
private bool $strip_redraw;
|
private bool $strip_redraw;
|
||||||
private ImageMetadataReader $image_metadata_reader;
|
private array $exif_readers;
|
||||||
|
|
||||||
|
|
||||||
private static function imageCreateFrom(string $file, string $mime): mixed {
|
private static function imageCreateFrom(string $file, string $mime): mixed {
|
||||||
|
@ -74,6 +74,14 @@ class GdMediaHandler implements MediaHandler {
|
||||||
return $gd;
|
return $gd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getOrientation(string $file_path, string $file_mime): ?int {
|
||||||
|
$exif_reader = $this->exif_readers[$file_mime] ?? null;
|
||||||
|
if ($exif_reader !== null) {
|
||||||
|
return $exif_reader->getOrientation($file_path);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private function generateThumbImpl(
|
private function generateThumbImpl(
|
||||||
mixed $gd,
|
mixed $gd,
|
||||||
string $source_file_path,
|
string $source_file_path,
|
||||||
|
@ -134,10 +142,11 @@ class GdMediaHandler implements MediaHandler {
|
||||||
/**
|
/**
|
||||||
* @param bool $strip_redraw If the EXIF metadata should be stripped by redrawing it.
|
* @param bool $strip_redraw If the EXIF metadata should be stripped by redrawing it.
|
||||||
* May cause the loss of color profiles. Orientation is still handled in JPG and PNG.
|
* May cause the loss of color profiles. Orientation is still handled in JPG and PNG.
|
||||||
|
* @param array $exif_readers A map of mime types to {@link Vichan\Data\Driver\Metadata\ExifReader} instances.
|
||||||
*/
|
*/
|
||||||
public function __construct(bool $strip_redraw, ImageMetadataReader $image_metadata_reader) {
|
public function __construct(bool $strip_redraw, array $exif_readers) {
|
||||||
$this->strip_redraw = $strip_redraw;
|
$this->strip_redraw = $strip_redraw;
|
||||||
$this->image_metadata_reader = $image_metadata_reader;
|
$this->exif_readers = $exif_readers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function supportsMime(string $mime): bool {
|
public function supportsMime(string $mime): bool {
|
||||||
|
@ -151,20 +160,16 @@ class GdMediaHandler implements MediaHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
|
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
|
||||||
$metadata = $this->image_metadata_reader->getMetadata($file_path);
|
|
||||||
if ($file_mime !== $file_mime) {
|
|
||||||
throw new \RuntimeException("Mime mismatch on '$file_path'");
|
|
||||||
}
|
|
||||||
|
|
||||||
$gd = self::imageCreateFrom($file_path, $file_mime);
|
$gd = self::imageCreateFrom($file_path, $file_mime);
|
||||||
if ($gd === false) {
|
if ($gd === false) {
|
||||||
throw new \RuntimeException("Could not open '$file_path'");
|
throw new \RuntimeException("Could not open '$file_path'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix the orientation once and for all.
|
// Fix the orientation once and for all.
|
||||||
if ($metadata->exif_orientation !== null) {
|
$exif_orientation = $this->getOrientation($file_path, $file_mime);
|
||||||
$degrees = Exif::exifOrientationDegrees($metadata->exif_orientation);
|
if ($exif_orientation !== null) {
|
||||||
$flipped = Exif::exifOrientationIsFlipped($metadata->exif_orientation);
|
$degrees = Exif::exifOrientationDegrees($exif_orientation);
|
||||||
|
$flipped = Exif::exifOrientationIsFlipped($exif_orientation);
|
||||||
|
|
||||||
if ($degrees !== 0) {
|
if ($degrees !== 0) {
|
||||||
self::enableTransparency($gd, $file_mime);
|
self::enableTransparency($gd, $file_mime);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue