masynchin

masynchin

Need help with :egd

I currenly learning Elixir and want to manipulate images with it. I use :egd to calculate sizes of text image that I will render.

My code produces this error:

** (MatchError) no match of right hand side value: []
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd_font.erl:62: :egd_font.glyph/2
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd_render.erl:601: :egd_render.text_intervals/5
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd_render.erl:339: :egd_render.precompile_objects/1
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd_render.erl:317: :egd_render.precompile/1
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd_render.erl:39: :egd_render.binary/2
    (egd 0.10.0) c:/expro/image_text/deps/egd/src/egd.erl:239: :egd.loop/1

My code:

defmodule ImageText do
  @moduledoc """
  Documentation for `ImageText`.
  """

  def main() do
    render_text("hello")
  end

  def font() do
    :egd_font.load("lib/fonts/font.wingsfont")
  end

  def render_text(text) do
    {image_width, image_height} = calculate_text_size(text)
    image = :egd.create(image_width, image_height)
    :egd.text(image, {0, 0}, font(), String.graphemes(text), :black)
    :egd.save(:egd.render(image), "lib/image.png")
    :egd.destroy(image)
  end

  def calculate_text_size(text) do
    {font_width, font_height} = :egd_font.size(font())

    {font_width * String.length(text), font_height}
  end
end

lib/fonts/font.wingsfont.ttf is https://github.com/erlang/egd/blob/master/priv/fonts/6x11_latin1.wingsfont

Most Liked

christhekeele

christhekeele

Welcome to the forum!

It looks like the examples for :egd.text/5 use the erlang string type (the double quote literal). This is shorthand for what Elixir calls a charlist, so that is what you will need to convert your binaries into. But you need to be using this for all your string input:

  • "lib/fonts/font.wingsfont"
  • text
  • "lib/image.png"

Additionally, you are using a literal atom :black. The erl guides actually use an erlang variable for this, Black = egd:color({0,0,0}) (lowercased barewords are atom literals in erlang, uppercase is variables). So I would replace the atom with :egd.color({0,0,0}).

masynchin

masynchin

Very thanks, now it works!
this should be rendered image, but I not allowed to upload it :grin:

epilgrim

epilgrim

I’m afk, so I cannot check this, but have you tried using charlists instead of a bynary when calling load?
that would be changing the quotes to :egd_font.load(‘lib/fonts/font.wingsfont’)
usually this is the first source of troubles when interfacing with erlang

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement