Class: GD::Image

Inherits:
Object
  • Object
show all
Defined in:
ext/gd/image.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'ext/gd/image.c', line 3

static VALUE gd_image_initialize(int argc, VALUE *argv, VALUE self) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  wrap->img = NULL;

  if (argc == 0) {
    return self;
  }
  if (argc != 2) {
    rb_raise(rb_eArgError, "expected 0 or 2 arguments");
  }

  int w = NUM2INT(argv[0]);
  int h = NUM2INT(argv[1]);
  if (w <= 0 || h <= 0)
    rb_raise(rb_eArgError, "width and height must be positive");

  wrap->img = gdImageCreateTrueColor(w, h);
  if (!wrap->img)
    rb_raise(rb_eRuntimeError, "gdImageCreateTrueColor failed");

  return self;
}

Class Method Details

.new_true_color(width, height) ⇒ Object



61
62
63
64
65
# File 'ext/gd/image.c', line 61

static VALUE gd_image_s_new_true_color(VALUE klass, VALUE width, VALUE height) {
  VALUE obj = rb_class_new_instance(0, NULL, klass);
  gd_image_initialize_true_color(obj, width, height);
  return obj;
}

.open(path) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/gd/image.c', line 127

static VALUE gd_image_open(VALUE klass, VALUE path) {
  const char *filename = StringValueCStr(path);

  FILE *f = fopen(filename, "rb");
  if (!f) rb_sys_fail(filename);

  gdImagePtr img = NULL;

  const char *ext = strrchr(filename, '.');
  if (!ext) rb_raise(rb_eArgError, "unknown image type");

  if (strcmp(ext, ".png") == 0) {
    img = gdImageCreateFromPng(f);
  } else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) {
    img = gdImageCreateFromJpeg(f);
  } else if (strcmp(ext, ".webp") == 0) {
    img = gdImageCreateFromWebp(f);
  } else if (strcmp(ext, ".gif") == 0) {
    img = gdImageCreateFromGif(f);
  } else {
    fclose(f);
    rb_raise(rb_eArgError, "unsupported format");
  }

  fclose(f);
  if (!img) rb_raise(rb_eRuntimeError, "image decode failed");

  VALUE obj = rb_class_new_instance(0, NULL, klass);
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(obj, gd_image_wrapper, &gd_image_type, wrap);
  wrap->img = img;

  return obj;
}

Instance Method Details

#alpha_blending=(flag) ⇒ Object



162
163
164
165
166
167
168
# File 'ext/gd/image.c', line 162

static VALUE gd_image_alpha_blending(VALUE self, VALUE flag) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    gdImageAlphaBlending(wrap->img, RTEST(flag));
    return self;
}

#arc(vcx, vcy, vw, vh, vstart, vend, vcolor) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'ext/gd/arc.c', line 3

static VALUE gd_image_arc(VALUE self,
    VALUE vcx, VALUE vcy,
    VALUE vw, VALUE vh,
    VALUE vstart, VALUE vend,
    VALUE vcolor
) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    int cx = NUM2INT(vcx);
    int cy = NUM2INT(vcy);
    int w  = NUM2INT(vw);
    int h  = NUM2INT(vh);
    int s  = NUM2INT(vstart);
    int e  = NUM2INT(vend);

    int c = color_to_gd(wrap->img, vcolor);

    gdImageArc(wrap->img, cx, cy, w, h, s, e, c);

    return self;
}

#circle(cx, cy, r, color) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'ext/gd/circle.c', line 3

static VALUE gd_image_circle(VALUE self, VALUE cx, VALUE cy, VALUE r, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int red = NUM2INT(rb_ary_entry(color, 0));
  int green = NUM2INT(rb_ary_entry(color, 1));
  int blue = NUM2INT(rb_ary_entry(color, 2));
  int c = gdImageColorAllocate(wrap->img, red, green, blue);

  gdImageArc(wrap->img,
             NUM2INT(cx), NUM2INT(cy),
             NUM2INT(r)*2, NUM2INT(r)*2,
             0, 360,
             c);
  return Qnil;
}

#cloneObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/gd/image.c', line 67

