From e591292eeb71ebb87f454bbc30599256048aee17 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 21 Jun 2024 12:37:20 +0100 Subject: [PATCH] Improvements on SVG logo support --- .../abstract-wcpdf-order-document.php | 3 +- includes/views/advanced-status.php | 6 +++ includes/wcpdf-functions.php | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index ff2e9e75a..e31b1e76e 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -901,8 +901,7 @@ public function header_logo(): void { } } - $image_info = getimagesize( $src ); - $mime_type = $image_info['mime'] ?? ''; + $mime_type = wpo_wcpdf_get_image_mime_type( $src ); $image_mime_types = apply_filters( 'wpo_wcpdf_image_mime_types', array( 'image/png', 'image/jpeg', 'image/svg+xml' ), $this ); if ( ! in_array( $mime_type, $image_mime_types, true ) ) { diff --git a/includes/views/advanced-status.php b/includes/views/advanced-status.php index 2f3e2e9d3..e1bd9545c 100644 --- a/includes/views/advanced-status.php +++ b/includes/views/advanced-status.php @@ -77,6 +77,12 @@ 'result' => ini_get( 'allow_url_fopen' ), 'fallback' => __( 'allow_url_fopen disabled', 'woocommerce-pdf-invoices-packing-slips' ), ), + 'fileinfo' => array ( + 'required' => __( 'Necessary to verify the MIME type of local images.', 'woocommerce-pdf-invoices-packing-slips' ), + 'value' => null, + 'result' => extension_loaded( 'fileinfo' ), + 'fallback' => __( 'fileinfo disabled', 'woocommerce-pdf-invoices-packing-slips' ), + ), 'base64_decode' => array ( 'required' => __( 'To compress and decompress font and image data', 'woocommerce-pdf-invoices-packing-slips' ), 'value' => null, diff --git a/includes/wcpdf-functions.php b/includes/wcpdf-functions.php index 16736f00f..2195ac543 100644 --- a/includes/wcpdf-functions.php +++ b/includes/wcpdf-functions.php @@ -721,6 +721,47 @@ function wpo_wcpdf_get_multilingual_languages(): array { return apply_filters( 'wpo_wcpdf_multilingual_languages', $languages ); } +/** + * Get image mime type + * + * @param string $src + * @return string + */ +function wpo_wcpdf_get_image_mime_type( string $src ): string { + $mime_type = ''; + + // Check if 'getimagesize' function exists and try to get mime type for local files + if ( function_exists( 'getimagesize' ) && file_exists( $src ) ) { + $image_info = @getimagesize( $src ); + $mime_type = $image_info['mime'] ?? ''; + } + + // Fallback to 'finfo_file' if mime type is empty for local files + if ( empty( $mime_type ) && function_exists( 'finfo_open' ) && file_exists( $src ) ) { + $finfo = finfo_open( FILEINFO_MIME_TYPE ); + if ( $finfo ) { + $mime_type = finfo_file( $finfo, $src ); + finfo_close( $finfo ); + } else { + wcpdf_log_error( 'Fileinfo failed to open.' ); + } + } + + // Handle remote files + if ( empty( $mime_type ) && filter_var( $src, FILTER_VALIDATE_URL ) ) { + $headers = get_headers( $src, 1 ); + if ( $headers && isset( $headers['Content-Type'] ) ) { + $mime_type = is_array( $headers['Content-Type'] ) ? $headers['Content-Type'][0] : $headers['Content-Type']; + } + } + + if ( empty( $mime_type ) ) { + wcpdf_log_error( 'Unable to determine MIME type for file: ' . $src ); + } + + return $mime_type ?? ''; +} + /** * Base64 encode image from URL or local path *