-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added helpers to convert images to pdf on the fly.
- Loading branch information
1 parent
356059a
commit f833ae7
Showing
6 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |