Moved graphics engine constants to their own class

This commit is contained in:
Matthew Barbour 2019-06-21 15:23:59 -05:00 committed by matthew
parent 3753a1b6d6
commit 7cc725fbc1
6 changed files with 79 additions and 79 deletions

View File

@ -21,6 +21,65 @@ abstract class GraphicsConfig
} }
abstract class GraphicsEngine {
public const GD = "gd";
public const IMAGICK = "convert";
public const FFMPEG = "ffmpeg";
public const ALL = [
GraphicsEngine::GD,
GraphicsEngine::FFMPEG,
GraphicsEngine::IMAGICK
];
public const OUTPUT_SUPPORT = [
GraphicsEngine::GD => [
"gif",
"jpg",
"png",
"webp",
self::WEBP_LOSSY,
],
GraphicsEngine::IMAGICK => [
"gif",
"jpg",
"png",
"webp",
self::WEBP_LOSSY,
self::WEBP_LOSSLESS,
],
GraphicsEngine::FFMPEG => [
]
];
public const INPUT_SUPPORT = [
GraphicsEngine::GD => [
"bmp",
"gif",
"jpg",
"png",
"webp",
],
GraphicsEngine::IMAGICK => [
"bmp",
"gif",
"jpg",
"png",
"psd",
"tiff",
"webp",
"ico",
],
GraphicsEngine::FFMPEG => [
"avi",
"mkv",
"webm",
"mp4",
"mov",
"flv"
]
];
}
class GraphicsException extends SCoreException class GraphicsException extends SCoreException
{ {
} }
@ -47,7 +106,7 @@ class GraphicResizeEvent extends Event
bool $minimize = false, bool $minimize = false,
bool $allow_upscale = true) bool $allow_upscale = true)
{ {
assert(in_array($engine, Graphics::GRAPHICS_ENGINES)); assert(in_array($engine, GraphicsEngine::ALL));
$this->engine = $engine; $this->engine = $engine;
$this->input_path = $input_path; $this->input_path = $input_path;
$this->input_type = $input_type; $this->input_type = $input_type;
@ -67,68 +126,9 @@ class Graphics extends Extension
const WEBP_LOSSY = "webp-lossy"; const WEBP_LOSSY = "webp-lossy";
const WEBP_LOSSLESS = "webp-lossless"; const WEBP_LOSSLESS = "webp-lossless";
const FFMPEG_ENGINE = "ffmpeg";
const GD_ENGINE = "gd";
const IMAGICK_ENGINE = "convert";
const GRAPHICS_ENGINES = [
self::GD_ENGINE,
self::FFMPEG_ENGINE,
self::IMAGICK_ENGINE
];
const IMAGE_GRAPHICS_ENGINES = [ const IMAGE_GRAPHICS_ENGINES = [
"GD" => self::GD_ENGINE, "GD" => GraphicsEngine::GD,
"ImageMagick" => self::IMAGICK_ENGINE, "ImageMagick" => GraphicsEngine::IMAGICK,
];
const ENGINE_INPUT_SUPPORT = [
self::GD_ENGINE => [
"bmp",
"gif",
"jpg",
"png",
"webp",
],
self::IMAGICK_ENGINE => [
"bmp",
"gif",
"jpg",
"png",
"psd",
"tiff",
"webp",
"ico",
],
self::FFMPEG_ENGINE => [
"avi",
"mkv",
"webm",
"mp4",
"mov",
"flv"
]
];
const ENGINE_OUTPUT_SUPPORT = [
self::GD_ENGINE => [
"gif",
"jpg",
"png",
"webp",
self::WEBP_LOSSY,
],
self::IMAGICK_ENGINE => [
"gif",
"jpg",
"png",
"webp",
self::WEBP_LOSSY,
self::WEBP_LOSSLESS,
],
self::FFMPEG_ENGINE => [
]
]; ];
const LOSSLESS_FORMATS = [ const LOSSLESS_FORMATS = [
@ -203,7 +203,7 @@ class Graphics extends Extension
public function onSetupBuilding(SetupBuildingEvent $event) public function onSetupBuilding(SetupBuildingEvent $event)
{ {
$sb = new SetupBlock("Graphics"); $sb = new SetupBlock("Graphic Engines");
// if (self::imagick_available()) { // if (self::imagick_available()) {
// try { // try {
@ -233,7 +233,7 @@ class Graphics extends Extension
public function onGraphicResize(GraphicResizeEvent $event) public function onGraphicResize(GraphicResizeEvent $event)
{ {
switch ($event->engine) { switch ($event->engine) {
case self::GD_ENGINE: case GraphicsEngine::GD:
$info = getimagesize($event->input_path); $info = getimagesize($event->input_path);
if ($info === false) { if ($info === false) {
throw new GraphicsException("getimagesize failed for " . $event->input_path); throw new GraphicsException("getimagesize failed for " . $event->input_path);
@ -251,7 +251,7 @@ class Graphics extends Extension
$event->allow_upscale); $event->allow_upscale);
break; break;
case self::IMAGICK_ENGINE: case GraphicsEngine::IMAGICK:
// if (self::imagick_available()) { // if (self::imagick_available()) {
// } else { // } else {
self::image_resize_convert( self::image_resize_convert(
@ -292,7 +292,7 @@ class Graphics extends Extension
* @param array $info The output of getimagesize() for the source file in question. * @param array $info The output of getimagesize() for the source file in question.
* @return int The number of bytes an image resize operation is estimated to use. * @return int The number of bytes an image resize operation is estimated to use.
*/ */
static function calc_memory_use(array $info): int public static function calc_memory_use(array $info): int
{ {
if (isset($info['bits']) && isset($info['channels'])) { if (isset($info['bits']) && isset($info['channels'])) {
$memory_use = ($info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5) / 1024; $memory_use = ($info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5) / 1024;
@ -312,7 +312,7 @@ class Graphics extends Extension
* @return bool true if successful, false if not. * @return bool true if successful, false if not.
* @throws GraphicsException * @throws GraphicsException
*/ */
static function create_thumbnail_ffmpeg($hash): bool public static function create_thumbnail_ffmpeg($hash): bool
{ {
global $config; global $config;
@ -500,7 +500,7 @@ class Graphics extends Extension
$convert = $config->get_string(GraphicsConfig::CONVERT_PATH); $convert = $config->get_string(GraphicsConfig::CONVERT_PATH);
if ($convert == null || $convert == "") { if (empty($convert)) {
throw new GraphicsException("convert command not configured"); throw new GraphicsException("convert command not configured");
} }
@ -735,7 +735,7 @@ class Graphics extends Extension
public static function is_input_supported($engine, $format): bool public static function is_input_supported($engine, $format): bool
{ {
$format = self::normalize_format($format); $format = self::normalize_format($format);
if (!in_array($format, Graphics::ENGINE_INPUT_SUPPORT[$engine])) { if (!in_array($format, GraphicsEngine::INPUT_SUPPORT[$engine])) {
return false; return false;
} }
return true; return true;
@ -744,7 +744,7 @@ class Graphics extends Extension
public static function is_output_supported($engine, $format): bool public static function is_output_supported($engine, $format): bool
{ {
$format = self::normalize_format($format); $format = self::normalize_format($format);
if (!in_array($format, Graphics::ENGINE_OUTPUT_SUPPORT[$engine])) { if (!in_array($format, GraphicsEngine::OUTPUT_SUPPORT[$engine])) {
return false; return false;
} }
return true; return true;

View File

@ -57,7 +57,7 @@ class IcoFileHandler extends DataHandlerExtension
protected function create_thumb(string $hash, string $type): bool protected function create_thumb(string $hash, string $type): bool
{ {
try { try {
create_image_thumb($hash, $type, Graphics::IMAGICK_ENGINE); create_image_thumb($hash, $type, GraphicsEngine::IMAGICK);
return true; return true;
} catch (GraphicsException $e) { } catch (GraphicsException $e) {
log_warning("handle_ico", "Could not generate thumbnail. " . $e->getMessage()); log_warning("handle_ico", "Could not generate thumbnail. " . $e->getMessage());

View File

@ -36,7 +36,7 @@ class SVGFileHandler extends DataHandlerExtension
protected function create_thumb(string $hash, string $type): bool protected function create_thumb(string $hash, string $type): bool
{ {
try { try {
create_image_thumb($hash, $type, Graphics::IMAGICK_ENGINE); create_image_thumb($hash, $type, GraphicsEngine::IMAGICK);
return true; return true;
} catch (GraphicsException $e) { } catch (GraphicsException $e) {
log_warning("handle_svg", "Could not generate thumbnail. " . $e->getMessage()); log_warning("handle_svg", "Could not generate thumbnail. " . $e->getMessage());

View File

@ -41,8 +41,8 @@ class ImageIO extends Extension
const THUMBNAIL_ENGINES = [ const THUMBNAIL_ENGINES = [
'Built-in GD' => Graphics::GD_ENGINE, 'Built-in GD' => GraphicsEngine::GD,
'ImageMagick' => Graphics::IMAGICK_ENGINE 'ImageMagick' => GraphicsEngine::IMAGICK
]; ];
const THUMBNAIL_TYPES = [ const THUMBNAIL_TYPES = [

View File

@ -40,7 +40,7 @@ class ResizeImage extends Extension
global $config; global $config;
$config->set_default_bool(ResizeConfig::ENABLED, true); $config->set_default_bool(ResizeConfig::ENABLED, true);
$config->set_default_bool(ResizeConfig::UPLOAD, false); $config->set_default_bool(ResizeConfig::UPLOAD, false);
$config->set_default_string(ResizeConfig::ENGINE, Graphics::GD_ENGINE); $config->set_default_string(ResizeConfig::ENGINE, GraphicsEngine::GD);
$config->set_default_int(ResizeConfig::DEFAULT_WIDTH, 0); $config->set_default_int(ResizeConfig::DEFAULT_WIDTH, 0);
$config->set_default_int(ResizeConfig::DEFAULT_HEIGHT, 0); $config->set_default_int(ResizeConfig::DEFAULT_HEIGHT, 0);
} }
@ -206,7 +206,7 @@ class ResizeImage extends Extension
} }
send_event(new GraphicResizeEvent( send_event(new GraphicResizeEvent(
Graphics::GD_ENGINE, GraphicsEngine::GD,
$image_filename, $image_filename,
$image_obj->ext, $image_obj->ext,
$tmp_filename, $tmp_filename,

View File

@ -66,7 +66,7 @@ class TranscodeImage extends Extension
global $config; global $config;
$config->set_default_bool(TranscodeConfig::ENABLED, true); $config->set_default_bool(TranscodeConfig::ENABLED, true);
$config->set_default_bool(TranscodeConfig::UPLOAD, false); $config->set_default_bool(TranscodeConfig::UPLOAD, false);
$config->set_default_string(TranscodeConfig::ENGINE, Graphics::GD_ENGINE); $config->set_default_string(TranscodeConfig::ENGINE, GraphicsEngine::GD);
$config->set_default_int(TranscodeConfig::QUALITY, 80); $config->set_default_int(TranscodeConfig::QUALITY, 80);
foreach (array_values(self::INPUT_FORMATS) as $format) { foreach (array_values(self::INPUT_FORMATS) as $format) {
@ -100,7 +100,7 @@ class TranscodeImage extends Extension
$sb->add_bool_option(TranscodeConfig::UPLOAD, "Transcode on upload: ", true); $sb->add_bool_option(TranscodeConfig::UPLOAD, "Transcode on upload: ", true);
$sb->add_choice_option(TranscodeConfig::ENGINE, Graphics::IMAGE_GRAPHICS_ENGINES, "Engine", true); $sb->add_choice_option(TranscodeConfig::ENGINE, Graphics::IMAGE_GRAPHICS_ENGINES, "Engine", true);
foreach (self::INPUT_FORMATS as $display=>$format) { foreach (self::INPUT_FORMATS as $display=>$format) {
if (in_array($format, Graphics::ENGINE_INPUT_SUPPORT[$engine])) { if (in_array($format, GraphicsEngine::INPUT_SUPPORT[$engine])) {
$outputs = $this->get_supported_output_formats($engine, $format); $outputs = $this->get_supported_output_formats($engine, $format);
$sb->add_choice_option(TranscodeConfig::UPLOAD_PREFIX.$format, $outputs, "$display", true); $sb->add_choice_option(TranscodeConfig::UPLOAD_PREFIX.$format, $outputs, "$display", true);
} }
@ -289,7 +289,7 @@ class TranscodeImage extends Extension
if (!$this->can_convert_format($engine, $source_format)) { if (!$this->can_convert_format($engine, $source_format)) {
throw new ImageTranscodeException("Engine $engine does not support input format $source_format"); throw new ImageTranscodeException("Engine $engine does not support input format $source_format");
} }
if (!in_array($target_format, Graphics::ENGINE_OUTPUT_SUPPORT[$engine])) { if (!in_array($target_format, GraphicsEngine::OUTPUT_SUPPORT[$engine])) {
throw new ImageTranscodeException("Engine $engine does not support output format $target_format"); throw new ImageTranscodeException("Engine $engine does not support output format $target_format");
} }