Skip to content

Added encoded_content field for SVG images #89

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions includes/utils/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public static function get_imagedata( $image_id ) {
'html' => wp_get_attachment_image( $image_id, 'full' ),
);

// Check if svg.
$svg = self::encode_svg( $image_id );
if ( $svg ) {
$image_data['encoded_content'] = $svg;
}

// Add meta data.
$image_meta = wp_get_attachment_metadata( $image_id );
if ( is_array( $image_meta ) ) {
Expand Down Expand Up @@ -116,6 +122,25 @@ public static function get_imagedata( $image_id ) {
return $image_data;
}

/**
* Encode SVG file by id.
*
* @param int $image_id Attachement id.
* @return string|false
*/
private static function encode_svg( $image_id ) {
$file_path = get_attached_file( $image_id );
$file_type = wp_check_filetype( $file_path );
if ( $file_type['ext'] === 'svg' ) {
$svg_content = file_get_contents( $file_path );
if ( $svg_content ) {
return base64_encode( $svg_content );
}
}

return false;
}

/**
* Get video data by id.
*
Expand Down