## 2 egd

### 2.1 Introduction

The egd module is an interface for the gd library and is used by Percept to generate dynamic graphs to its web pages.

Please see libgd.org for futher details about the gd library.

The foremost purpose for this module is to enable users to generate images from erlang code and/or datasets and to send these images to either files or web servers.

### 2.2 File example

Drawing examples:

    -module(img).

    -export([do/0]).

    do() ->
        Im = egd:create(200,200),
        White = egd:color(Im, white),
        Green = egd:color(Im, green),
        Blue = egd:color(Im, blue),
        Red = egd:color(Im, red),
        Black = egd:color(Im, black),
        Yellow = egd:color(Im, {255,255,0}),

        egd:fill(Im, {10,10}, White),

        % Line and fillRectangle

        egd:filledRectangle(Im, {20,20}, {180,180}, Red),
        egd:line(Im, {0,0}, {200,200}, Black),

        egd:save(egd:image(Im, gif), "/home/egil/test1.gif"),

        % Fill border

        egd:fill(Im, {15,5}, Blue),
        egd:fill(Im, {115,100}, Green),

        egd:save(egd:image(Im, gif), "/home/egil/test2.gif"),

        % Pacman filledArc
        egd:filledArc(Im, {100,100}, 100,100, 28,332, Yellow, [arc]),
        egd:filledArc(Im, {100,100}, 100,100, 28,332, Black, [arc, no_fill, edged]),

        egd:save(egd:image(Im, gif), "/home/egil/test3.gif"),

        % Text
        {W,H} = egd:fontSize(Im, giant),
        String = "egd says hello!",
        Length = length(String),
        egd:text(Im, giant, {round(100 - W*Length/2), 200 - H - 5}, String, Black),
        egd:save(egd:image(Im, gif), "/home/egil/test4.gif"),

        egd:destroy(Im).

test1.gif
test2.gif
test3.gif
test4.gif

### 2.3 ESI example

Using egd with inets ESI to generate images on the fly:

    -module(img_esi).

    -export([image/3]).

    image(SessionID, _Env, _Input) ->
        mod_esi:deliver(SessionID, header()),
        Binary = my_image(),
        mod_esi:deliver(SessionID, binary_to_list(Binary)).

    my_image() ->
        Im = egd:create(300,20),
        White = egd:color(Im, white),
        Black = egd:color(Im, black),
        Red = egd:color(Im, red),
        egd:fill(Im, {20,10}, White),
        egd:filledRectangle(Im, {30,14}, {270,19}, Red),
        egd:rectangle(Im, {30,14}, {270,19}, Black),
        egd:text(Im, medium, {30, 0}, "egd with esi callback", Black),
        Bin = egd:image(Im, png),
        egd:destroy(Im),
        Bin.

    header() ->
        "Content-Type: image/png\r\n\r\n".

Example of result.

For more information regarding ESI, please see inets application mod_esi.

percept 0.6.2
Copyright © 1991-2008 Ericsson AB