From 41358f289088c60831591f9a904f491b7555fb5d Mon Sep 17 00:00:00 2001 From: Chengmin Date: Wed, 12 Feb 2025 03:30:59 -0500 Subject: [PATCH] added encoded_content field for SVG images --- includes/utils/class-utils.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/includes/utils/class-utils.php b/includes/utils/class-utils.php index b07bcbd..35fb1d3 100644 --- a/includes/utils/class-utils.php +++ b/includes/utils/class-utils.php @@ -7,7 +7,7 @@ namespace FuxtApi\Utils; -use \FuxtApi\Utils\Acf as AcfUtils; +use FuxtApi\Utils\Acf as AcfUtils; /** * Class Utils @@ -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 ) ) { @@ -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. * @@ -230,12 +255,11 @@ public static function cgi_parse_str( $str ) { /** * Convert camelCase to snake_case. * - * @param string $string. + * @param string $str. * * @return string */ - public static function decamelize( $string ) { - return strtolower( preg_replace( [ '/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/' ], '$1_$2', $string ) ); + public static function decamelize( $str ) { + return strtolower( preg_replace( array( '/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/' ), '$1_$2', $str ) ); } } -