0

I would like to get the image data part out of a base64 image, to alter it. I tried this : (origin : How can I strip the data:image part from a base64 string of any image type in Javascript)

let originalImage = myBase64Image
let strImage = originalImage.replace(/^data:image\/[a-z]+;base64,/, "");

The problem with the snippet is that when you alter strImage that you don't alter the originalImage. Does anyone now a way of converting the strImage back to a base64 image or a way to directly alter the image data of the originalImage?

jps
  • 20,041
  • 15
  • 75
  • 79
Jip Helsen
  • 1,034
  • 4
  • 15
  • 30
  • Normally I would load the image into a hidden element and then I can change it all the way I want. But I do not know what you want to do with the image data. – Thallius Oct 27 '21 at 12:12
  • I would like to encrypt en decrypt it. But you can replace the encryption part by a simple inverse function as they both just need a string input. – Jip Helsen Oct 27 '21 at 12:17
  • So what is the advantage of encrypting only the image data and not the whole base64 string? – Thallius Oct 27 '21 at 12:20
  • Every base64 string start with "data:image/", that and some other things are constant in every base64 image. If you were to just encrypt the whole image anyone could decrypt it by tracing back the key from the constant parts. Therefor I only want to alter the base64 imageData. – Jip Helsen Oct 27 '21 at 12:34

0 Answers0