static VALUE gd_image_clone(VALUE self) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  if (!wrap->img) rb_raise(rb_eRuntimeError, "cannot clone: image is NULL");

  gdImagePtr copy = gdImageClone(wrap->img);
  if (!copy) rb_raise(rb_eRuntimeError, "gdImageClone failed");

  VALUE obj = rb_class_new_instance(0, NULL, CLASS_OF(self));
  gd_image_wrapper *w2;
  TypedData_Get_Struct(obj, gd_image_wrapper, &gd_image_type, w2);
  w2->img = copy;

  return obj;
}

#copy_merge(src, dx, dy, sx, sy, w, h, pct) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'ext/gd/blit.c', line 10

static VALUE gd_image_copy_merge(
  VALUE self, VALUE src,
  VALUE dx, VALUE dy,
  VALUE sx, VALUE sy,
  VALUE w, VALUE h,
  VALUE pct
) {
  gd_image_wrapper *dst_wrap;
  gd_image_wrapper *src_wrap;

  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, dst_wrap);
  TypedData_Get_Struct(src,  gd_image_wrapper, &gd_image_type, src_wrap);

  gdImageCopyMerge(
    dst_wrap->img,
    src_wrap->img,
    NUM2INT(dx), NUM2INT(dy),
    NUM2INT(sx), NUM2INT(sy),
    NUM2INT(w),  NUM2INT(h),
    NUM2INT(pct)
  );

  return self;
}

#crop(vx, vy, vw, vh) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'ext/gd/transform.c', line 11

static VALUE gd_image_crop(VALUE self, VALUE vx, VALUE vy, VALUE vw, VALUE vh) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    gdRect r;
    r.x = NUM2INT(vx);
    r.y = NUM2INT(vy);
    r.width  = NUM2INT(vw);
    r.height = NUM2INT(vh);

    gdImagePtr out = gdImageCrop(wrap->img, &r);
    if (!out) rb_raise(rb_eRuntimeError, "crop failed");

    return wrap_new_image(out, CLASS_OF(self));
}

#fill(color) ⇒ Object

  • x

    imagefill — Flood fill



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'ext/gd/fill.c', line 6

static VALUE gd_image_fill(VALUE self, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  if (!wrap || !wrap->img)
    rb_raise(rb_eRuntimeError, "uninitialized GD::Image");

  int r = NUM2INT(rb_ary_entry(color,0));
  int g = NUM2INT(rb_ary_entry(color,1));
  int b = NUM2INT(rb_ary_entry(color,2));
  int c = gdImageColorAllocate(wrap->img,r,g,b);

  gdImageFilledRectangle(wrap->img,0,0,wrap->img->sx,wrap->img->sy,c);
  return Qnil;
}

#filled_arc(vcx, vcy, vw, vh, vstart, vend, vcolor) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/gd/arc.c', line 26

static VALUE gd_image_filled_arc(VALUE self,
    VALUE vcx, VALUE vcy,
    VALUE vw, VALUE vh,
    VALUE vstart, VALUE vend,
    VALUE vcolor
) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    int cx = NUM2INT(vcx);
    int cy = NUM2INT(vcy);
    int w  = NUM2INT(vw);
    int h  = NUM2INT(vh);
    int s  = NUM2INT(vstart);
    int e  = NUM2INT(vend);

    int c = color_to_gd(wrap->img, vcolor);

    gdImageFilledArc(wrap->img, cx, cy, w, h, s, e, c, gdArc);

    return self;
}

#filled_circle(cx, cy, r, color) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'ext/gd/circle.c', line 20

static VALUE gd_image_filled_circle(VALUE self, VALUE cx, VALUE cy, VALUE r, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int red = NUM2INT(rb_ary_entry(color, 0));
  int green = NUM2INT(rb_ary_entry(color, 1));
  int blue = NUM2INT(rb_ary_entry(color, 2));
  int c = gdImageColorAllocate(wrap->img, red, green, blue);

  gdImageFilledEllipse(wrap->img,
                       NUM2INT(cx), NUM2INT(cy),
                       NUM2INT(r)*2, NUM2INT(r)*2,
                       c);
  return Qnil;
}

#filled_polygon(points, color) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/gd/polygon.c', line 72

