forked from leftypol/leftypol
ImageFormatReader: add iamge format readers
This commit is contained in:
parent
557e43e38f
commit
1485ef3f2d
3 changed files with 62 additions and 0 deletions
18
inc/Service/Media/GdImageFormatReader.php
Normal file
18
inc/Service/Media/GdImageFormatReader.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
|
||||
class GdImageFormatReader implements ImageFormatReader {
|
||||
/**
|
||||
* getimagesize() is agnostic of any image metadata.
|
||||
* 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.
|
||||
*/
|
||||
public function getSizes(string $file_path): array {
|
||||
$ret = \getimagesize($file_path);
|
||||
if ($ret === false) {
|
||||
throw new \RuntimeException("Could not read image sizes of '$file_path'");
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
11
inc/Service/Media/ImageFormatReader.php
Normal file
11
inc/Service/Media/ImageFormatReader.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?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;
|
||||
}
|
33
inc/Service/Media/MagickImageFormatReader.php
Normal file
33
inc/Service/Media/MagickImageFormatReader.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?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] ];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue