Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

center image on canvas if source image size is smaller than destinati… #79

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ngx_http_small_light_gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,5 @@ ngx_int_t ngx_http_small_light_gd_process(ngx_http_request_t *r, ngx_http_small_
ictx->complete = 1;

return NGX_OK;

}
53 changes: 50 additions & 3 deletions src/ngx_http_small_light_imagemagick.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void ngx_http_small_light_imagemagick_term(void *data)
DestroyMagickWand(ictx->wand);
}

/**
/**
* following original functions are brought from
* mod_small_light(Dynamic image transformation module for Apache2) and customed
*/
Expand All @@ -100,7 +100,7 @@ ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_ht
int rmprof_flg, progressive_flg, cmyk2rgb_flg;
double iw, ih, q;
char *unsharp, *sharpen, *blur, *of, *of_orig;
MagickWand *trans_wand, *canvas_wand;
MagickWand *trans_wand, *canvas_wand, *canvas_bg_wand;
DrawingWand *border_wand;
PixelWand *bg_color, *canvas_color, *border_color;
GeometryInfo geo;
Expand All @@ -112,9 +112,10 @@ ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_ht
u_char jpeg_size_opt[32], crop_geo[128], size_geo[128], embedicon_path[256];
ColorspaceType color_space;
#if MagickLibVersion >= 0x690
int autoorient_flg;
int autoorient_flg, backgroundfill_flg;
#endif


status = MagickFalse;

ictx = (ngx_http_small_light_imagemagick_ctx_t *)ctx->ictx;
Expand Down Expand Up @@ -205,8 +206,27 @@ ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_ht
/* calc size. */
iw = (double)MagickGetImageWidth(ictx->wand);
ih = (double)MagickGetImageHeight(ictx->wand);

/* dpr adjustment */
if (sz.img_dpr > 1 && (iw < sz.dw || ih < sz.dh )) {
MagickAdaptiveResizeImage(ictx->wand, iw*sz.img_dpr, ih*sz.img_dpr);

sz.sw = iw * sz.img_dpr;
sz.sh = ih * sz.img_dpr;
sz.scale_flg = 1;

ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
"dpr info:iw=%f,ih=%f,sw=%f,sh=%f,resized source to %f x %f",
iw, ih, sz.sw, sz.sh, iw*sz.img_dpr, ih*sz.img_dpr);

}

/* calc size again */
iw = (double)MagickGetImageWidth(ictx->wand);
ih = (double)MagickGetImageHeight(ictx->wand);
ngx_http_small_light_calc_image_size(r, ctx, &sz, iw, ih);


/* adjust image offset automatically */
ngx_http_small_light_imagemagick_adjust_image_offset(r, ictx, &sz);

Expand All @@ -225,6 +245,8 @@ ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_ht
sz.dh = sz.sh;
}



/* crop, scale. */
if (sz.scale_flg != 0) {
p = ngx_snprintf(crop_geo, sizeof(crop_geo) - 1, "%f!x%f!+%f+%f", sz.sw, sz.sh, sz.sx, sz.sy);
Expand Down Expand Up @@ -278,6 +300,31 @@ ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_ht

ngx_http_small_light_adjust_canvas_image_offset(&sz);

// check background fill
backgroundfill_flg = ngx_http_small_light_parse_flag(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "backgroundfill"));
if (backgroundfill_flg == 1) {

// first trim whitespace off the original image
MagickTrimImage(ictx->wand, 1.0);

canvas_bg_wand = CloneMagickWand(ictx->wand);
MagickResizeImage(canvas_bg_wand, sz.cw/4, sz.ch/4, LanczosFilter, 1.0);
MagickGaussianBlurImage(canvas_bg_wand, 0, 1);
MagickResizeImage(canvas_bg_wand, sz.cw*2, sz.ch*2, LanczosFilter, 1.0);
MagickSetImageOpacity(canvas_bg_wand, 0.5);

status = MagickCompositeImageGravity(canvas_wand, canvas_bg_wand, AtopCompositeOp, CenterGravity);

if (status == MagickFalse) {
r->err_status = NGX_HTTP_INTERNAL_SERVER_ERROR;
DestroyMagickWand(canvas_wand);
DestroyString(of_orig);
return NGX_ERROR;
}
DestroyMagickWand(canvas_bg_wand);

}