static VALUE gd_image_filled_polygon(VALUE self, VALUE points, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  if (!wrap || !wrap->img) {
    rb_raise(rb_eRuntimeError, "uninitialized GD::Image");
  }

  int count = 0;
  gdPointPtr pts = build_points(points, &count);

  if (!pts || count < 3) {
    if (pts) xfree(pts);
    rb_raise(rb_eArgError, "points must be an Array of at least 3 [x,y] pairs");
  }

  int c = color_to_gd(wrap->img, color);
  gdImageFilledPolygon(wrap->img, pts, count, c);

  xfree(pts);
  return self;
}

#filled_rect(x1, y1, x2, y2, color) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/gd/rectangle.c', line 22

static VALUE gd_image_filled_rect(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int r = NUM2INT(rb_ary_entry(color, 0));
  int g = NUM2INT(rb_ary_entry(color, 1));
  int b = NUM2INT(rb_ary_entry(color, 2));

  int c = gdImageColorAllocate(wrap->img, r, g, b);
  gdImageFilledRectangle(wrap->img,
                         NUM2INT(x1), NUM2INT(y1),
                         NUM2INT(x2), NUM2INT(y2),
                         c);
  return Qnil;
}

#filter(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/gd/filter.c', line 4

static VALUE gd_image_filter(int argc, VALUE *argv, VALUE self) {
  VALUE type, arg1, arg2, arg3, arg4;
  rb_scan_args(argc, argv, "14", &type, &arg1, &arg2, &arg3, &arg4);

  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  type = rb_obj_as_string(type);
  const char *name = StringValueCStr(type);

  if (strcmp(name, "negate") == 0) {
    gdImageNegate(wrap->img);
  }
  else if (strcmp(name, "grayscale") == 0) {
    gdImageGrayScale(wrap->img);
  }
  else if (strcmp(name, "brightness") == 0) {
    gdImageBrightness(wrap->img, NUM2INT(arg1));
  }
  else if (strcmp(name, "contrast") == 0) {
    gdImageContrast(wrap->img, NUM2INT(arg1));
  }
  else if (strcmp(name, "colorize") == 0) {
    rb_raise(rb_eNotImpError, "colorize not supported by libgd");
  }
  else if (strcmp(name, "edge_detect") == 0) {
    gdImageEdgeDetectQuick(wrap->img);
  }
  else if (strcmp(name, "emboss") == 0) {
    gdImageEmboss(wrap->img);
  }
  else if (strcmp(name, "gaussian_blur") == 0) {
    gdImageGaussianBlur(wrap->img);
  }
  else if (strcmp(name, "selective_blur") == 0) {
    gdImageSelectiveBlur(wrap->img);
  }
  else if (strcmp(name, "mean_removal") == 0) {
    gdImageMeanRemoval(wrap->img);
  }
  else if (strcmp(name, "smooth") == 0) {
    gdImageSmooth(wrap->img, NUM2INT(arg1));
  }
  else if (strcmp(name, "pixelate") == 0) {
    gdImagePixelate(
      wrap->img,
      NUM2INT(arg1),
      RTEST(arg2)
    );
  }
  else if (strcmp(name, "scatter") == 0) {
    gdImageScatter(
      wrap->img,
      NUM2INT(arg1),
      NUM2INT(arg2)
    );
  }
  else {
    rb_raise(rb_eArgError, "unknown filter");
  }

  return self;
}

#get_pixel(x, y) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'ext/gd/pixel.c', line 18

static VALUE gd_image_get_pixel(VALUE self, VALUE x, VALUE y) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int c = gdImageGetPixel(wrap->img, NUM2INT(x), NUM2INT(y));

  int r = gdImageRed(wrap->img, c);
  int g = gdImageGreen(wrap->img, c);
  int b = gdImageBlue(wrap->img, c);

  VALUE ary = rb_ary_new_capa(3);
  rb_ary_push(ary, INT2NUM(r));
  rb_ary_push(ary, INT2NUM(g));
  rb_ary_push(ary, INT2NUM(b));

  return ary;
}

#heightObject



184
185
186
187
188
# File 'ext/gd/image.c', line 184

static VALUE gd_image_height(VALUE self) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);
    return INT2NUM(gdImageSY(wrap->img));
}

