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
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