status = MagickCompositeImage(canvas_wand, ictx->wand, AtopCompositeOp, sz.dx, sz.dy);
if (status == MagickFalse) {
r->err_status = NGX_HTTP_INTERNAL_SERVER_ERROR;
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_small_light_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ typedef struct {
ngx_http_small_light_color_t bc;
ngx_int_t ix;
ngx_int_t iy;
ngx_int_t img_dpr;
double aspect;
double fitfactor;
ngx_int_t pt_flg;
ngx_int_t scale_flg;
ngx_int_t jpeghint_flg;
Expand Down
9 changes: 7 additions & 2 deletions src/ngx_http_small_light_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ static const ngx_http_small_light_param_t ngx_http_small_light_params[] = {
{ ngx_string("progressive"), "n"},
{ ngx_string("cmyk2rgb"), "n"},
{ ngx_string("rmprof"), "n"},
{ ngx_string("autoorient"),"n"}
{ ngx_string("autoorient"),"n"},
{ ngx_string("dpr") ,"1"},
{ ngx_string("backgroundfill") ,"n"}
};

static const ngx_str_t ngx_http_small_light_getparams[] = {
Expand Down Expand Up @@ -100,7 +102,10 @@ static const ngx_str_t ngx_http_small_light_getparams[] = {
ngx_string("arg_progressive"),
ngx_string("arg_cmyk2rgb"),
ngx_string("arg_rmprof"),
ngx_string("arg_autoorient")
ngx_string("arg_autoorient"),
ngx_string("arg_dpr"),
ngx_string("arg_backgroundfill"),

};

static void ngx_http_small_light_init_params_default(ngx_http_small_light_ctx_t *ctx)
Expand Down
119 changes: 101 additions & 18 deletions src/ngx_http_small_light_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,43 @@
void ngx_http_small_light_adjust_canvas_image_offset(ngx_http_small_light_image_size_t *sz)
{
if (sz->dx == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
sz->dx = (sz->cw - sz->dw) * 0.5;

if (sz->sw < sz->dw) {
sz->dx = (sz->cw - sz->sw) * 0.5;
} else {
sz->dx = (sz->cw - sz->dw) * 0.5;
}

}
if (sz->dy == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
sz->dy = (sz->ch - sz->dh) * 0.5;

if (sz->sh < sz->dh) {
sz->dy = (sz->ch - sz->sh) * 0.5;
} else {
sz->dy = (sz->ch - sz->dh) * 0.5;
}

}



}

/**
/**
* following original functions are brought from
* mod_small_light(Dynamic image transformation module for Apache2) and customed
*/
*/

void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
ngx_http_small_light_ctx_t *ctx,
ngx_http_small_light_image_size_t *sz,
double iw, double ih)
{

ngx_http_small_light_coord_t sx_coord, sy_coord, sw_coord, sh_coord;
ngx_http_small_light_coord_t dx_coord, dy_coord, dw_coord, dh_coord;
char *da_str, *pt, *prm_ds_str, da, prm_ds;
ngx_int_t pt_flg;
ngx_int_t pt_flg, img_dpr;

ngx_http_small_light_parse_coord(&sx_coord, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "sx"));
ngx_http_small_light_parse_coord(&sy_coord, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "sy"));
Expand All @@ -59,6 +74,7 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
ngx_http_small_light_parse_coord(&dw_coord, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "dw"));
ngx_http_small_light_parse_coord(&dh_coord, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "dh"));


sz->sx = ngx_http_small_light_calc_coord(&sx_coord, iw);
if (sz->sx == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
sz->sx = 0;
Expand All @@ -81,6 +97,13 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
sz->dh = ngx_http_small_light_calc_coord(&dh_coord, ih);
sz->aspect = sz->sw / sz->sh;

sz->cw = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "cw"));
sz->ch = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "ch"));
sz->bw = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "bw"));
sz->bh = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "bh"));
sz->ix = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "ix"));
sz->iy = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "iy"));