#line(x1, y1, x2, y2, color) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/gd/draw_line.c', line 5

static VALUE gd_image_line(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int ix1 = NUM2INT(x1);
  int iy1 = NUM2INT(y1);
  int ix2 = NUM2INT(x2);
  int iy2 = NUM2INT(y2);

  int ox1 = ix1;
  int oy1 = iy1;
  int ox2 = ix2;
  int oy2 = iy2;

  int w = wrap->img->sx;
  int h = wrap->img->sy;

  /* Clip to image bounds */
  int xmin = 0;
  int ymin = 0;
  int xmax = w - 1;
  int ymax = h - 1;

  if (!gd_clip_line_to_rect(&ix1, &iy1, &ix2, &iy2, xmin, ymin, xmax, ymax)) {
    rb_warn("Line is completely outside image bounds");
    return Qnil;
  }

  if (ix1 != ox1 || iy1 != oy1 || ix2 != ox2 || iy2 != oy2) {
    rb_warn("Line coordinates clipped to image bounds");
  }

  if (TYPE(color) != T_ARRAY || RARRAY_LEN(color) != 3) {
    rb_raise(rb_eArgError, "color must be [r,g,b]");
  }

  int r = NUM2INT(rb_ary_entry(color, 0));
  int g = NUM2INT(rb_ary_entry(color, 1));
  int b = NUM2INT(rb_ary_entry(color, 2));

  int c = gdImageColorAllocate(wrap->img, r, g, b);
  gdImageLine(wrap->img, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), c);

  return Qnil;
}

#polygon(points, color) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/gd/polygon.c', line 58

static VALUE gd_image_polygon(VALUE self, VALUE points, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int count = 0;
  gdPointPtr pts = build_points(points, &count);
  int c = color_to_gd(wrap->img, color);

  gdImagePolygon(wrap->img, pts, count, c);

  xfree(pts);
  return self;
}

#rect(x1, y1, x2, y2, color) ⇒ Object

  • imagerectangle — Draw a rectangle

  • imagefilledrectangle — Draw a filled rectangle



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'ext/gd/rectangle.c', line 6

static VALUE gd_image_rect(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int r = NUM2INT(rb_ary_entry(color, 0));
  int g = NUM2INT(rb_ary_entry(color, 1));
  int b = NUM2INT(rb_ary_entry(color, 2));

  int c = gdImageColorAllocate(wrap->img, r, g, b);
  gdImageRectangle(wrap->img,
                   NUM2INT(x1), NUM2INT(y1),
                   NUM2INT(x2), NUM2INT(y2),
                   c);
  return Qnil;
}

#resize(vw, vh) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'ext/gd/transform.c', line 27

static VALUE gd_image_scale(VALUE self, VALUE vw, VALUE vh) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    int w = NUM2INT(vw);
    int h = NUM2INT(vh);

    gdImagePtr out = gdImageScale(wrap->img, w, h);
    if (!out) rb_raise(rb_eRuntimeError, "scale failed");

    return wrap_new_image(out, CLASS_OF(self));
}

#save(*args) ⇒ Object

  • imagegif — Output image to browser or file

  • x

    imagewbmp — Output image to browser or file

  • x

    imagewebp — Output a WebP image to browser or file

  • imagexbm — Output an XBM image to browser or file

  • x

    imagepng — Output a PNG image to either the browser or a file

  • x

    imagejpeg — Output image to browser or file

  • imagegd — Output GD image to browser or file

  • imagegd2 — Output GD2 image to browser or file

  • imageavif — Output image to browser or file

  • imagebmp — Output a BMP image to browser or file



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/gd/encode.c', line 15

