leftypol/inc/Service/Media/MagickImageFormatReader.php

34 lines
887 B
PHP
Raw Normal View History

<?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] ];
}
}