vbscript code that helps write UTF-8 text

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Post Reply
User avatar
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

vbscript code that helps write UTF-8 text

Post by magick »

Simon Pedersen thought ImageMagick users might benefit from the following:


I think this code should be shared. When we switched our database to storing all data in UTF-8 format, we encountered a problem when using imageMagick to write text to graphic (typically when having non standard fonts)

Using ImageMagick to write text to a graphic file from a text-file, using -annotate, resulted in a strange character before the text. It turned out that this char was the Byte Order Mark that Windows uses in UTF-8 files.

I therefore created a couple of vbscript functions that remove the BOM and resaves the file.

Code: Select all

' Input is Unicode text and filePath is path to the image file we wish to create. First we create a utf-8 file 
' named the same as the image file and then we use the utf-8 file as an input to when creating the image file.

                                strResult = img.Convert( _ 
                                        "-size"                 , "200x200", _
                                        "-font"                 , "Arial-Bold", _       
                                        "-pointsize"    , "12", _
                                        "-fill"                 , "#B6B6B6", _
                                        "-annotate"             , "0x0+25+18"   ,writeUnicodeADODB(UCase(sText), strDefaultPath & "top_" & sFilename), _
                                        "-trim", _
                                        strDefaultPath & "template.png", _
                                        strDefaultPath & "top_" & sFilename)

Function writeUnicodeADODB(txtInput,filePath)
        ' Create and open stream
                Dim objStream
                Set objStream = CreateObject("ADODB.Stream")

        'Reset the position and indicate the charactor encoding
                objStream.Position = 0
                objStream.Charset = "UTF-8"
        'Write to the steam
                objStream.WriteText txtInput
        'Save the stream to a file
                filePath = filePath & ".txt"
                objStream.SaveToFile filePath, 2 ' overwrite if exists
        ' Return filepath with an @ so that imagemagick understands that it's a file
                writeUnicodeADODB = "@" & RemoveBOM(filePath)
        ' Kill stream
                Set objStream = Nothing
End Function

Code: Select all

' Removes the Byte Order Mark - BOM from a text file with UTF-8 encoding 
' The BOM defines that the file was stored with an UTF-8 encoding.
Public function RemoveBOM(filePath)
        ' Create a reader and a writer
                Dim writer,reader, fileSize
                Set writer = CreateObject("Adodb.Stream")
                Set reader = CreateObject("Adodb.Stream")
        ' Load from the text file we just wrote
                reader.LoadFromFile filePath
        ' Copy all data from reader to writer, except the BOM
                reader.copyto writer,-1 

        ' Overwrite file
                writer.SaveToFile filePath,2
        ' Return file name
                RemoveBOM = filePath

        ' Kill objects
                Set writer = Nothing 
                Set reader = Nothing

end Function
Kind regards,
Post Reply