Skip to content

Commit

Permalink
fix black edge line issue when image is smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Jan 11, 2022
1 parent 675901e commit b9376ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Nginx module - https://github.com/niiknow/docker-nginx-image-proxy/blob/master/b

Original File - https://github.com/niiknow/docker-nginx-image-proxy/blob/master/build/src/http/modules/ngx_http_image_filter_module.c

Patch creation: `diff -u src/http/modules/ngx_http_image_filter_module.c ngx_http_image_filter_module.c > image_filter.patch`
Patch creation: `diff -u build/src/http/modules/ngx_http_image_filter_module.c build/ngx_http_image_filter_module.c > build/image_filter.patch`

Patch apply with: `patch src/http/modules/ngx_http_image_filter_module.c image_filter.patch`

Expand Down
53 changes: 35 additions & 18 deletions build/image_filter.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- src/http/modules/ngx_http_image_filter_module.c 2019-09-24 15:35:32.000000000 -0500
+++ ngx_http_image_filter_module.c 2019-09-24 15:38:46.000000000 -0500
--- build/src/http/modules/ngx_http_image_filter_module.c 2019-09-24 15:35:32.000000000 -0500
+++ build/ngx_http_image_filter_module.c 2022-01-11 12:32:00.000000000 -0600
@@ -10,7 +10,7 @@
#include <ngx_http.h>

Expand Down Expand Up @@ -324,7 +324,7 @@
if (!ctx->force
&& ctx->angle == 0
&& (ngx_uint_t) sx <= ctx->max_width
@@ -872,6 +1003,63 @@
@@ -872,6 +1003,65 @@

transparent:

Expand Down Expand Up @@ -376,6 +376,8 @@
+ }
+ }
+
+ // white background
+ // gdImageFill(dst, 0, 0, gdImageColorAllocate(dst, 255, 255, 255));
+ my_resize(src, dst);
+ // set the new original
+ gdImageDestroy(src);
Expand All @@ -388,7 +390,7 @@
gdImageColorTransparent(src, -1);

dx = sx;
@@ -901,7 +1089,23 @@
@@ -901,7 +1091,23 @@

resize = 0;

Expand All @@ -413,7 +415,18 @@
if ((ngx_uint_t) dx > ctx->max_width) {
dy = dy * ctx->max_width / dx;
dy = dy ? dy : 1;
@@ -989,7 +1193,9 @@
@@ -931,7 +1137,9 @@
gdImageAlphaBlending(dst, 0);
}

- gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy);
+ // gdImageFill(dst, 0, 0, gdImageColorAllocate(dst, 255, 255, 255));
+
+ gdImageCopyResampled(dst, src, 0, 0, 0, 0, ceil(dx), ceil(dy), sx, sy);

if (colors) {
gdImageTrueColorToPalette(dst, 1, 256);
@@ -989,7 +1197,9 @@
}
}

Expand All @@ -424,7 +437,7 @@

src = dst;

@@ -1016,8 +1222,24 @@
@@ -1016,8 +1226,24 @@
return NULL;
}

Expand All @@ -451,15 +464,15 @@

ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"image crop: %d x %d @ %d x %d",
@@ -1048,6 +1270,7 @@
@@ -1048,6 +1274,7 @@
}

gdImageInterlace(dst, (int) conf->interlace);
+ ngx_http_image_watermark(r, conf, dst);

out = ngx_http_image_out(r, ctx->type, dst, &size);

@@ -1122,6 +1345,20 @@
@@ -1122,6 +1349,20 @@
#endif
break;

