-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- several PHP warnins and error fixed - introduct two new classes to support image to pdf including tiff images
- Loading branch information
Showing
3 changed files
with
186 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* PdfCreator class | ||
* | ||
* @package OpenEMR | ||
* @link http://www.open-emr.org | ||
* @author Jerry Padgett <sjpadgett@gmail.com> | ||
* @copyright Copyright (c) 2024 Jerry Padgett <sjpadgett@gmail.com> | ||
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 | ||
*/ | ||
|
||
namespace OpenEMR\Services\ImageUtilities; | ||
|
||
use Exception; | ||
use OpenEMR\Services\ImageUtilities\HandleImageService; | ||
|
||
class HandleImageController | ||
{ | ||
private HandleImageService $imageService; | ||
private $extension; | ||
|
||
public function __construct($extension = 'gd') | ||
{ | ||
$this->imageService = new HandleImageService(); | ||
$this->extension = $extension; | ||
} | ||
|
||
public function isImagickAvailable(): bool | ||
{ | ||
return extension_loaded('imagick'); | ||
} | ||
|
||
public function isGdAvailable(): bool | ||
{ | ||
return extension_loaded('gd'); | ||
} | ||
|
||
public function convertImageToPdf($imageData, $pdfPath = '', $useExt = 'imagick'): false|string | ||
{ | ||
$content = ''; | ||
if (is_file($imageData)) { | ||
$imageContent = file_get_contents($imageData); | ||
} else { | ||
$imageContent = $imageData; | ||
} | ||
|
||
$usingImagick = $useExt === 'imagick' && $this->isImagickAvailable(); | ||
$usingGd = $useExt === 'gd' && $this->isGdAvailable() && !$usingImagick; | ||
|
||
if ($usingImagick || $usingGd) { | ||
try { | ||
if ($usingImagick) { | ||
$content = $this->imageService->convertImageToPdfUseImagick($imageContent, $pdfPath); | ||
} elseif ($usingGd) { | ||
$content = $this->imageService->convertImageToPdfUseGD($imageContent, $pdfPath); | ||
} | ||
} catch (Exception $e) { | ||
error_log('Error converting image to PDF: ' . $e->getMessage()); | ||
return false; | ||
} | ||
} else { | ||
error_log('No suitable image processing library available.'); | ||
return false; | ||
} | ||
|
||
return $content; | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace OpenEMR\Services\ImageUtilities; | ||
|
||
use Exception; | ||
use Imagick; | ||
use ImagickException; | ||
use OpenEMR\Pdf\MpdfGenericPdfCreator; | ||
|
||
class HandleImageService | ||
{ | ||
public function convertImageToPdfUseGD($imageData, $pdfPath): false|string | ||
{ | ||
try { | ||
$imageRaw = base64_decode($imageData); // Decode base64 image data (if needed) | ||
$image = imagecreatefromstring($imageRaw); // Load image using GD | ||
if ($image === false) { | ||
throw new Exception('Failed to create image from data'); | ||
} | ||
ob_start(); | ||
imagepng($image); | ||
$imagePngData = ob_get_clean(); | ||
imagedestroy($image); | ||
|
||
$pdf = new MpdfGenericPdfCreator(); | ||
$pdf->addImageToPDF($imagePngData); // Add image to PDF | ||
|
||
return $pdf->outputPDF($pdfPath, 'S'); // Output PDF as a string | ||
} catch (Exception $e) { | ||
// Handle exceptions | ||
error_log('Error: ' . $e->getMessage()); | ||
return false; | ||
} finally { | ||
// Clean up GD resources | ||
if (is_resource($image)) { | ||
imagedestroy($image); | ||
} | ||
} | ||
} | ||
|
||
public function convertImageToPdfUseImagick($imageContent, $pdfOutPath = ''): false|string | ||
{ | ||
try { | ||
$imagick = new Imagick(); | ||
$imageRaw = base64_decode($imageContent); // Decode base64 image data (if needed) | ||
$imagick->readImageBlob($imageRaw); // Load image using Imagick from binary data | ||
$imagick->setFirstIterator(); // Set iterator to first page | ||
$imagick->setImageFormat('pdf'); | ||
$pdfContent = $imagick->getImagesBlob(); | ||
} catch (ImagickException $e) { | ||
// Handle Imagick-related exceptions | ||
error_log('Imagick error: ' . $e->getMessage()); | ||
return false; | ||
} catch (Exception $e) { | ||
// Handle other exceptions | ||
error_log('Error: ' . $e->getMessage()); | ||
return false; | ||
} finally { | ||
// Clean up Imagick resources | ||
if (isset($imagick)) { | ||
$imagick->clear(); | ||
$imagick->destroy(); | ||
} | ||
} | ||
|
||
if ($pdfOutPath) { | ||
// Write the PDF content to a file if $pdfOutPath is provided | ||
file_put_contents($pdfOutPath, $pdfContent); | ||
return true; // Return true if PDF is successfully written to file | ||
} | ||
// Return the PDF content as a string if $pdfOutPath is empty | ||
return $pdfContent; | ||
} | ||
} |