Skip to content

Commit

Permalink
Improvements on SVG logo support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Jun 21, 2024
1 parent b3fd2db commit e591292
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 1 addition & 2 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
6 changes: 6 additions & 0 deletions includes/views/advanced-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit e591292

Please sign in to comment.