Skip to content

Commit

Permalink
Added helpers to convert images to pdf on the fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Jul 11, 2024
1 parent 356059a commit f833ae7
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 8 deletions.
4 changes: 2 additions & 2 deletions includes/admin/views/label/html-shipment-label.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
}

$actions['delete'] = array(
'url' => $label->get_download_url(),
'url' => '#',
'classes' => 'remove-shipment-label delete',
'name' => _x( 'Delete label', 'shipments', 'woocommerce-germanized-shipments' ),
'action' => 'delete_label',
'target' => '_blank',
'target' => 'self',
'custom_attributes' => array(
'data-shipment' => $shipment->get_id(),
),
Expand Down
1 change: 0 additions & 1 deletion includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,6 @@ function wc_gzd_customer_return_needs_manual_confirmation( $order ) {
* @return array
*/
function wc_gzd_get_account_shipments_actions( $shipment ) {

if ( ! is_object( $shipment ) ) {
$shipment_id = absint( $shipment );
$shipment = wc_gzd_get_shipment( $shipment_id );
Expand Down
4 changes: 2 additions & 2 deletions src/Compatibility/Bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public static function apply_bundle_hierarchy( $shipment ) {
* @return Product
*/
public static function get_product_from_item( $product, $item ) {
if ( ! $order = $item->get_order() || ! $product ) {
if ( ( ! $order = $item->get_order() ) || ! $product ) {
return $product;
}

$reset_shipping_props = false;

if ( wc_pb_is_bundle_container_order_item( $item ) ) {
if ( wc_pb_is_bundle_container_order_item( $item, $order ) ) {
if ( $product->needs_shipping() ) {
if ( $bundle_weight = $item->get_meta( '_bundle_weight', true ) ) {
if ( is_null( $bundle_weight ) ) {
Expand Down
56 changes: 56 additions & 0 deletions src/ImageToPDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Vendidero\Germanized\Shipments;

class ImageToPDF extends \FPDF {

public function __construct( $orientation = 'P', $size = array( 0, 0 ) ) {
parent::__construct( $orientation, 'mm', $size );

$this->SetMargins( 0, 0 );

stream_wrapper_register( 'var', '\Vendidero\Germanized\Shipments\Utilities\VariableStreamHandler' );
}

protected function convert_px_to_mm( $px, $dpi = 72 ) {
$pixel = 25.4 / $dpi;

return $pixel * $px;
}

public function import_image( $stream_or_file, $x = 0, $y = 0, $dpi = 72 ) {
if ( is_file( $stream_or_file ) ) {
$image_meta = @getimagesize( $stream_or_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

if ( ! $image_meta ) {
$this->Error( 'Invalid image data' );
}

$width = $this->convert_px_to_mm( $image_meta[0], $dpi );
$height = $this->convert_px_to_mm( $image_meta[1], $dpi );

$this->AddPage( $this->DefOrientation, array( $width, $height ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

$this->Image( $stream_or_file, $x, $y, $width, $height );
} else {
// Display the image contained in $data
$image_stream = 'img' . md5( $stream_or_file );
$GLOBALS[ $image_stream ] = $stream_or_file;
$image_meta = @getimagesize( 'var://' . $image_stream ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

if ( ! $image_meta ) {
$this->Error( 'Invalid image data' );
}

$width = $this->convert_px_to_mm( $image_meta[0], $dpi );
$height = $this->convert_px_to_mm( $image_meta[1], $dpi );

$this->AddPage( $this->DefOrientation, array( $width, $height ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

$type = substr( strstr( $image_meta['mime'], '/' ), 1 );
$this->Image( 'var://' . $image_stream, 0, 0, $width, $height, $type );

unset( $GLOBALS[ $image_stream ] );
}
}
}
40 changes: 37 additions & 3 deletions src/ShippingProvider/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ public function get_supported_label_reference_types( $shipment_type = 'simple' )
return array();
}

/**
* @param $ref_type
* @param $shipment_type
*
* @return array|false
*/
public function get_supported_label_reference_type( $ref_type, $shipment_type = 'simple' ) {
$ref_types = $this->get_supported_label_reference_types( $shipment_type );
$type = false;

if ( array_key_exists( $ref_type, $ref_types ) ) {
$type = wp_parse_args(
$ref_types[ $ref_type ],
array(
'max_length' => -1,
'label' => '',
'default' => '',
)
);
}

return $type;
}

/**
* @param string $shipment_type
* @param Label|null $label
Expand Down Expand Up @@ -183,15 +207,25 @@ public function get_label_reference( $shipment_type = 'simple', $reference_type
* @return string
*/
public function get_formatted_label_reference( $label, $shipment_type = 'simple', $reference_type = '' ) {
$reference = $this->get_label_reference( $shipment_type, $reference_type );
$reference = $this->get_label_reference( $shipment_type, $reference_type );
$max_length = -1;

if ( $ref_type = $this->get_supported_label_reference_type( $reference_type, $shipment_type ) ) {
$max_length = $ref_type['max_length'];
}

if ( ! empty( $reference ) ) {
$placeholders = $this->get_label_reference_placeholders( $shipment_type, $label );
$reference = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $reference );
}

$formatted_ref = apply_filters( "{$this->get_hook_prefix()}formatted_label_reference", $reference, $label, $shipment_type, $reference_type );

$reference = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $reference );
if ( -1 !== $max_length ) {
$formatted_ref = wc_gzd_shipments_substring( $formatted_ref, 0, $max_length );
}

return apply_filters( "{$this->get_hook_prefix()}formatted_label_reference", $reference, $label, $shipment_type, $reference_type );
return $formatted_ref;
}

public function get_label_return_auto_enable( $context = 'view' ) {
Expand Down
46 changes: 46 additions & 0 deletions src/Utilities/VariableStreamHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Vendidero\Germanized\Shipments\Utilities;

class VariableStreamHandler {

public $context;
private $varname;
private $position;

public function stream_open( $path, $mode, $options, &$opened_path ) {
$url = wp_parse_url( $path );
$this->varname = $url['host'];
if ( ! isset( $GLOBALS[ $this->varname ] ) ) {
return false;
}
$this->position = 0;
return true;
}

public function stream_read( $count ) {
$ret = substr( $GLOBALS[ $this->varname ], $this->position, $count );
$this->position += strlen( $ret );
return $ret;
}

public function stream_eof() {
return $this->position >= strlen( $GLOBALS[ $this->varname ] );
}

public function stream_tell() {
return $this->position;
}

public function stream_seek( $offset, $whence ) {
if ( SEEK_SET === $whence ) {
$this->position = $offset;
return true;
}
return false;
}

public function stream_stat() {
return array();
}
}

0 comments on commit f833ae7

Please sign in to comment.