forked from leftypol/leftypol
ImageFormatReader: rework into ImageMetadataReader
This commit is contained in:
parent
7f6e84e593
commit
29684043ab
5 changed files with 58 additions and 47 deletions
|
@ -1,6 +1,9 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
use Vichan\Data\ImageMetadataResult;
|
||||
use Vichan\Data\MagickMetadataReader;
|
||||
|
||||
|
||||
/**
|
||||
* Do not use this if you can.
|
||||
|
@ -12,8 +15,8 @@ namespace Vichan\Service\Media;
|
|||
* If e.g. the Exif Orientation flag is set to a value which rotates the image by 90 or 270 degress, index 0 and 1
|
||||
* are swapped, i.e. the contain the height and width, respectively.
|
||||
*/
|
||||
class DefaultImageFormatReader implements ImageFormatReader {
|
||||
public function getSizes(string $file_path): array {
|
||||
class DefaultImageMetadataReader implements ImageMetadataReader {
|
||||
public function getMetadata(string $file_path): ImageMetadataResult {
|
||||
$ret = \getimagesize($file_path, $info);
|
||||
if ($ret === false) {
|
||||
throw new \RuntimeException("Could not read image sizes of '$file_path'");
|
||||
|
@ -21,6 +24,6 @@ class DefaultImageFormatReader implements ImageFormatReader {
|
|||
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
|
||||
throw new \RuntimeException("Error '$file_path' is not an image");
|
||||
}
|
||||
return $ret;
|
||||
return new ImageMetadataResult($ret[0], $ret[1], $ret['mime']);
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
|
||||
interface ImageFormatReader {
|
||||
/**
|
||||
* @param string $file_path Image file path.
|
||||
* @return array An array with width and height.
|
||||
*/
|
||||
public function getSizes(string $file_path): array;
|
||||
}
|
12
inc/Service/Media/ImageMetadataReader.php
Normal file
12
inc/Service/Media/ImageMetadataReader.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
use Vichan\Data\ImageMetadataResult;
|
||||
|
||||
|
||||
interface ImageMetadataReader {
|
||||
/**
|
||||
* @param string $file_path Image file path.
|
||||
*/
|
||||
public function getMetadata(string $file_path): ImageMetadataResult;
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
|
||||
class MagickImageFormatReader implements ImageFormatReader {
|
||||
private string $prefix;
|
||||
|
||||
public static function createImageMagickReader(): MagickImageFormatReader {
|
||||
return new self('');
|
||||
}
|
||||
|
||||
public static function createGraphicsMagickReader(): MagickImageFormatReader {
|
||||
return new self('gm ');
|
||||
}
|
||||
|
||||
private function __construct(string $prefix) {
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function getSizes(string $file_path): array {
|
||||
$arg = escapeshellarg("$file_path[0]");
|
||||
$ret_exec = shell_exec_error("{$this->prefix}identify -format \"%w %h\" $arg");
|
||||
|
||||
if (!\is_string($ret_exec)) {
|
||||
throw new \RuntimeException("Error while executing identify");
|
||||
}
|
||||
$ret_match = \preg_match('/^(\d+) (\d+)$/', $ret_exec, $m);
|
||||
if (!$ret_match) {
|
||||
throw new \RuntimeException("Could not parse identify output");
|
||||
}
|
||||
return [ $m[1], $m[2] ];
|
||||
}
|
||||
}
|
40
inc/Service/Media/MagickImageMetadataReader.php
Normal file
40
inc/Service/Media/MagickImageMetadataReader.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
use Mimey\MimeTypes;
|
||||
use Vichan\Data\ImageMetadataResult;
|
||||
|
||||
|
||||
class MagickImageMetadataReader implements ImageMetadataReader {
|
||||
private string $prefix;
|
||||
private MimeTypes $mime_types;
|
||||
|
||||
public static function createImageMagickReader(MimeTypes $mime_types): MagickImageMetadataReader {
|
||||
return new self('', $mime_types);
|
||||
}
|
||||
|
||||
public static function createGraphicsMagickReader(MimeTypes $mime_types): MagickImageMetadataReader {
|
||||
return new self('gm ', $mime_types);
|
||||
}
|
||||
|
||||
private function __construct(string $prefix, MimeTypes $mime_types) {
|
||||
$this->prefix = $prefix;
|
||||
$this->mime_types = $mime_types;
|
||||
}
|
||||
|
||||
public function getMetadata(string $file_path): ImageMetadataResult {
|
||||
$arg = escapeshellarg("$file_path[0]");
|
||||
$ret_exec = shell_exec_error("{$this->prefix}identify -format \"%w %h %m\" $arg");
|
||||
|
||||
if (!\is_string($ret_exec)) {
|
||||
throw new \RuntimeException("Error while executing identify");
|
||||
}
|
||||
$ret_match = \preg_match('/^(\d+) (\d+) ([\w\d]+)$/', $ret_exec, $m);
|
||||
if (!$ret_match) {
|
||||
throw new \RuntimeException("Could not parse identify output");
|
||||
}
|
||||
|
||||
$mime = $this->mime_types->getMimeType($m[3]) ?? 'application/octet-stream';
|
||||
return new ImageMetadataResult($m[1], $m[2], $mime);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue