Created
September 2, 2010 01:09
-
-
Save tyler/561666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- src/http/modules/ngx_http_image_filter_module_old.c 2010-09-01 17:02:17.000000000 -0700 | |
+++ src/http/modules/ngx_http_image_filter_module.c 2010-09-01 17:06:17.000000000 -0700 | |
@@ -16,6 +16,7 @@ | |
#define NGX_HTTP_IMAGE_SIZE 2 | |
#define NGX_HTTP_IMAGE_RESIZE 3 | |
#define NGX_HTTP_IMAGE_CROP 4 | |
+#define NGX_HTTP_IMAGE_BOX 5 | |
#define NGX_HTTP_IMAGE_START 0 | |
@@ -503,7 +504,8 @@ | |
if (rc == NGX_OK | |
&& ctx->width <= ctx->max_width | |
&& ctx->height <= ctx->max_height | |
- && !ctx->force) | |
+ && !ctx->force | |
+ && conf->filter != NGX_HTTP_IMAGE_BOX) | |
{ | |
return ngx_http_image_asis(r, ctx); | |
} | |
@@ -729,7 +731,8 @@ | |
if (!ctx->force | |
&& (ngx_uint_t) sx <= ctx->max_width | |
- && (ngx_uint_t) sy <= ctx->max_height) | |
+ && (ngx_uint_t) sy <= ctx->max_height | |
+ && conf->filter != NGX_HTTP_IMAGE_BOX) | |
{ | |
gdImageDestroy(src); | |
return ngx_http_image_asis(r, ctx); | |
@@ -763,7 +766,7 @@ | |
dx = sx; | |
dy = sy; | |
- if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
+ if (conf->filter == NGX_HTTP_IMAGE_RESIZE || conf->filter == NGX_HTTP_IMAGE_BOX) { | |
if ((ngx_uint_t) dx > ctx->max_width) { | |
dy = dy * ctx->max_width / dx; | |
@@ -874,6 +877,45 @@ | |
gdImageDestroy(src); | |
} | |
+ | |
+ } else if (conf->filter == NGX_HTTP_IMAGE_BOX) { | |
+ | |
+ src = dst; | |
+ | |
+ if ((ngx_uint_t) dx < ctx->max_width) { | |
+ ox = ctx->max_width - dx; | |
+ } else { | |
+ ox = 0; | |
+ } | |
+ | |
+ if ((ngx_uint_t) dy < ctx->max_height) { | |
+ oy = ctx->max_height - dy; | |
+ } else { | |
+ oy = 0; | |
+ } | |
+ | |
+ if(ox || oy) { | |
+ dst = ngx_http_image_new(r, dx + ox, dy + oy, colors); | |
+ | |
+ ox /= 2; // we'll center it | |
+ oy /= 2; | |
+ | |
+ if (colors == 0) { | |
+ gdImageSaveAlpha(dst, 1); | |
+ gdImageAlphaBlending(dst, 0); | |
+ } | |
+ | |
+ int white = gdImageColorAllocate(dst, 255, 255, 255); | |
+ gdImageFill(dst, 0, 0, white); | |
+ | |
+ gdImageCopy(dst, src, ox, oy, 0, 0, dx, dy); | |
+ | |
+ if (colors) { | |
+ gdImageTrueColorToPalette(dst, 1, 256); | |
+ } | |
+ | |
+ gdImageDestroy(src); | |
+ } | |
} | |
if (transparent != -1 && colors) { | |
@@ -1158,6 +1200,9 @@ | |
} else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
imcf->filter = NGX_HTTP_IMAGE_CROP; | |
+ } else if (ngx_strcmp(value[i].data, "box") == 0) { | |
+ imcf->filter = NGX_HTTP_IMAGE_BOX; | |
+ | |
} else { | |
goto failed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment