Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/Internal/CommonUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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.");
}
}
5 changes: 2 additions & 3 deletions lib/Recognition/BarCodeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ private function getBarCodeReaderDto(): BarcodeReaderDTO
private function setBarCodeReaderDto(BarcodeReaderDTO $barCodeReaderDto): void
{
$this->barCodeReaderDto = $barCodeReaderDto;
// $this->initFieldsFromDto();
}

private QualitySettings $qualitySettings;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
Binary file not shown.