Skip to content

Commit 250216c

Browse files
authored
Merge pull request #7 from aspose-barcode/develop
Release 25.10
2 parents ddac001 + 38c90fc commit 250216c

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

lib/Internal/CommonUtility.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static function convertAreasToStringFormattedAreas($areas): array
118118
return $stringFormattedAreas;
119119
}
120120

121-
public static function convertImageResourceToBase64($imageResource): ?string
121+
public static function convertImageResourceToBinared($imageResource): ?string
122122
{
123123
if ($imageResource === null) {
124124
return null;
@@ -132,13 +132,16 @@ public static function convertImageResourceToBase64($imageResource): ?string
132132
case 'gd':
133133
ob_start();
134134
imagepng($imageResource);
135-
$imageData = ob_get_clean(); // same as ob_get_contents() + ob_end_clean()
136-
return base64_encode($imageData);
135+
$imageData = ob_get_clean();
136+
return $imageData;
137137

138138
case 'file':
139139
$imageData = stream_get_contents($imageResource);
140140
fclose($imageResource);
141-
return base64_encode($imageData);
141+
if ($imageData === false) {
142+
throw new BarcodeException("Failed to read from file resource");
143+
}
144+
return $imageData;
142145

143146
default:
144147
throw new BarcodeException("Unsupported resource type: $type");
@@ -154,18 +157,19 @@ public static function convertImageResourceToBase64($imageResource): ?string
154157
if ($fileContent === false) {
155158
throw new BarcodeException("Failed to read file content: $imageResource");
156159
}
157-
return base64_encode($fileContent);
160+
return $fileContent;
158161
}
159162

160-
161-
// If string is already valid base64 (safe check)
162-
if (base64_encode(base64_decode($imageResource, true)) === $imageResource) {
163-
return $imageResource;
163+
// If string is base64
164+
$decoded = base64_decode($imageResource, true);
165+
if ($decoded !== false && base64_encode($decoded) === $imageResource) {
166+
return $decoded;
164167
}
165-
throw new BarcodeException("File does not exist or is not readable: $imageResource");
168+
169+
throw new BarcodeException("File does not exist, is unreadable, or invalid base64 string: $imageResource");
166170
}
167171

168-
// Unsupported input
172+
// Unsupported input type
169173
throw new BarcodeException("Unsupported input type. Expected GD resource, file resource, base64 string, or valid file path.");
170174
}
171175
}

lib/Recognition/BarCodeReader.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ private function getBarCodeReaderDto(): BarcodeReaderDTO
3939
private function setBarCodeReaderDto(BarcodeReaderDTO $barCodeReaderDto): void
4040
{
4141
$this->barCodeReaderDto = $barCodeReaderDto;
42-
// $this->initFieldsFromDto();
4342
}
4443

4544
private QualitySettings $qualitySettings;
@@ -86,7 +85,7 @@ public function initFieldsFromDto()
8685
*/
8786
private function initializeImageRelatedFields(): void
8887
{
89-
$this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBase64($this->imageResource);
88+
$this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBinared($this->imageResource);
9089
$this->barCodeReaderDto->areas = CommonUtility::convertAreasToStringFormattedAreas($this->areas);
9190
}
9291

@@ -370,7 +369,7 @@ public final function setBarCodeImage($imageResource, ?Rectangle ...$areas): voi
370369
{
371370
try
372371
{
373-
$this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBase64($imageResource);
372+
$this->barCodeReaderDto->base64Image = CommonUtility::convertImageResourceToBinared($imageResource);
374373
$this->barCodeReaderDto->areas = CommonUtility::convertAreasToStringFormattedAreas($areas);
375374
if (is_null($this->barCodeReaderDto->barCodeDecodeTypes) || sizeof($this->barCodeReaderDto->barCodeDecodeTypes) == 0)
376375
$this->barCodeReaderDto->barCodeDecodeTypes = array(DecodeType::ALL_SUPPORTED_TYPES);
Binary file not shown.

0 commit comments

Comments
 (0)