Use Base64Encoder/Decoder to Store/Restore images.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Use Base64Encoder/Decoder to Store/Restore images.

Post by Flype »

Include File : Base64Ex.PBI

Code: Select all

;- 
;- Object: Base64 Decoding/Encoding for images.
;- 
;- Usage:  These functions can be use to store/restore images
;-         in plain text such as in Mails, XML, Databases.
;- 
;- Target: All OS, PureBasic 4.x.
;- 

EnableExplicit

Procedure.s Base64EncodeFile(FileName.s) ; Encode a file to a Base64 string.
  
  Protected FileID.l, FileSize.l, FileBuff.l, Base64Size.l, Base64Buff.s
  
  FileID = ReadFile(#PB_Any, FileName)
  If FileID
    FileSize = Lof(FileID)
    FileBuff = AllocateMemory(FileSize)
    If FileBuff
      ReadData(FileID, FileBuff, FileSize)
      Base64Size = Int(1.35 * FileSize)
      Base64Buff = Space(Base64Size) ; 35% bigger than the original
      Base64Encoder(FileBuff, FileSize, @Base64Buff, Base64Size)
      FreeMemory(FileBuff)
    EndIf
    CloseFile(FileID)
  EndIf
  
  ProcedureReturn Base64Buff
  
EndProcedure
Procedure.l Base64CatchImage(Image.l, Base64.s, flags.l = 0) ; Create a new image from a Base64 string.
  
  Protected Base64Size.l, ImageBuff.l, ImageSize.l, result.l
  
  Base64Size = Len(Base64)
  ImageBuff = AllocateMemory(Base64Size)
  
  If ImageBuff
    ImageSize = Base64Decoder(@Base64, Base64Size, ImageBuff, Base64Size)
    If ImageSize
      result = CatchImage(Image, ImageBuff, ImageSize, flags)
    EndIf
    FreeMemory(ImageBuff)
  EndIf
  
  ProcedureReturn result
  
EndProcedure

DisableExplicit



Store image into a database

Code: Select all

If DatabaseUpdate(0, "INSERT INTO tbl_image (id,base64) VALUES('test.bmp','" + Base64EncodeFile("test.bmp") + "')")
  ; Success
EndIf



Restore image from a database

Code: Select all

If DatabaseQuery(0, "SELECT base64 FROM tbl_image WHERE id='test.bmp'") 
  If NextDatabaseRow(0)
    If Base64CatchImage(0, GetDatabaseString(0,0))
      ; Success
    EndIf
  EndIf
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Nice tip, Flype! Just one tiny suggestion, I think it would be well to do a PackMemory before the Base64Encode for the first procedure and an UnpackMemory after the Base64Decode in the Catch procedure, then you've got a pretty-near-perfect little system there. 'Course if you're using images already packed such as PNGs it won't help much but it won't hurt either and BMPs or TIFFs will benefit greatly.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Very nice. I could find a use for this. 8)

Thanks.
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

It's good to see someone adapt this to standard PB database commands. I'm using something similar for SqLite quite happily, that GedB showed me in 2006.

Never even thought about the PackMemory part though. Nice idea.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

ok i will add the PackMemory part as an optional argument.
It could be efficient for bmp or ico image (or any binary data).
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Use Base64Encoder/Decoder to Store/Restore images.

Post by tikidays »

This doesn't seem to run under the latest PB, is it possible to update it? How would I encoded a image already in memory, not loaded from a file?
plouf
Enthusiast
Enthusiast
Posts: 282
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: Use Base64Encoder/Decoder to Store/Restore images.

Post by plouf »

Pb has internal commands nowdays
Base64Encoder. And Base64Decoder
No need to use external
Christos
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Use Base64Encoder/Decoder to Store/Restore images.

Post by infratec »

These inbuild procedures are used in the code :wink:
Post Reply