Browse files

added transparency support to ImageResize (PNG and GIF)

(re)added "file not found" error handlers(cherry picked from commit 07b86e0)
  • Loading branch information...
1 parent 43ee0d3 commit 65559987b98ff129d7ceea212c43b52a0471a85c @mockey mockey committed with Jun 8, 2012
Showing with 25 additions and 5 deletions.
  1. +16 −3 src/nGDCopy.c
  2. +9 −2 src/nGDFile.c
View
19 src/nGDCopy.c 100644 → 100755
@@ -56,11 +56,24 @@ value ImageResize(value img, value w,value h,value resample) {
int _r = val_bool(resample);
gdImagePtr cd;
- //creating new one
- if (gdImageTrueColor(imageImage(_img)))
+ int tc = gdImageTrueColor(imageImage(_img));
+ int transp;
+ if (tc) {
+ // create a new true color image:
cd = gdImageCreateTrueColor(_w,_h);
- else
+ // set alpha for PNGs:
+ gdImageAlphaBlending(cd, 0);
+ cd->saveAlphaFlag = 1;
+ transp = gdImageColorAllocateAlpha(cd, 255, 255, 255, 127);
+ }
+ else {
+ // create a new image:
cd = gdImageCreate(_w,_h);
+ // set transparency for GIFs:
+ transp = gdImageGetTransparent(imageImage(_img));
+ gdImageColorTransparent(cd, transp);
+ }
+ gdImageFilledRectangle(cd, 0, 0, _w, _h, transp);
//copy
if (_r)
View
11 src/nGDFile.c 100644 → 100755
@@ -27,19 +27,24 @@
FILE* openFileRead(value filename) {
val_check(filename, string);
char *_filename = val_string(filename);
- return fopen(_filename, "r");
+ FILE *_file = fopen(_filename, "r");
+ if (_file == NULL) failure("file not found / could not be loaded");
+ return _file;
}
FILE* openFileWrite(value filename) {
val_check(filename, string);
char *_filename = val_string(filename);
- return fopen(_filename, "wb");
+ FILE *_file = fopen(_filename, "wb");
+ if (_file == NULL) failure("file not found / could not be loaded");
+ return _file;
}
value ImageCreateFromJpeg(value filename) {
FILE *_file = openFileRead(filename);
gdImagePtr img = gdImageCreateFromJpeg(_file);
fclose(_file);
+ if (img == NULL) failure("image file could not be loaded: probably wrong format");
value ret = alloc_gc_image(img);
return ret;
}
@@ -48,13 +53,15 @@ value ImageCreateFromPng(value filename) {
FILE *_file = openFileRead(filename);
gdImagePtr img = gdImageCreateFromPng(_file);
fclose(_file);
+ if (img == NULL) failure("image file could not be loaded: probably wrong format");
return alloc_gc_image(img);
}
value ImageCreateFromGif(value filename) {
FILE *_file = openFileRead(filename);
gdImagePtr img = gdImageCreateFromGif(_file);
fclose(_file);
+ if (img == NULL) failure("image file could not be loaded: probably wrong format");
return alloc_gc_image(img);
}

0 comments on commit 6555998

Please sign in to comment.