da_str = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "da");
da = da_str[0] ? da_str[0] : 'l';
if (sz->dw != NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE && sz->dh != NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
Expand All @@ -96,7 +119,46 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
} else {
sz->dw = sz->dh * sz->aspect;
}
}
} else if (da == 'f') {
// scalefit

// portrait
if (sz->sw < sz->sh) {
sz->dh = sz->dh / sz->aspect;
sz->dw = sz->dw;
sz->dy = 0;
if (sz->dx > 0) {
sz->dx = 0;
}

// if canvas height > destination height :
if (sz->ch > sz->dh) {
sz->fitfactor = sz->ch/sz->dh;
sz->dh = sz->dh*sz->fitfactor;
sz->dw = sz->dw*sz->fitfactor;
}

// landscape
} else {
sz->dh = sz->dh;
sz->dw = sz->dw * sz->aspect;
if (sz->dx > 0) {
sz->dx = 0;
}
if (sz->dy > 0) {
sz->dy = 0;
}

// if canvas width > destination width :
if (sz->cw > sz->dw) {
sz->fitfactor = sz->cw/sz->dw;
sz->dh = sz->dh*sz->fitfactor;
sz->dw = sz->dw*sz->fitfactor;
}

}
}

} else {
if (sz->dw == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE && sz->dh == sz->dw) {
// do nothing
Expand All @@ -106,15 +168,10 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
sz->dh = sz->dw / sz->aspect;
}
}
sz->cw = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "cw"));
sz->ch = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "ch"));
sz->bw = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "bw"));
sz->bh = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "bh"));
sz->ix = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "ix"));
sz->iy = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "iy"));


ngx_http_small_light_parse_color(&sz->cc, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "cc"));
ngx_http_small_light_parse_color(&sz->bc, NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "bc"));

/* get pass through option. */
pt_flg = 0;
pt = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "pt");
Expand All @@ -129,6 +186,21 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
}
sz->pt_flg = pt_flg;

/* get dpr option. */
img_dpr = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "dpr"));
if (img_dpr != 2 && img_dpr != 3) {
img_dpr = 1;
}

sz->img_dpr = img_dpr;

/* dpr adjustments */
sz->cw = sz->cw * sz->img_dpr;
sz->ch = sz->ch * sz->img_dpr;
sz->dw = sz->dw * sz->img_dpr;
sz->dh = sz->dh * sz->img_dpr;


/* get scaling option. */
prm_ds_str = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "ds");
prm_ds = prm_ds_str[0];
Expand All @@ -138,19 +210,30 @@ void ngx_http_small_light_calc_image_size(ngx_http_request_t *r,
sz->scale_flg = 0;
}
if (sz->dw != NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE && sz->dx == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
sz->dx = (sz->cw - sz->dw) * 0.5;
if (sz->sw < sz->dw) {
sz->dx = (sz->cw - sz->sw) * 0.5;
} else {
sz->dx = (sz->cw - sz->dw) * 0.5;
}
}
if (sz->dh != NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE && sz->dy == NGX_HTTP_SMALL_LIGHT_COORD_INVALID_VALUE) {
sz->dy = (sz->ch - sz->dh) * 0.5;
if (sz->sh < sz->dh) {
sz->dy = (sz->ch - sz->sh) * 0.5;
} else {
sz->dy = (sz->ch - sz->dh) * 0.5;
}
}



sz->jpeghint_flg = ngx_http_small_light_parse_flag(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "jpeghint"));
sz->angle = ngx_http_small_light_parse_int(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "angle"));

ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
"size info:sx=%f,sy=%f,sw=%f,sh=%f,dw=%f,dh=%f,dx=%f,dy=%f,cw=%f,ch=%f,bw=%f,bh=%f,ix=%i,iy=%i",
"size info:sx=%f,sy=%f,sw=%f,sh=%f,dw=%f,dh=%f,dx=%f,dy=%f,cw=%f,ch=%f,bw=%f,bh=%f,ix=%i,iy=%i opts: dpr=%i,pt=%i",
sz->sx, sz->sy, sz->sw, sz->sh,
sz->dw, sz->dh, sz->dx, sz->dy,
sz->cw, sz->ch, sz->bw, sz->bh,
sz->ix, sz->iy);
sz->ix, sz->iy, sz->img_dpr, sz->pt_flg);

}