Expand All @@ -480,7 +493,7 @@
default:
failed = "unknown image type";
break;
@@ -1171,14 +1408,37 @@
@@ -1171,14 +1412,37 @@
u_char *out;
ngx_int_t q;
ngx_http_image_filter_conf_t *conf;
Expand Down Expand Up @@ -520,7 +533,7 @@
q = ngx_http_image_filter_get_value(r, conf->jqcv, conf->jpeg_quality);
if (q <= 0) {
return NULL;
@@ -1195,13 +1455,11 @@
@@ -1195,13 +1459,11 @@

case NGX_HTTP_IMAGE_PNG:
out = gdImagePngPtr(img, size);
Expand All @@ -535,7 +548,7 @@
q = ngx_http_image_filter_get_value(r, conf->wqcv, conf->webp_quality);
if (q <= 0) {
return NULL;
@@ -1214,6 +1472,22 @@
@@ -1214,6 +1476,22 @@
#endif
break;

Expand All @@ -558,7 +571,7 @@
default:
failed = "unknown image type";
break;
@@ -1263,7 +1537,27 @@
@@ -1263,7 +1541,27 @@

n = ngx_atoi(value->data, value->len);

Expand Down Expand Up @@ -587,7 +600,7 @@
return (ngx_uint_t) n;
}

@@ -1299,9 +1593,13 @@
@@ -1299,9 +1597,13 @@
conf->jpeg_quality = NGX_CONF_UNSET_UINT;
conf->webp_quality = NGX_CONF_UNSET_UINT;
conf->sharpen = NGX_CONF_UNSET_UINT;
Expand All @@ -601,7 +614,7 @@

return conf;
}
@@ -1364,6 +1662,37 @@
@@ -1364,6 +1666,37 @@
ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
1 * 1024 * 1024);

Expand Down Expand Up @@ -639,7 +652,7 @@
return NGX_CONF_OK;
}

@@ -1662,6 +1991,66 @@
@@ -1662,6 +1995,66 @@
}


Expand Down Expand Up @@ -706,7 +719,7 @@
static ngx_int_t
ngx_http_image_filter_init(ngx_conf_t *cf)
{
@@ -1673,3 +2062,109 @@
@@ -1673,3 +2066,113 @@

return NGX_OK;
}
Expand Down Expand Up @@ -785,6 +798,10 @@
+ offset = (int)((requestedLength - calculatedLength) / 2);
+ }
+
+ if (offset < 0) {
+ offset = 0;
+ }
+
+ return (offset);
+}
+
Expand All @@ -797,10 +814,10 @@
+ int destinationY = destination->sy;
+
+ if (destinationRatio > originalRatio) {
+ destinationX = floor(destination->sy * originalRatio);
+ destinationX = ceil(destination->sy * originalRatio);
+ }
+ else {
+ destinationY = floor(destination->sx / originalRatio);
+ destinationY = ceil(destination->sx / originalRatio);
+ }
+
+ gdImageCopyResampled(
Expand Down
14 changes: 11 additions & 3 deletions build/ngx_http_image_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
}
}

// white background
// gdImageFill(dst, 0, 0, gdImageColorAllocate(dst, 255, 255, 255));
my_resize(src, dst);
// set the new original
gdImageDestroy(src);
Expand Down Expand Up @@ -1135,7 +1137,9 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
gdImageAlphaBlending(dst, 0);
}

gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy);
// gdImageFill(dst, 0, 0, gdImageColorAllocate(dst, 255, 255, 255));

gdImageCopyResampled(dst, src, 0, 0, 0, 0, ceil(dx), ceil(dy), sx, sy);

if (colors) {
gdImageTrueColorToPalette(dst, 1, 256);
Expand Down Expand Up @@ -2137,6 +2141,10 @@ static int my_offset(int calculatedLength, int requestedLength) {
offset = (int)((requestedLength - calculatedLength) / 2);
}

if (offset < 0) {
offset = 0;
}

return (offset);
}

Expand All @@ -2149,10 +2157,10 @@ static void my_resize (gdImagePtr original, gdImagePtr destination) {
int destinationY = destination->sy;

if (destinationRatio > originalRatio) {
destinationX = floor(destination->sy * originalRatio);
destinationX = ceil(destination->sy * originalRatio);
}
else {
destinationY = floor(destination->sx / originalRatio);
destinationY = ceil(destination->sx / originalRatio);
}

gdImageCopyResampled(
Expand Down
5 changes: 5 additions & 0 deletions files/root/bin/my-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ fi
echo "*** Running cron"
cron

# now=$(date +"%T")
# echo "Current time : $now"

ls -la /root/bin

echo "*** Running nginx"
exec /usr/sbin/nginx -g "daemon off;"

0 comments on commit b9376ec

Please sign in to comment.