Skip to content
This repository
Browse code

added transparency support to ImageResize (PNG and GIF)

(re)added "file not found" error handlers(cherry picked from commit 07b86e0)
  • Loading branch information...
commit 65559987b98ff129d7ceea212c43b52a0471a85c 1 parent 43ee0d3
mockey mockey authored committed

Showing 2 changed files with 25 additions and 5 deletions. Show diff stats Hide diff stats

  1. +16 3 src/nGDCopy.c
  2. +9 2 src/nGDFile.c
19 src/nGDCopy.c 100644 → 100755
@@ -56,11 +56,24 @@ value ImageResize(value img, value w,value h,value resample) {
56 56 int _r = val_bool(resample);
57 57 gdImagePtr cd;
58 58
59   - //creating new one
60   - if (gdImageTrueColor(imageImage(_img)))
  59 + int tc = gdImageTrueColor(imageImage(_img));
  60 + int transp;
  61 + if (tc) {
  62 + // create a new true color image:
61 63 cd = gdImageCreateTrueColor(_w,_h);
62   - else
  64 + // set alpha for PNGs:
  65 + gdImageAlphaBlending(cd, 0);
  66 + cd->saveAlphaFlag = 1;
  67 + transp = gdImageColorAllocateAlpha(cd, 255, 255, 255, 127);
  68 + }
  69 + else {
  70 + // create a new image:
63 71 cd = gdImageCreate(_w,_h);
  72 + // set transparency for GIFs:
  73 + transp = gdImageGetTransparent(imageImage(_img));
  74 + gdImageColorTransparent(cd, transp);
  75 + }
  76 + gdImageFilledRectangle(cd, 0, 0, _w, _h, transp);
64 77
65 78 //copy
66 79 if (_r)
11 src/nGDFile.c 100644 → 100755
@@ -27,19 +27,24 @@
27 27 FILE* openFileRead(value filename) {
28 28 val_check(filename, string);
29 29 char *_filename = val_string(filename);
30   - return fopen(_filename, "r");
  30 + FILE *_file = fopen(_filename, "r");
  31 + if (_file == NULL) failure("file not found / could not be loaded");
  32 + return _file;
31 33 }
32 34
33 35 FILE* openFileWrite(value filename) {
34 36 val_check(filename, string);
35 37 char *_filename = val_string(filename);
36   - return fopen(_filename, "wb");
  38 + FILE *_file = fopen(_filename, "wb");
  39 + if (_file == NULL) failure("file not found / could not be loaded");
  40 + return _file;
37 41 }
38 42
39 43 value ImageCreateFromJpeg(value filename) {
40 44 FILE *_file = openFileRead(filename);
41 45 gdImagePtr img = gdImageCreateFromJpeg(_file);
42 46 fclose(_file);
  47 + if (img == NULL) failure("image file could not be loaded: probably wrong format");
43 48 value ret = alloc_gc_image(img);
44 49 return ret;
45 50 }
@@ -48,6 +53,7 @@ value ImageCreateFromPng(value filename) {
48 53 FILE *_file = openFileRead(filename);
49 54 gdImagePtr img = gdImageCreateFromPng(_file);
50 55 fclose(_file);
  56 + if (img == NULL) failure("image file could not be loaded: probably wrong format");
51 57 return alloc_gc_image(img);
52 58 }
53 59
@@ -55,6 +61,7 @@ value ImageCreateFromGif(value filename) {
55 61 FILE *_file = openFileRead(filename);
56 62 gdImagePtr img = gdImageCreateFromGif(_file);
57 63 fclose(_file);
  64 + if (img == NULL) failure("image file could not be loaded: probably wrong format");
58 65 return alloc_gc_image(img);
59 66 }
60 67

0 comments on commit 6555998

Please sign in to comment.
Something went wrong with that request. Please try again.