Skip to content

Commit 19317ec

Browse files
authored
Merge pull request #599 from vepo/main
2 parents 42f1484 + f52b900 commit 19317ec

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

core/src/processing/core/PShape.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,23 @@ static protected PImage parseBase64Image(String imagePath) {
19851985
String extension = parts[0].substring(11);
19861986
String encodedData = parts[1];
19871987

1988-
byte[] decodedBytes = Base64.getDecoder().decode(encodedData);
1988+
// If a SVG image has an error, the base 64 will fail, but the browser will work.
1989+
// So we are sanitizing the values before reading it.
1990+
// If you have any questions, please, read this
1991+
// reference: https://www.prostdev.com/post/understanding-the-illegal-base64-character-error-java-groovy-and-mule-4-dw-2-0
1992+
Base64.Decoder decoder;
1993+
if (encodedData.contains("+") || encodedData.contains("/")) {
1994+
decoder = Base64.getDecoder();
1995+
encodedData = encodedData.replaceAll("[^A-Za-z0-9+/=]", "");
1996+
} else if (encodedData.contains("-") || encodedData.contains("_")) {
1997+
decoder = Base64.getUrlDecoder();
1998+
encodedData = encodedData.replaceAll("[^A-Za-z0-9-_=]", "");
1999+
} else {
2000+
decoder = Base64.getDecoder();
2001+
encodedData = encodedData.replaceAll("[^A-Za-z0-9+/=]", "");
2002+
}
2003+
2004+
byte[] decodedBytes = decoder.decode(encodedData);
19892005

19902006
if (decodedBytes == null) {
19912007
System.err.println("Decode Error on image: " + imagePath.substring(0, 20));

0 commit comments

Comments
 (0)