Page 1 of 1

Use Base64Encoder/Decoder to Store/Restore images.

Posted: Fri Dec 29, 2006 3:40 pm
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

Posted: Fri Dec 29, 2006 4:33 pm
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.

Posted: Sun Dec 31, 2006 3:19 pm
by srod
Very nice. I could find a use for this. 8)

Thanks.

Posted: Sun Dec 31, 2006 3:25 pm
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.

Posted: Sun Dec 31, 2006 5:42 pm
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).

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

Posted: Sun Dec 31, 2023 2:09 am
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?

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

Posted: Sun Dec 31, 2023 6:45 am
by plouf
Pb has internal commands nowdays
Base64Encoder. And Base64Decoder
No need to use external

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

Posted: Sun Dec 31, 2023 10:39 am
by infratec
These inbuild procedures are used in the code :wink: