Skip to content

Commit

Permalink
New setting for embed images
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Jun 27, 2024
1 parent a5e633a commit 0bc16a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
31 changes: 2 additions & 29 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,38 +864,11 @@ public function header_logo(): void {
$src = apply_filters( 'wpo_wcpdf_use_path', true ) ? $attachment_path : $attachment_src;

if ( ! wpo_wcpdf_is_file_readable( $src ) ) {
// convert path to URL
if ( apply_filters( 'wpo_wcpdf_use_path', true ) && false !== strpos( $src, WP_CONTENT_DIR ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_DIR ), trailingslashit( WP_CONTENT_URL ), $src );

// convert url to path
} elseif ( false !== strpos( $src, WP_CONTENT_URL ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_URL ), trailingslashit( WP_CONTENT_DIR ), $src );
}

// last check
if ( ! wpo_wcpdf_is_file_readable( $src ) ) {
wcpdf_log_error( 'Header logo file not readable: ' . $src, 'critical' );
return;
}
}

$mime_type = wpo_wcpdf_get_image_mime_type( $src );
$image_mime_types = apply_filters( 'wpo_wcpdf_header_logo_image_mime_types', array( 'image/png', 'image/jpeg', 'image/svg+xml', 'image/webp' ), $this );

if ( ! in_array( $mime_type, $image_mime_types, true ) ) {
wcpdf_log_error( 'Unsupported header logo mime type: ' . $mime_type, 'critical' );
return;
}

$image_base64 = wpo_wcpdf_base64_encode_image( $src );

if ( ! $image_base64 ) {
wcpdf_log_error( 'Unable to encode header logo to base64.', 'critical' );
wcpdf_log_error( 'Header logo file not readable: ' . $src, 'critical' );
return;
}

$image_src = 'data:' . $mime_type . ';base64,' . $image_base64;
$image_src = isset( WPO_WCPDF()->settings->debug_settings['embed_images'] ) ? wpo_wcpdf_get_image_src_in_base64( $src ) : $src;
$img_element = sprintf( '<img src="%1$s" alt="%2$s"/>', esc_attr( $image_src ), esc_attr( $company ) );

echo apply_filters( 'wpo_wcpdf_header_logo_img_element', $img_element, $attachment, $this );
Expand Down
12 changes: 12 additions & 0 deletions includes/settings/class-wcpdf-settings-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,18 @@ public function init_settings() {
__( 'You can also add <code>&output=html</code> to the URL to apply this on a per-order basis.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'embed_images',
'title' => __( 'Embed Images', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => 'debug_settings',
'args' => array(
'option_name' => $option_name,
'id' => 'embed_images',
'description' => __( 'Embed images, especially if PNG files are having issues loading in your PDF. Please note that this option may significantly increase the file size.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'log_to_order_notes',
Expand Down
28 changes: 28 additions & 0 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,31 @@ function wpo_wcpdf_is_file_readable( string $path ): bool {
}
}

/**
* Get image source in base64 format
*
* @param string $src
*
* @return string
*/
function wpo_wcpdf_get_image_src_in_base64( string $src ): string {
if ( empty( $src ) ) {
return $src;
}

$mime_type = wpo_wcpdf_get_image_mime_type( $src );

if ( empty( $mime_type ) ) {
wcpdf_log_error( 'Unable to determine image mime type for file: ' . $src, 'critical' );
return $src;
}

$image_base64 = wpo_wcpdf_base64_encode_image( $src );

if ( ! $image_base64 ) {
wcpdf_log_error( 'Unable to encode header logo to base64.', 'critical' );
return $src;
}

return 'data:' . $mime_type . ';base64,' . $image_base64;
}
4 changes: 2 additions & 2 deletions woocommerce-pdf-invoices-packingslips.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Requires Plugins: woocommerce
* Plugin URI: https://wpovernight.com/downloads/woocommerce-pdf-invoices-packing-slips-bundle/
* Description: Create, print & email PDF or UBL Invoices & PDF Packing Slips for WooCommerce orders.
* Version: 3.8.5-beta-4
* Version: 3.8.5-beta-5
* Author: WP Overnight
* Author URI: https://www.wpovernight.com
* License: GPLv2 or later
Expand All @@ -22,7 +22,7 @@

class WPO_WCPDF {

public $version = '3.8.5-beta-4';
public $version = '3.8.5-beta-5';
public $version_php = '7.2';
public $version_woo = '3.3';
public $version_wp = '4.4';
Expand Down

0 comments on commit 0bc16a9

Please sign in to comment.