Skip to content

Commit

Permalink
Add missing metadata for SVG images
Browse files Browse the repository at this point in the history
  • Loading branch information
daomapsieucap committed Jan 10, 2022
1 parent 0226c6f commit e39af5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
23 changes: 23 additions & 0 deletions includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* Fixed: PHP 8.0 compatibility.
* Fixed: Add missing metadata for SVG images.

0 comments on commit e39af5e

Please sign in to comment.