static VALUE gd_image_save(int argc, VALUE *argv, VALUE self) {
  VALUE path, opts;
  rb_scan_args(argc, argv, "11", &path, &opts);

  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  const char *filename = StringValueCStr(path);

  const char *ext = strrchr(filename, '.');
  if (!ext) rb_raise(rb_eArgError, "file extension required");

  FILE *f = fopen(filename, "wb");
  if (!f) rb_sys_fail(filename);

  if (strcmp(ext, ".png") == 0) {
    gdImagePng(wrap->img, f);
  } else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) {
    int quality = 90;
    if (opts != Qnil) {
      VALUE q = rb_hash_aref(opts, ID2SYM(rb_intern("quality")));
      if (!NIL_P(q)) quality = NUM2INT(q);
    }
    gdImageJpeg(wrap->img, f, quality);
  } else if (strcmp(ext, ".webp") == 0) {
    gdImageWebp(wrap->img, f);
  } else {
    fclose(f);
    rb_raise(rb_eArgError, "unsupported format");
  }

  fclose(f);
  return Qtrue;
}

#save_alpha=(flag) ⇒ Object



170
171
172
173
174
175
176
# File 'ext/gd/image.c', line 170

static VALUE gd_image_save_alpha(VALUE self, VALUE flag) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    gdImageSaveAlpha(wrap->img, RTEST(flag));
    return self;
}

#scale(vw, vh) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'ext/gd/transform.c', line 27

static VALUE gd_image_scale(VALUE self, VALUE vw, VALUE vh) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

    int w = NUM2INT(vw);
    int h = NUM2INT(vh);

    gdImagePtr out = gdImageScale(wrap->img, w, h);
    if (!out) rb_raise(rb_eRuntimeError, "scale failed");

    return wrap_new_image(out, CLASS_OF(self));
}

#set_pixel(x, y, color) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'ext/gd/pixel.c', line 3

static VALUE gd_image_set_pixel(VALUE self, VALUE x, VALUE y, VALUE color) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int r = NUM2INT(rb_ary_entry(color,0));
  int g = NUM2INT(rb_ary_entry(color,1));
  int b = NUM2INT(rb_ary_entry(color,2));

  int c = gdImageColorAllocate(wrap->img, r, g, b);

  gdImageSetPixel(wrap->img, NUM2INT(x), NUM2INT(y), c);

  return Qnil;
}

#text(text, opts) ⇒ Object

  • imagefontheight — Get font height

  • imagefontwidth — Get font width

  • imageftbbox — Give the bounding box of a text using fonts via freetype2

  • imagefttext — Write text to the image using fonts using FreeType 2



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/gd/text.c', line 9

static VALUE gd_image_text(VALUE self, VALUE text, VALUE opts) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  VALUE x = rb_hash_aref(opts, ID2SYM(rb_intern("x")));
  VALUE y = rb_hash_aref(opts, ID2SYM(rb_intern("y")));
  VALUE size = rb_hash_aref(opts, ID2SYM(rb_intern("size")));
  VALUE color = rb_hash_aref(opts, ID2SYM(rb_intern("color")));
  VALUE font = rb_hash_aref(opts, ID2SYM(rb_intern("font")));

  if (NIL_P(x) || NIL_P(y) || NIL_P(size) || NIL_P(color) || NIL_P(font))
    rb_raise(rb_eArgError, "missing options");

  int r = NUM2INT(rb_ary_entry(color,0));
  int g = NUM2INT(rb_ary_entry(color,1));
  int b = NUM2INT(rb_ary_entry(color,2));

  int fg = gdImageColorAllocate(wrap->img, r, g, b);

  int brect[8];
  char *err = gdImageStringFT(
    wrap->img,
    brect,
    fg,
    StringValueCStr(font),
    NUM2DBL(size),
    0,
    NUM2INT(x),
    NUM2INT(y),
    StringValueCStr(text)
  );

  if (err) rb_raise(rb_eRuntimeError, "%s", err);

  return Qnil;
}

#to_pngObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/gd/encode.c', line 50

static VALUE gd_image_to_png(VALUE self) {
  gd_image_wrapper *wrap;
  TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);

  int size = 0;
  void *data = gdImagePngPtr(wrap->img, &size);
  if (!data) rb_raise(rb_eRuntimeError, "PNG encode failed");

  VALUE str = rb_str_new((const char*)data, size);
  gdFree(data);
  return str;
}

#widthObject



178
179
180
181
182
# File 'ext/gd/image.c', line 178

static VALUE gd_image_width(VALUE self) {
    gd_image_wrapper *wrap;
    TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap);
    return INT2NUM(gdImageSX(wrap->img));
}