From e39af5e463925a4291b35dd63ae020e92de9eed1 Mon Sep 17 00:00:00 2001 From: daomapsieucap Date: Mon, 10 Jan 2022 11:57:41 +0700 Subject: [PATCH] Add missing metadata for SVG images --- changelog.txt | 1 + includes/image.php | 23 +++++++++++++++++++++++ readme.txt | 3 ++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index c11489f..cae4d58 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,7 @@ *Release Date - xx January 2022* * Fixed: PHP 8.0 compatibility. +* Fixed: Add missing metadata for SVG images. = 1.5.12= *Release Date - 28 November 2021* diff --git a/includes/image.php b/includes/image.php index 15270ae..5414a8e 100644 --- a/includes/image.php +++ b/includes/image.php @@ -42,6 +42,9 @@ public function __construct(){ add_filter('wp_handle_upload', array($this, 'fiad_santialize_svg'), 10, 2); add_action("admin_enqueue_scripts", array($this, 'fiad_svg_enqueue_scripts')); + + // SVG metadata + add_filter('wp_update_attachment_metadata', 'fiad_svg_attachment_metadata', 10, 2); } } @@ -132,6 +135,26 @@ public function fiad_santialize_svg($upload, $context){ return $upload; } + + public function fiad_svg_attachment_metadata($data, $id){ + $attachment = get_post($id); + $mime_type = $attachment->post_mime_type; + + //If the attachment is an SVG + if($mime_type == 'image/svg+xml'){ + //If the svg metadata are empty or the width is empty or the height is empty + //then get the attributes from xml. + if(empty($data) || empty($data['width']) || empty($data['height'])){ + $xml = simplexml_load_file(wp_get_attachment_url($id)); + $attr = $xml->attributes(); + $viewbox = explode(' ', $attr->viewBox); + $data['width'] = isset($attr->width) && preg_match('/\d+/', $attr->width, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[2] : null); + $data['height'] = isset($attr->height) && preg_match('/\d+/', $attr->height, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[3] : null); + } + } + + return $data; + } } new Fiber_Admin_Image(); \ No newline at end of file diff --git a/readme.txt b/readme.txt index ddaca37..1e8b59c 100644 --- a/readme.txt +++ b/readme.txt @@ -49,4 +49,5 @@ At the first time using this setting, Fiber Admin will ask you to save it to cre = 1.5.13= *Release Date - xx January 2022* -* Fixed: PHP 8.0 compatibility. \ No newline at end of file +* Fixed: PHP 8.0 compatibility. +* Fixed: Add missing metadata for SVG images. \ No newline at end of file