Scrivere un testo in un'immagine mediante le funzioni del API di Imlib2

Da Gambas-it.org - Wikipedia.

La libreria Imlib2 consente di gestire, elaborare e salvare file immagini.

Con alcune sue risorse è possibile scrivere del testo all'interno di un'immagine, anche operando con un programma a riga di comando, e quindi anche al di fuori di qualunque ambiente grafico fornito da Gambas.

Per poter fruire delle sue funzionalità in Gambas, è necessario richiamare la libreria (nella sua attuale versione): "libImlib2.so.1.7.4 ".

Mostriamo un semplice esempio:

Library "libImlib2:1.7.4"
 
' Imlib_Image imlib_load_image_immediately(const char *file)
' Loads an image from disk located at the path specified by file.
Private Extern imlib_load_image_immediately(filename As String) As Pointer

' void imlib_context_set_image(Imlib_Image image)
' Sets the current image Imlib2 will be using with its function calls.
Private Extern imlib_context_set_image(Imlib_Image As Pointer)

' Imlib_Font imlib_load_font(const char * font_name)
' Loads a truetype font from the first directory in the font path that contains that font.
Private Extern imlib_load_font(font_name As String) As Pointer

' void imlib_context_set_color(int red, int green, int blue, int alpha)
' Sets the color with which text, lines and rectangles are drawn when being rendered onto an image.
Private Extern imlib_context_set_color(red As Integer, green As Integer, blue As Integer, alpha As Integer)

' void imlib_context_set_font(Imlib_Font font)
' Sets the current font to use when rendering text.
Private Extern imlib_context_set_font(font As Pointer)

' void imlib_text_draw(int x, int y, const char * text)
' Draws the null-byte terminated string text using the current font on the current image
' at the (x, y) location (x, y denoting the top left corner of the font string).
Private Extern imlib_text_draw(x As Integer, y As Integer, text As String)

' void imlib_image_set_format (const char * format)
' Sets the format of the current image. This is used for when you wish to save an image in a different format that it was loaded in, or if the image currently has no file format associated with it.
Private Extern imlib_image_set_format(formato_immagine As String)

' void imlib_save_image(const char *filename)
' Saves the current image in the format specified by the current image's format settings to the filename filename.
Private Extern imlib_save_image(filename As String)


Public Sub Main()

 Dim ImlibImage, fnt As Pointer
 Dim w, h, i As Integer

' Carica il file immagine:
 ImlibImage = imlib_load_image_immediately("/percoso/del/file/immagine")

 imlib_context_set_image(ImlibImage)

' Imposta il completo percorso del font di tipo TTF e della dimensione da utilizzare con il testo.
' Il parametro è una stringa così impostata "percorso_del_font/dimensione_del_carattere".
' Qui facciamo un esempio concreto con il font "FreeSans.ttf", ed impostando la dimensione del carattere a "16":
 fnt = imlib_load_font("/usr/share/fonts/truetype/freefont/FreeSans.ttf/16")
 If fnt == 0 Then Error.Raise("Errore nel caricamento del font !")

' Volendo, si può impostare il colore del font (in formato RGBA):
 imlib_context_set_color(255, 255, 0, 255)

 imlib_context_set_font(fnt)

' Si impostano le coordinate ove verrà scritto il testo, nonché il testo da scrivere:
 imlib_text_draw(10, 20, "Testo qualsiasi")

' Si imposta il formato di file immagine da salvare.
' Nel parametro va inserita una stringa contenente il formato del file imagine (es: "png", "jpg", "bmp", e così via):
 imlib_image_set_format("formato_del_file")

 imlib_save_image("/percorso/del/file/immagine/da/salvare")

End


Riferimenti