@@ -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}
0 commit comments