diff --git a/lib/Internal/CommonUtility.php b/lib/Internal/CommonUtility.php index 4ff05243..846f3874 100644 --- a/lib/Internal/CommonUtility.php +++ b/lib/Internal/CommonUtility.php @@ -118,7 +118,7 @@ public static function convertAreasToStringFormattedAreas($areas): array return $stringFormattedAreas; } - public static function convertImageResourceToBase64($imageResource): ?string + public static function convertImageResourceToBinared($imageResource): ?string { if ($imageResource === null) { return null; @@ -132,13 +132,16 @@ public static function convertImageResourceToBase64($imageResource): ?string case 'gd': ob_start(); imagepng($imageResource); - $imageData = ob_get_clean(); // same as ob_get_contents() + ob_end_clean() - return base64_encode($imageData); + $imageData = ob_get_clean(); + return $imageData; case 'file': $imageData = stream_get_contents($imageResource); fclose($imageResource); - return base64_encode($imageData); + if ($imageData === false) { + throw new BarcodeException("Failed to read from file resource"); + } + return $imageData; default: throw new BarcodeException("Unsupported resource type: $type"); @@ -154,18 +157,19 @@ public static function convertImageResourceToBase64($imageResource): ?string if ($fileContent === false) { throw new BarcodeException("Failed to read file content: $imageResource"); } - return base64_encode($fileContent); + return $fileContent; } - - // If string is already valid base64 (safe check) - if (base64_encode(base64_decode($imageResource, true)) === $imageResource) { - return $imageResource; + // If string is base64 + $decoded = base64_decode($imageResource, true); + if ($decoded !== false && base64_encode($decoded) === $imageResource) { + return $decoded; } - throw new BarcodeException("File does not exist or is not readable: $imageResource"); + + throw new BarcodeException("File does not exist, is unreadable, or invalid base64 string: $imageResource"); } - // Unsupported input + // Unsupported input type throw new BarcodeException("Unsupported input type. Expected GD resource, file resource, base64 string, or valid file path."); } } \ No newline at end of file diff --git a/lib/Recognition/BarCodeReader.php b/lib/Recognition/BarCodeReader.php index 7ce74a50..b24f698e 100644 --- a/lib/Recognition/BarCodeReader.php +++ b/lib/Recognition/BarCodeReader.php @@ -39,7 +39,6 @@ private function getBarCodeReaderDto(): BarcodeReaderDTO private function setBarCodeReaderDto(BarcodeReaderDTO $barCodeReaderDto): void { $this->barCodeReaderDto = $barCodeReaderDto; -// $this->initFieldsFromDto(); } private QualitySettings $qualitySettings; @@ -86,7 +85,7 @@ public function initFieldsFromDto() */ private function initializeImageRelatedFields(): void { - $this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBase64($this->imageResource); + $this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBinared($this->imageResource); $this->barCodeReaderDto->areas = CommonUtility::convertAreasToStringFormattedAreas($this->areas); } @@ -370,7 +369,7 @@ public final function setBarCodeImage($imageResource, ?Rectangle ...$areas): voi { try { - $this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBase64($imageResource); + $this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBinared($imageResource); $this->barCodeReaderDto->areas = CommonUtility::convertAreasToStringFormattedAreas($areas); if (is_null($this->barCodeReaderDto->barCodeDecodeTypes) || sizeof($this->barCodeReaderDto->barCodeDecodeTypes) == 0) $this->barCodeReaderDto->barCodeDecodeTypes = array(DecodeType::ALL_SUPPORTED_TYPES); diff --git a/lib/aspose-barcode-php-25.9.jar b/lib/aspose-barcode-php-25.10.jar similarity index 89% rename from lib/aspose-barcode-php-25.9.jar rename to lib/aspose-barcode-php-25.10.jar index 144e6d87..0bef29d2 100644 Binary files a/lib/aspose-barcode-php-25.9.jar and b/lib/aspose-barcode-php-25.10.jar differ