Skip to content

Instantly share code, notes, and snippets.

@mrosset
Created December 2, 2012 14:19
Show Gist options
  • Save mrosset/4188939 to your computer and use it in GitHub Desktop.
Save mrosset/4188939 to your computer and use it in GitHub Desktop.
#include "thumb.h"
gdImagePtr loadImage(char *path) {
FILE *fp;
gdImagePtr im;
fp = fopen(path, "rb");
if (!fp) {
assert_perror(errno);
}
im = gdImageCreateFromJpeg(fp);
fclose(fp);
return im;
}
void savePngImage(gdImagePtr im, char *path) {
FILE *fp;
fp = fopen(path, "wb");
if (!fp) {
assert_perror(errno);
}
gdImagePng(im, fp);
fclose(fp);
gdFree(im);
}
void thumbnail(char *path, char *tpath) {
gdImagePtr src = loadImage(path);
int w, h, tw = 100, th = 100;
w = gdImageSX(src);
h = gdImageSY(src);
if ( w > h ) {
th = 100 * h / w;
fflush(stdout);
}
else {
tw = 100 * w / h;
}
gdImagePtr dst = gdImageCreateTrueColor(tw, th);
gdImageCopyResampled(dst, src, 0, 0, 0, 0, tw, th, w, h);
savePngImage(dst, tpath);
gdFree(src);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment