Skip to content

Commit

Permalink
Added rotation option to image converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Jul 22, 2024
1 parent fe57226 commit 2308047
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/ImageToPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class ImageToPDF extends \FPDF {

protected $image_rotation = 0;

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

Expand All @@ -12,6 +14,10 @@ public function __construct( $orientation = 'P', $size = array( 0, 0 ) ) {
stream_wrapper_register( 'var', '\Vendidero\Germanized\Shipments\Utilities\VariableStreamHandler' );
}

public function set_rotation( $rotation ) {
$this->image_rotation = $rotation;
}

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

Expand All @@ -20,7 +26,7 @@ protected function convert_px_to_mm( $px, $dpi = 72 ) {

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
$image_meta = wp_getimagesize( $stream_or_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

if ( ! $image_meta ) {
$this->Error( 'Invalid image data' );
Expand All @@ -29,14 +35,14 @@ public function import_image( $stream_or_file, $x = 0, $y = 0, $dpi = 72 ) {
$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->AddPage( $this->DefOrientation, array( $width, $height ), $this->image_rotation ); // 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
$image_meta = wp_getimagesize( 'var://' . $image_stream ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

if ( ! $image_meta ) {
$this->Error( 'Invalid image data' );
Expand All @@ -45,7 +51,7 @@ public function import_image( $stream_or_file, $x = 0, $y = 0, $dpi = 72 ) {
$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->AddPage( $this->DefOrientation, array( $width, $height ), $this->image_rotation ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

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

0 comments on commit 2308047

Please sign in to comment.