484
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
4 * Copyright (C) Nginx, Inc.
|
484
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
488
|
11
|
|
12 #include <gd.h>
|
484
|
13
|
|
14
|
|
15 #define NGX_HTTP_IMAGE_OFF 0
|
|
16 #define NGX_HTTP_IMAGE_TEST 1
|
|
17 #define NGX_HTTP_IMAGE_SIZE 2
|
|
18 #define NGX_HTTP_IMAGE_RESIZE 3
|
|
19 #define NGX_HTTP_IMAGE_CROP 4
|
618
|
20 #define NGX_HTTP_IMAGE_ROTATE 5
|
484
|
21
|
|
22
|
|
23 #define NGX_HTTP_IMAGE_START 0
|
|
24 #define NGX_HTTP_IMAGE_READ 1
|
|
25 #define NGX_HTTP_IMAGE_PROCESS 2
|
488
|
26 #define NGX_HTTP_IMAGE_PASS 3
|
|
27 #define NGX_HTTP_IMAGE_DONE 4
|
484
|
28
|
|
29
|
|
30 #define NGX_HTTP_IMAGE_NONE 0
|
|
31 #define NGX_HTTP_IMAGE_JPEG 1
|
|
32 #define NGX_HTTP_IMAGE_GIF 2
|
|
33 #define NGX_HTTP_IMAGE_PNG 3
|
|
34
|
|
35
|
|
36 #define NGX_HTTP_IMAGE_BUFFERED 0x08
|
|
37
|
|
38
|
|
39 typedef struct {
|
|
40 ngx_uint_t filter;
|
|
41 ngx_uint_t width;
|
|
42 ngx_uint_t height;
|
618
|
43 ngx_uint_t angle;
|
594
|
44 ngx_uint_t jpeg_quality;
|
642
|
45 ngx_uint_t sharpen;
|
484
|
46
|
528
|
47 ngx_flag_t transparency;
|
|
48
|
510
|
49 ngx_http_complex_value_t *wcv;
|
|
50 ngx_http_complex_value_t *hcv;
|
618
|
51 ngx_http_complex_value_t *acv;
|
594
|
52 ngx_http_complex_value_t *jqcv;
|
642
|
53 ngx_http_complex_value_t *shcv;
|
510
|
54
|
484
|
55 size_t buffer_size;
|
|
56 } ngx_http_image_filter_conf_t;
|
|
57
|
|
58
|
|
59 typedef struct {
|
|
60 u_char *image;
|
|
61 u_char *last;
|
|
62
|
|
63 size_t length;
|
|
64
|
|
65 ngx_uint_t width;
|
|
66 ngx_uint_t height;
|
510
|
67 ngx_uint_t max_width;
|
|
68 ngx_uint_t max_height;
|
618
|
69 ngx_uint_t angle;
|
510
|
70
|
484
|
71 ngx_uint_t phase;
|
|
72 ngx_uint_t type;
|
558
|
73 ngx_uint_t force;
|
484
|
74 } ngx_http_image_filter_ctx_t;
|
|
75
|
|
76
|
488
|
77 static ngx_int_t ngx_http_image_send(ngx_http_request_t *r,
|
|
78 ngx_http_image_filter_ctx_t *ctx, ngx_chain_t *in);
|
484
|
79 static ngx_uint_t ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in);
|
|
80 static ngx_int_t ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in);
|
|
81 static ngx_buf_t *ngx_http_image_process(ngx_http_request_t *r);
|
|
82 static ngx_buf_t *ngx_http_image_json(ngx_http_request_t *r,
|
|
83 ngx_http_image_filter_ctx_t *ctx);
|
|
84 static ngx_buf_t *ngx_http_image_asis(ngx_http_request_t *r,
|
|
85 ngx_http_image_filter_ctx_t *ctx);
|
|
86 static void ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b);
|
|
87 static ngx_int_t ngx_http_image_size(ngx_http_request_t *r,
|
|
88 ngx_http_image_filter_ctx_t *ctx);
|
|
89
|
|
90 static ngx_buf_t *ngx_http_image_resize(ngx_http_request_t *r,
|
|
91 ngx_http_image_filter_ctx_t *ctx);
|
|
92 static gdImagePtr ngx_http_image_source(ngx_http_request_t *r,
|
|
93 ngx_http_image_filter_ctx_t *ctx);
|
|
94 static gdImagePtr ngx_http_image_new(ngx_http_request_t *r, int w, int h,
|
|
95 int colors);
|
|
96 static u_char *ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type,
|
|
97 gdImagePtr img, int *size);
|
|
98 static void ngx_http_image_cleanup(void *data);
|
510
|
99 static ngx_uint_t ngx_http_image_filter_get_value(ngx_http_request_t *r,
|
|
100 ngx_http_complex_value_t *cv, ngx_uint_t v);
|
|
101 static ngx_uint_t ngx_http_image_filter_value(ngx_str_t *value);
|
484
|
102
|
|
103
|
|
104 static void *ngx_http_image_filter_create_conf(ngx_conf_t *cf);
|
|
105 static char *ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent,
|
|
106 void *child);
|
|
107 static char *ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
108 void *conf);
|
594
|
109 static char *ngx_http_image_filter_jpeg_quality(ngx_conf_t *cf,
|
|
110 ngx_command_t *cmd, void *conf);
|
642
|
111 static char *ngx_http_image_filter_sharpen(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
112 void *conf);
|
484
|
113 static ngx_int_t ngx_http_image_filter_init(ngx_conf_t *cf);
|
|
114
|
|
115
|
|
116 static ngx_command_t ngx_http_image_filter_commands[] = {
|
|
117
|
|
118 { ngx_string("image_filter"),
|
642
|
119 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
484
|
120 ngx_http_image_filter,
|
|
121 NGX_HTTP_LOC_CONF_OFFSET,
|
|
122 0,
|
|
123 NULL },
|
|
124
|
492
|
125 { ngx_string("image_filter_jpeg_quality"),
|
|
126 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
594
|
127 ngx_http_image_filter_jpeg_quality,
|
492
|
128 NGX_HTTP_LOC_CONF_OFFSET,
|
594
|
129 0,
|
492
|
130 NULL },
|
|
131
|
642
|
132 { ngx_string("image_filter_sharpen"),
|
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
134 ngx_http_image_filter_sharpen,
|
|
135 NGX_HTTP_LOC_CONF_OFFSET,
|
|
136 0,
|
|
137 NULL },
|
|
138
|
528
|
139 { ngx_string("image_filter_transparency"),
|
|
140 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
141 ngx_conf_set_flag_slot,
|
|
142 NGX_HTTP_LOC_CONF_OFFSET,
|
|
143 offsetof(ngx_http_image_filter_conf_t, transparency),
|
|
144 NULL },
|
|
145
|
484
|
146 { ngx_string("image_filter_buffer"),
|
|
147 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
148 ngx_conf_set_size_slot,
|
|
149 NGX_HTTP_LOC_CONF_OFFSET,
|
|
150 offsetof(ngx_http_image_filter_conf_t, buffer_size),
|
|
151 NULL },
|
|
152
|
|
153 ngx_null_command
|
|
154 };
|
|
155
|
|
156
|
|
157 static ngx_http_module_t ngx_http_image_filter_module_ctx = {
|
|
158 NULL, /* preconfiguration */
|
|
159 ngx_http_image_filter_init, /* postconfiguration */
|
|
160
|
|
161 NULL, /* create main configuration */
|
|
162 NULL, /* init main configuration */
|
|
163
|
|
164 NULL, /* create server configuration */
|
|
165 NULL, /* merge server configuration */
|
|
166
|
|
167 ngx_http_image_filter_create_conf, /* create location configuration */
|
|
168 ngx_http_image_filter_merge_conf /* merge location configuration */
|
|
169 };
|
|
170
|
|
171
|
|
172 ngx_module_t ngx_http_image_filter_module = {
|
|
173 NGX_MODULE_V1,
|
|
174 &ngx_http_image_filter_module_ctx, /* module context */
|
|
175 ngx_http_image_filter_commands, /* module directives */
|
|
176 NGX_HTTP_MODULE, /* module type */
|
|
177 NULL, /* init master */
|
|
178 NULL, /* init module */
|
|
179 NULL, /* init process */
|
|
180 NULL, /* init thread */
|
|
181 NULL, /* exit thread */
|
|
182 NULL, /* exit process */
|
|
183 NULL, /* exit master */
|
|
184 NGX_MODULE_V1_PADDING
|
|
185 };
|
|
186
|
|
187
|
|
188 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
189 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
190
|
|
191
|
|
192 static ngx_str_t ngx_http_image_types[] = {
|
|
193 ngx_string("image/jpeg"),
|
|
194 ngx_string("image/gif"),
|
|
195 ngx_string("image/png")
|
|
196 };
|
|
197
|
|
198
|
|
199 static ngx_int_t
|
|
200 ngx_http_image_header_filter(ngx_http_request_t *r)
|
|
201 {
|
|
202 off_t len;
|
|
203 ngx_http_image_filter_ctx_t *ctx;
|
|
204 ngx_http_image_filter_conf_t *conf;
|
|
205
|
|
206 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
|
|
207 return ngx_http_next_header_filter(r);
|
|
208 }
|
|
209
|
490
|
210 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module);
|
|
211
|
|
212 if (ctx) {
|
|
213 ngx_http_set_ctx(r, NULL, ngx_http_image_filter_module);
|
|
214 return ngx_http_next_header_filter(r);
|
|
215 }
|
|
216
|
484
|
217 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
|
|
218
|
|
219 if (conf->filter == NGX_HTTP_IMAGE_OFF) {
|
|
220 return ngx_http_next_header_filter(r);
|
|
221 }
|
|
222
|
|
223 if (r->headers_out.content_type.len
|
|
224 >= sizeof("multipart/x-mixed-replace") - 1
|
|
225 && ngx_strncasecmp(r->headers_out.content_type.data,
|
|
226 (u_char *) "multipart/x-mixed-replace",
|
|
227 sizeof("multipart/x-mixed-replace") - 1)
|
|
228 == 0)
|
|
229 {
|
|
230 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
231 "image filter: multipart/x-mixed-replace response");
|
|
232
|
|
233 return NGX_ERROR;
|
|
234 }
|
|
235
|
|
236 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_image_filter_ctx_t));
|
|
237 if (ctx == NULL) {
|
|
238 return NGX_ERROR;
|
|
239 }
|
|
240
|
|
241 ngx_http_set_ctx(r, ctx, ngx_http_image_filter_module);
|
|
242
|
|
243 len = r->headers_out.content_length_n;
|
|
244
|
486
|
245 if (len != -1 && len > (off_t) conf->buffer_size) {
|
484
|
246 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
247 "image filter: too big response: %O", len);
|
|
248
|
592
|
249 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
|
484
|
250 }
|
|
251
|
|
252 if (len == -1) {
|
|
253 ctx->length = conf->buffer_size;
|
|
254
|
|
255 } else {
|
|
256 ctx->length = (size_t) len;
|
|
257 }
|
|
258
|
|
259 if (r->headers_out.refresh) {
|
|
260 r->headers_out.refresh->hash = 0;
|
|
261 }
|
|
262
|
|
263 r->main_filter_need_in_memory = 1;
|
|
264 r->allow_ranges = 0;
|
|
265
|
|
266 return NGX_OK;
|
|
267 }
|
|
268
|
|
269
|
|
270 static ngx_int_t
|
|
271 ngx_http_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
272 {
|
|
273 ngx_int_t rc;
|
|
274 ngx_str_t *ct;
|
|
275 ngx_chain_t out;
|
|
276 ngx_http_image_filter_ctx_t *ctx;
|
|
277 ngx_http_image_filter_conf_t *conf;
|
|
278
|
|
279 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "image filter");
|
|
280
|
|
281 if (in == NULL) {
|
|
282 return ngx_http_next_body_filter(r, in);
|
|
283 }
|
|
284
|
|
285 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module);
|
|
286
|
|
287 if (ctx == NULL) {
|
|
288 return ngx_http_next_body_filter(r, in);
|
|
289 }
|
|
290
|
|
291 switch (ctx->phase) {
|
|
292
|
|
293 case NGX_HTTP_IMAGE_START:
|
|
294
|
|
295 ctx->type = ngx_http_image_test(r, in);
|
|
296
|
|
297 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
|
|
298
|
|
299 if (ctx->type == NGX_HTTP_IMAGE_NONE) {
|
|
300
|
|
301 if (conf->filter == NGX_HTTP_IMAGE_SIZE) {
|
|
302 out.buf = ngx_http_image_json(r, NULL);
|
|
303
|
|
304 if (out.buf) {
|
|
305 out.next = NULL;
|
488
|
306 ctx->phase = NGX_HTTP_IMAGE_DONE;
|
484
|
307
|
488
|
308 return ngx_http_image_send(r, ctx, &out);
|
484
|
309 }
|
|
310 }
|
|
311
|
|
312 return ngx_http_filter_finalize_request(r,
|
488
|
313 &ngx_http_image_filter_module,
|
484
|
314 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE);
|
|
315 }
|
|
316
|
|
317 /* override content type */
|
|
318
|
|
319 ct = &ngx_http_image_types[ctx->type - 1];
|
|
320 r->headers_out.content_type_len = ct->len;
|
|
321 r->headers_out.content_type = *ct;
|
494
|
322 r->headers_out.content_type_lowcase = NULL;
|
484
|
323
|
|
324 if (conf->filter == NGX_HTTP_IMAGE_TEST) {
|
488
|
325 ctx->phase = NGX_HTTP_IMAGE_PASS;
|
|
326
|
|
327 return ngx_http_image_send(r, ctx, in);
|
484
|
328 }
|
|
329
|
|
330 ctx->phase = NGX_HTTP_IMAGE_READ;
|
|
331
|
|
332 /* fall through */
|
|
333
|
|
334 case NGX_HTTP_IMAGE_READ:
|
|
335
|
|
336 rc = ngx_http_image_read(r, in);
|
|
337
|
|
338 if (rc == NGX_AGAIN) {
|
|
339 return NGX_OK;
|
|
340 }
|
|
341
|
|
342 if (rc == NGX_ERROR) {
|
|
343 return ngx_http_filter_finalize_request(r,
|
488
|
344 &ngx_http_image_filter_module,
|
484
|
345 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE);
|
|
346 }
|
|
347
|
|
348 /* fall through */
|
|
349
|
|
350 case NGX_HTTP_IMAGE_PROCESS:
|
|
351
|
|
352 out.buf = ngx_http_image_process(r);
|
|
353
|
|
354 if (out.buf == NULL) {
|
|
355 return ngx_http_filter_finalize_request(r,
|
488
|
356 &ngx_http_image_filter_module,
|
484
|
357 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE);
|
|
358 }
|
|
359
|
|
360 out.next = NULL;
|
488
|
361 ctx->phase = NGX_HTTP_IMAGE_PASS;
|
|
362
|
|
363 return ngx_http_image_send(r, ctx, &out);
|
484
|
364
|
488
|
365 case NGX_HTTP_IMAGE_PASS:
|
|
366
|
|
367 return ngx_http_next_body_filter(r, in);
|
484
|
368
|
|
369 default: /* NGX_HTTP_IMAGE_DONE */
|
|
370
|
488
|
371 rc = ngx_http_next_body_filter(r, NULL);
|
|
372
|
|
373 /* NGX_ERROR resets any pending data */
|
|
374 return (rc == NGX_OK) ? NGX_ERROR : rc;
|
484
|
375 }
|
488
|
376 }
|
484
|
377
|
488
|
378
|
|
379 static ngx_int_t
|
|
380 ngx_http_image_send(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx,
|
|
381 ngx_chain_t *in)
|
|
382 {
|
|
383 ngx_int_t rc;
|
484
|
384
|
|
385 rc = ngx_http_next_header_filter(r);
|
|
386
|
|
387 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
488
|
388 return NGX_ERROR;
|
484
|
389 }
|
|
390
|
488
|
391 rc = ngx_http_next_body_filter(r, in);
|
|
392
|
|
393 if (ctx->phase == NGX_HTTP_IMAGE_DONE) {
|
|
394 /* NGX_ERROR resets any pending data */
|
|
395 return (rc == NGX_OK) ? NGX_ERROR : rc;
|
|
396 }
|
|
397
|
|
398 return rc;
|
484
|
399 }
|
|
400
|
|
401
|
|
402 static ngx_uint_t
|
|
403 ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in)
|
|
404 {
|
|
405 u_char *p;
|
|
406
|
|
407 p = in->buf->pos;
|
|
408
|
|
409 if (in->buf->last - p < 16) {
|
|
410 return NGX_HTTP_IMAGE_NONE;
|
|
411 }
|
|
412
|
|
413 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
414 "image filter: \"%c%c\"", p[0], p[1]);
|
|
415
|
|
416 if (p[0] == 0xff && p[1] == 0xd8) {
|
|
417
|
|
418 /* JPEG */
|
|
419
|
|
420 return NGX_HTTP_IMAGE_JPEG;
|
|
421
|
|
422 } else if (p[0] == 'G' && p[1] == 'I' && p[2] == 'F' && p[3] == '8'
|
498
|
423 && p[5] == 'a')
|
484
|
424 {
|
498
|
425 if (p[4] == '9' || p[4] == '7') {
|
|
426 /* GIF */
|
|
427 return NGX_HTTP_IMAGE_GIF;
|
|
428 }
|
484
|
429
|
|
430 } else if (p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' && p[3] == 'G'
|
|
431 && p[4] == 0x0d && p[5] == 0x0a && p[6] == 0x1a && p[7] == 0x0a)
|
|
432 {
|
|
433 /* PNG */
|
|
434
|
|
435 return NGX_HTTP_IMAGE_PNG;
|
|
436 }
|
|
437
|
|
438 return NGX_HTTP_IMAGE_NONE;
|
|
439 }
|
|
440
|
|
441
|
|
442 static ngx_int_t
|
|
443 ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in)
|
|
444 {
|
|
445 u_char *p;
|
|
446 size_t size, rest;
|
|
447 ngx_buf_t *b;
|
|
448 ngx_chain_t *cl;
|
|
449 ngx_http_image_filter_ctx_t *ctx;
|
|
450
|
|
451 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module);
|
|
452
|
|
453 if (ctx->image == NULL) {
|
|
454 ctx->image = ngx_palloc(r->pool, ctx->length);
|
|
455 if (ctx->image == NULL) {
|
|
456 return NGX_ERROR;
|
|
457 }
|
|
458
|
|
459 ctx->last = ctx->image;
|
|
460 }
|
|
461
|
|
462 p = ctx->last;
|
|
463
|
|
464 for (cl = in; cl; cl = cl->next) {
|
|
465
|
|
466 b = cl->buf;
|
|
467 size = b->last - b->pos;
|
|
468
|
|
469 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
470 "image buf: %uz", size);
|
|
471
|
|
472 rest = ctx->image + ctx->length - p;
|
|
473 size = (rest < size) ? rest : size;
|
|
474
|
|
475 p = ngx_cpymem(p, b->pos, size);
|
|
476 b->pos += size;
|
|
477
|
|
478 if (b->last_buf) {
|
|
479 ctx->last = p;
|
|
480 return NGX_OK;
|
|
481 }
|
|
482 }
|
|
483
|
|
484 ctx->last = p;
|
|
485 r->connection->buffered |= NGX_HTTP_IMAGE_BUFFERED;
|
|
486
|
|
487 return NGX_AGAIN;
|
|
488 }
|
|
489
|
|
490
|
|
491 static ngx_buf_t *
|
|
492 ngx_http_image_process(ngx_http_request_t *r)
|
|
493 {
|
|
494 ngx_int_t rc;
|
|
495 ngx_http_image_filter_ctx_t *ctx;
|
|
496 ngx_http_image_filter_conf_t *conf;
|
|
497
|
|
498 r->connection->buffered &= ~NGX_HTTP_IMAGE_BUFFERED;
|
|
499
|
|
500 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module);
|
|
501
|
|
502 rc = ngx_http_image_size(r, ctx);
|
|
503
|
|
504 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
|
|
505
|
|
506 if (conf->filter == NGX_HTTP_IMAGE_SIZE) {
|
510
|
507 return ngx_http_image_json(r, rc == NGX_OK ? ctx : NULL);
|
|
508 }
|
484
|
509
|
618
|
510 ctx->angle = ngx_http_image_filter_get_value(r, conf->acv, conf->angle);
|
|
511
|
|
512 if (conf->filter == NGX_HTTP_IMAGE_ROTATE) {
|
|
513
|
|
514 if (ctx->angle != 90 && ctx->angle != 180 && ctx->angle != 270) {
|
|
515 return NULL;
|
|
516 }
|
|
517
|
|
518 return ngx_http_image_resize(r, ctx);
|
|
519 }
|
|
520
|
510
|
521 ctx->max_width = ngx_http_image_filter_get_value(r, conf->wcv, conf->width);
|
|
522 if (ctx->max_width == 0) {
|
|
523 return NULL;
|
484
|
524 }
|
|
525
|
510
|
526 ctx->max_height = ngx_http_image_filter_get_value(r, conf->hcv,
|
|
527 conf->height);
|
|
528 if (ctx->max_height == 0) {
|
|
529 return NULL;
|
|
530 }
|
|
531
|
|
532 if (rc == NGX_OK
|
|
533 && ctx->width <= ctx->max_width
|
558
|
534 && ctx->height <= ctx->max_height
|
618
|
535 && ctx->angle == 0
|
558
|
536 && !ctx->force)
|
510
|
537 {
|
|
538 return ngx_http_image_asis(r, ctx);
|
|
539 }
|
|
540
|
|
541 return ngx_http_image_resize(r, ctx);
|
484
|
542 }
|
|
543
|
|
544
|
|
545 static ngx_buf_t *
|
|
546 ngx_http_image_json(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
|
|
547 {
|
|
548 size_t len;
|
|
549 ngx_buf_t *b;
|
|
550
|
|
551 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
552 if (b == NULL) {
|
|
553 return NULL;
|
|
554 }
|
|
555
|
|
556 b->memory = 1;
|
|
557 b->last_buf = 1;
|
|
558
|
|
559 ngx_http_clean_header(r);
|
|
560
|
|
561 r->headers_out.status = NGX_HTTP_OK;
|
570
|
562 ngx_str_set(&r->headers_out.content_type, "text/plain");
|
494
|
563 r->headers_out.content_type_lowcase = NULL;
|
484
|
564
|
|
565 if (ctx == NULL) {
|
|
566 b->pos = (u_char *) "{}" CRLF;
|
|
567 b->last = b->pos + sizeof("{}" CRLF) - 1;
|
|
568
|
|
569 ngx_http_image_length(r, b);
|
|
570
|
|
571 return b;
|
|
572 }
|
|
573
|
|
574 len = sizeof("{ \"img\" : "
|
|
575 "{ \"width\": , \"height\": , \"type\": \"jpeg\" } }" CRLF) - 1
|
|
576 + 2 * NGX_SIZE_T_LEN;
|
|
577
|
|
578 b->pos = ngx_pnalloc(r->pool, len);
|
|
579 if (b->pos == NULL) {
|
|
580 return NULL;
|
|
581 }
|
|
582
|
|
583 b->last = ngx_sprintf(b->pos,
|
|
584 "{ \"img\" : "
|
|
585 "{ \"width\": %uz,"
|
|
586 " \"height\": %uz,"
|
|
587 " \"type\": \"%s\" } }" CRLF,
|
|
588 ctx->width, ctx->height,
|
|
589 ngx_http_image_types[ctx->type - 1].data + 6);
|
|
590
|
|
591 ngx_http_image_length(r, b);
|
|
592
|
|
593 return b;
|
|
594 }
|
|
595
|
|
596
|
|
597 static ngx_buf_t *
|
|
598 ngx_http_image_asis(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
|
|
599 {
|
|
600 ngx_buf_t *b;
|
|
601
|
|
602 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
603 if (b == NULL) {
|
|
604 return NULL;
|
|
605 }
|
|
606
|
|
607 b->pos = ctx->image;
|
|
608 b->last = ctx->last;
|
|
609 b->memory = 1;
|
|
610 b->last_buf = 1;
|
|
611
|
|
612 ngx_http_image_length(r, b);
|
|
613
|
|
614 return b;
|
|
615 }
|
|
616
|
|
617
|
|
618 static void
|
|
619 ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b)
|
|
620 {
|
|
621 r->headers_out.content_length_n = b->last - b->pos;
|
|
622
|
|
623 if (r->headers_out.content_length) {
|
|
624 r->headers_out.content_length->hash = 0;
|
|
625 }
|
|
626
|
|
627 r->headers_out.content_length = NULL;
|
|
628 }
|
|
629
|
|
630
|
|
631 static ngx_int_t
|
|
632 ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
|
|
633 {
|
|
634 u_char *p, *last;
|
558
|
635 size_t len, app;
|
484
|
636 ngx_uint_t width, height;
|
|
637
|
|
638 p = ctx->image;
|
|
639
|
|
640 switch (ctx->type) {
|
|
641
|
|
642 case NGX_HTTP_IMAGE_JPEG:
|
|
643
|
|
644 p += 2;
|
|
645 last = ctx->image + ctx->length - 10;
|
558
|
646 width = 0;
|
|
647 height = 0;
|
|
648 app = 0;
|
484
|
649
|
|
650 while (p < last) {
|
|
651
|
|
652 if (p[0] == 0xff && p[1] != 0xff) {
|
|
653
|
|
654 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
558
|
655 "JPEG: %02xd %02xd", p[0], p[1]);
|
484
|
656
|
|
657 p++;
|
|
658
|
558
|
659 if ((*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3
|
|
660 || *p == 0xc9 || *p == 0xca || *p == 0xcb)
|
|
661 && (width == 0 || height == 0))
|
484
|
662 {
|
558
|
663 width = p[6] * 256 + p[7];
|
|
664 height = p[4] * 256 + p[5];
|
484
|
665 }
|
|
666
|
|
667 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
668 "JPEG: %02xd %02xd", p[1], p[2]);
|
|
669
|
558
|
670 len = p[1] * 256 + p[2];
|
|
671
|
|
672 if (*p >= 0xe1 && *p <= 0xef) {
|
|
673 /* application data, e.g., EXIF, Adobe XMP, etc. */
|
|
674 app += len;
|
|
675 }
|
|
676
|
|
677 p += len;
|
484
|
678
|
|
679 continue;
|
|
680 }
|
|
681
|
|
682 p++;
|
|
683 }
|
|
684
|
558
|
685 if (width == 0 || height == 0) {
|
|
686 return NGX_DECLINED;
|
|
687 }
|
484
|
688
|
558
|
689 if (ctx->length / 20 < app) {
|
|
690 /* force conversion if application data consume more than 5% */
|
|
691 ctx->force = 1;
|
|
692 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
693 "app data size: %uz", app);
|
|
694 }
|
484
|
695
|
|
696 break;
|
|
697
|
|
698 case NGX_HTTP_IMAGE_GIF:
|
|
699
|
|
700 if (ctx->length < 10) {
|
|
701 return NGX_DECLINED;
|
|
702 }
|
|
703
|
|
704 width = p[7] * 256 + p[6];
|
|
705 height = p[9] * 256 + p[8];
|
|
706
|
|
707 break;
|
|
708
|
|
709 case NGX_HTTP_IMAGE_PNG:
|
|
710
|
|
711 if (ctx->length < 24) {
|
|
712 return NGX_DECLINED;
|
|
713 }
|
|
714
|
|
715 width = p[18] * 256 + p[19];
|
|
716 height = p[22] * 256 + p[23];
|
|
717
|
|
718 break;
|
|
719
|
|
720 default:
|
|
721
|
|
722 return NGX_DECLINED;
|
|
723 }
|
|
724
|
|
725 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
726 "image size: %d x %d", width, height);
|
|
727
|
|
728 ctx->width = width;
|
|
729 ctx->height = height;
|
|
730
|
|
731 return NGX_OK;
|
|
732 }
|
|
733
|
|
734
|
|
735 static ngx_buf_t *
|
|
736 ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
|
|
737 {
|
624
|
738 int sx, sy, dx, dy, ox, oy, ax, ay, size,
|
642
|
739 colors, palette, transparent, sharpen,
|
618
|
740 red, green, blue, t;
|
484
|
741 u_char *out;
|
|
742 ngx_buf_t *b;
|
|
743 ngx_uint_t resize;
|
|
744 gdImagePtr src, dst;
|
|
745 ngx_pool_cleanup_t *cln;
|
|
746 ngx_http_image_filter_conf_t *conf;
|
|
747
|
|
748 src = ngx_http_image_source(r, ctx);
|
|
749
|
|
750 if (src == NULL) {
|
|
751 return NULL;
|
|
752 }
|
|
753
|
|
754 sx = gdImageSX(src);
|
|
755 sy = gdImageSY(src);
|
|
756
|
|
757 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
|
|
758
|
558
|
759 if (!ctx->force
|
618
|
760 && ctx->angle == 0
|
558
|
761 && (ngx_uint_t) sx <= ctx->max_width
|
510
|
762 && (ngx_uint_t) sy <= ctx->max_height)
|
|
763 {
|
484
|
764 gdImageDestroy(src);
|
|
765 return ngx_http_image_asis(r, ctx);
|
|
766 }
|
|
767
|
|
768 colors = gdImageColorsTotal(src);
|
528
|
769
|
|
770 if (colors && conf->transparency) {
|
|
771 transparent = gdImageGetTransparent(src);
|
|
772
|
|
773 if (transparent != -1) {
|
|
774 palette = colors;
|
|
775 red = gdImageRed(src, transparent);
|
|
776 green = gdImageGreen(src, transparent);
|
|
777 blue = gdImageBlue(src, transparent);
|
484
|
778
|
528
|
779 goto transparent;
|
|
780 }
|
|
781 }
|
526
|
782
|
528
|
783 palette = 0;
|
|
784 transparent = -1;
|
|
785 red = 0;
|
|
786 green = 0;
|
|
787 blue = 0;
|
|
788
|
|
789 transparent:
|
|
790
|
|
791 gdImageColorTransparent(src, -1);
|
526
|
792
|
484
|
793 dx = sx;
|
|
794 dy = sy;
|
|
795
|
|
796 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) {
|
|
797
|
510
|
798 if ((ngx_uint_t) dx > ctx->max_width) {
|
|
799 dy = dy * ctx->max_width / dx;
|
484
|
800 dy = dy ? dy : 1;
|
510
|
801 dx = ctx->max_width;
|
484
|
802 }
|
|
803
|
510
|
804 if ((ngx_uint_t) dy > ctx->max_height) {
|
|
805 dx = dx * ctx->max_height / dy;
|
484
|
806 dx = dx ? dx : 1;
|
510
|
807 dy = ctx->max_height;
|
484
|
808 }
|
|
809
|
|
810 resize = 1;
|
|
811
|
618
|
812 } else if (conf->filter == NGX_HTTP_IMAGE_ROTATE) {
|
|
813
|
|
814 resize = 0;
|
|
815
|
484
|
816 } else { /* NGX_HTTP_IMAGE_CROP */
|
|
817
|
|
818 resize = 0;
|
|
819
|
510
|
820 if ((ngx_uint_t) (dx * 100 / dy)
|
|
821 < ctx->max_width * 100 / ctx->max_height)
|
|
822 {
|
|
823 if ((ngx_uint_t) dx > ctx->max_width) {
|
|
824 dy = dy * ctx->max_width / dx;
|
484
|
825 dy = dy ? dy : 1;
|
510
|
826 dx = ctx->max_width;
|
484
|
827 resize = 1;
|
|
828 }
|
|
829
|
|
830 } else {
|
510
|
831 if ((ngx_uint_t) dy > ctx->max_height) {
|
|
832 dx = dx * ctx->max_height / dy;
|
484
|
833 dx = dx ? dx : 1;
|
510
|
834 dy = ctx->max_height;
|
484
|
835 resize = 1;
|
|
836 }
|
|
837 }
|
|
838 }
|
|
839
|
|
840 if (resize) {
|
528
|
841 dst = ngx_http_image_new(r, dx, dy, palette);
|
484
|
842 if (dst == NULL) {
|
|
843 gdImageDestroy(src);
|
|
844 return NULL;
|
|
845 }
|
|
846
|
528
|
847 if (colors == 0) {
|
|
848 gdImageSaveAlpha(dst, 1);
|
|
849 gdImageAlphaBlending(dst, 0);
|
|
850 }
|
|
851
|
484
|
852 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy);
|
|
853
|
528
|
854 if (colors) {
|
|
855 gdImageTrueColorToPalette(dst, 1, 256);
|
|
856 }
|
|
857
|
484
|
858 gdImageDestroy(src);
|
|
859
|
|
860 } else {
|
|
861 dst = src;
|
|
862 }
|
|
863
|
618
|
864 if (ctx->angle) {
|
|
865 src = dst;
|
|
866
|
624
|
867 ax = (dx % 2 == 0) ? 1 : 0;
|
|
868 ay = (dy % 2 == 0) ? 1 : 0;
|
|
869
|
618
|
870 switch (ctx->angle) {
|
|
871
|
|
872 case 90:
|
|
873 case 270:
|
|
874 dst = ngx_http_image_new(r, dy, dx, palette);
|
|
875 if (dst == NULL) {
|
|
876 gdImageDestroy(src);
|
|
877 return NULL;
|
|
878 }
|
624
|
879 if (ctx->angle == 90) {
|
|
880 ox = dy / 2 + ay;
|
|
881 oy = dx / 2 - ax;
|
|
882
|
|
883 } else {
|
|
884 ox = dy / 2 - ay;
|
|
885 oy = dx / 2 + ax;
|
|
886 }
|
|
887
|
|
888 gdImageCopyRotated(dst, src, ox, oy, 0, 0,
|
|
889 dx + ax, dy + ay, ctx->angle);
|
618
|
890 gdImageDestroy(src);
|
620
|
891
|
|
892 t = dx;
|
|
893 dx = dy;
|
|
894 dy = t;
|
618
|
895 break;
|
|
896
|
|
897 case 180:
|
|
898 dst = ngx_http_image_new(r, dx, dy, palette);
|
|
899 if (dst == NULL) {
|
|
900 gdImageDestroy(src);
|
|
901 return NULL;
|
|
902 }
|
624
|
903 gdImageCopyRotated(dst, src, dx / 2 - ax, dy / 2 - ay, 0, 0,
|
|
904 dx + ax, dy + ay, ctx->angle);
|
618
|
905 gdImageDestroy(src);
|
|
906 break;
|
|
907 }
|
|
908 }
|
|
909
|
484
|
910 if (conf->filter == NGX_HTTP_IMAGE_CROP) {
|
|
911
|
|
912 src = dst;
|
|
913
|
510
|
914 if ((ngx_uint_t) dx > ctx->max_width) {
|
|
915 ox = dx - ctx->max_width;
|
484
|
916
|
|
917 } else {
|
|
918 ox = 0;
|
|
919 }
|
|
920
|
510
|
921 if ((ngx_uint_t) dy > ctx->max_height) {
|
|
922 oy = dy - ctx->max_height;
|
484
|
923
|
|
924 } else {
|
|
925 oy = 0;
|
|
926 }
|
|
927
|
|
928 if (ox || oy) {
|
|
929
|
|
930 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors);
|
|
931
|
|
932 if (dst == NULL) {
|
|
933 gdImageDestroy(src);
|
|
934 return NULL;
|
|
935 }
|
|
936
|
|
937 ox /= 2;
|
|
938 oy /= 2;
|
|
939
|
|
940 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
941 "image crop: %d x %d @ %d x %d",
|
|
942 dx, dy, ox, oy);
|
|
943
|
528
|
944 if (colors == 0) {
|
|
945 gdImageSaveAlpha(dst, 1);
|
|
946 gdImageAlphaBlending(dst, 0);
|
|
947 }
|
|
948
|
484
|
949 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy);
|
|
950
|
528
|
951 if (colors) {
|
|
952 gdImageTrueColorToPalette(dst, 1, 256);
|
|
953 }
|
|
954
|
484
|
955 gdImageDestroy(src);
|
|
956 }
|
|
957 }
|
|
958
|
526
|
959 if (transparent != -1 && colors) {
|
|
960 gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue));
|
|
961 }
|
484
|
962
|
642
|
963 sharpen = ngx_http_image_filter_get_value(r, conf->shcv, conf->sharpen);
|
|
964 if (sharpen > 0) {
|
|
965 gdImageSharpen(dst, sharpen);
|
|
966 }
|
|
967
|
484
|
968 out = ngx_http_image_out(r, ctx->type, dst, &size);
|
|
969
|
|
970 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
971 "image: %d x %d %d", sx, sy, colors);
|
|
972
|
|
973 gdImageDestroy(dst);
|
|
974 ngx_pfree(r->pool, ctx->image);
|
|
975
|
|
976 if (out == NULL) {
|
|
977 return NULL;
|
|
978 }
|
|
979
|
|
980 cln = ngx_pool_cleanup_add(r->pool, 0);
|
|
981 if (cln == NULL) {
|
|
982 gdFree(out);
|
|
983 return NULL;
|
|
984 }
|
|
985
|
|
986 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
987 if (b == NULL) {
|
|
988 gdFree(out);
|
|
989 return NULL;
|
|
990 }
|
|
991
|
|
992 cln->handler = ngx_http_image_cleanup;
|
|
993 cln->data = out;
|
|
994
|
|
995 b->pos = out;
|
|
996 b->last = out + size;
|
|
997 b->memory = 1;
|
|
998 b->last_buf = 1;
|
|
999
|
|
1000 ngx_http_image_length(r, b);
|
|
1001
|
|
1002 return b;
|
|
1003 }
|
|
1004
|
|
1005
|
|
1006 static gdImagePtr
|
|
1007 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
|
|
1008 {
|
|
1009 char *failed;
|
|
1010 gdImagePtr img;
|
|
1011
|
|
1012 img = NULL;
|
|
1013
|
|
1014 switch (ctx->type) {
|
|
1015
|
|
1016 case NGX_HTTP_IMAGE_JPEG:
|
|
1017 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image);
|
|
1018 failed = "gdImageCreateFromJpegPtr() failed";
|
|
1019 break;
|
|
1020
|
|
1021 case NGX_HTTP_IMAGE_GIF:
|
|
1022 img = gdImageCreateFromGifPtr(ctx->length, ctx->image);
|
|
1023 failed = "gdImageCreateFromGifPtr() failed";
|
|
1024 break;
|
|
1025
|
|
1026 case NGX_HTTP_IMAGE_PNG:
|
|
1027 img = gdImageCreateFromPngPtr(ctx->length, ctx->image);
|
|
1028 failed = "gdImageCreateFromPngPtr() failed";
|
|
1029 break;
|
|
1030
|
|
1031 default:
|
|
1032 failed = "unknown image type";
|
|
1033 break;
|
|
1034 }
|
|
1035
|
|
1036 if (img == NULL) {
|
|
1037 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed);
|
|
1038 }
|
|
1039
|
|
1040 return img;
|
|
1041 }
|
|
1042
|
|
1043
|
|
1044 static gdImagePtr
|
|
1045 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors)
|
|
1046 {
|
|
1047 gdImagePtr img;
|
|
1048
|
|
1049 if (colors == 0) {
|
|
1050 img = gdImageCreateTrueColor(w, h);
|
|
1051
|
|
1052 if (img == NULL) {
|
|
1053 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1054 "gdImageCreateTrueColor() failed");
|
|
1055 return NULL;
|
|
1056 }
|
|
1057
|
|
1058 } else {
|
|
1059 img = gdImageCreate(w, h);
|
|
1060
|
|
1061 if (img == NULL) {
|
|
1062 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1063 "gdImageCreate() failed");
|
|
1064 return NULL;
|
|
1065 }
|
|
1066 }
|
|
1067
|
|
1068 return img;
|
|
1069 }
|
|
1070
|
|
1071
|
|
1072 static u_char *
|
|
1073 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,
|
|
1074 int *size)
|
|
1075 {
|
492
|
1076 char *failed;
|
|
1077 u_char *out;
|
594
|
1078 ngx_int_t jq;
|
492
|
1079 ngx_http_image_filter_conf_t *conf;
|
484
|
1080
|
|
1081 out = NULL;
|
|
1082
|
|
1083 switch (type) {
|
|
1084
|
|
1085 case NGX_HTTP_IMAGE_JPEG:
|
492
|
1086 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
|
594
|
1087
|
|
1088 jq = ngx_http_image_filter_get_value(r, conf->jqcv, conf->jpeg_quality);
|
|
1089 if (jq <= 0) {
|
|
1090 return NULL;
|
|
1091 }
|
|
1092
|
|
1093 out = gdImageJpegPtr(img, size, jq);
|
484
|
1094 failed = "gdImageJpegPtr() failed";
|
|
1095 break;
|
|
1096
|
|
1097 case NGX_HTTP_IMAGE_GIF:
|
|
1098 out = gdImageGifPtr(img, size);
|
|
1099 failed = "gdImageGifPtr() failed";
|
|
1100 break;
|
|
1101
|
|
1102 case NGX_HTTP_IMAGE_PNG:
|
|
1103 out = gdImagePngPtr(img, size);
|
|
1104 failed = "gdImagePngPtr() failed";
|
|
1105 break;
|
|
1106
|
|
1107 default:
|
|
1108 failed = "unknown image type";
|
|
1109 break;
|
|
1110 }
|
|
1111
|
|
1112 if (out == NULL) {
|
|
1113 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed);
|
|
1114 }
|
|
1115
|
|
1116 return out;
|
|
1117 }
|
|
1118
|
|
1119
|
|
1120 static void
|
|
1121 ngx_http_image_cleanup(void *data)
|
|
1122 {
|
|
1123 gdFree(data);
|
|
1124 }
|
|
1125
|
|
1126
|
510
|
1127 static ngx_uint_t
|
|
1128 ngx_http_image_filter_get_value(ngx_http_request_t *r,
|
|
1129 ngx_http_complex_value_t *cv, ngx_uint_t v)
|
|
1130 {
|
|
1131 ngx_str_t val;
|
|
1132
|
|
1133 if (cv == NULL) {
|
|
1134 return v;
|
|
1135 }
|
|
1136
|
|
1137 if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
|
|
1138 return 0;
|
|
1139 }
|
|
1140
|
|
1141 return ngx_http_image_filter_value(&val);
|
|
1142 }
|
|
1143
|
|
1144
|
|
1145 static ngx_uint_t
|
|
1146 ngx_http_image_filter_value(ngx_str_t *value)
|
|
1147 {
|
|
1148 ngx_int_t n;
|
|
1149
|
|
1150 if (value->len == 1 && value->data[0] == '-') {
|
|
1151 return (ngx_uint_t) -1;
|
|
1152 }
|
|
1153
|
|
1154 n = ngx_atoi(value->data, value->len);
|
|
1155
|
|
1156 if (n > 0) {
|
|
1157 return (ngx_uint_t) n;
|
|
1158 }
|
|
1159
|
|
1160 return 0;
|
|
1161 }
|
|
1162
|
|
1163
|
484
|
1164 static void *
|
|
1165 ngx_http_image_filter_create_conf(ngx_conf_t *cf)
|
|
1166 {
|
|
1167 ngx_http_image_filter_conf_t *conf;
|
|
1168
|
|
1169 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t));
|
|
1170 if (conf == NULL) {
|
496
|
1171 return NULL;
|
484
|
1172 }
|
|
1173
|
|
1174 conf->filter = NGX_CONF_UNSET_UINT;
|
594
|
1175 conf->jpeg_quality = NGX_CONF_UNSET_UINT;
|
642
|
1176 conf->sharpen = NGX_CONF_UNSET_UINT;
|
618
|
1177 conf->angle = NGX_CONF_UNSET_UINT;
|
528
|
1178 conf->transparency = NGX_CONF_UNSET;
|
484
|
1179 conf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
1180
|
|
1181 return conf;
|
|
1182 }
|
|
1183
|
|
1184
|
|
1185 static char *
|
|
1186 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1187 {
|
|
1188 ngx_http_image_filter_conf_t *prev = parent;
|
|
1189 ngx_http_image_filter_conf_t *conf = child;
|
|
1190
|
|
1191 if (conf->filter == NGX_CONF_UNSET_UINT) {
|
|
1192
|
|
1193 if (prev->filter == NGX_CONF_UNSET_UINT) {
|
|
1194 conf->filter = NGX_HTTP_IMAGE_OFF;
|
|
1195
|
|
1196 } else {
|
|
1197 conf->filter = prev->filter;
|
|
1198 conf->width = prev->width;
|
|
1199 conf->height = prev->height;
|
510
|
1200 conf->wcv = prev->wcv;
|
|
1201 conf->hcv = prev->hcv;
|
484
|
1202 }
|
|
1203 }
|
|
1204
|
492
|
1205 /* 75 is libjpeg default quality */
|
594
|
1206 ngx_conf_merge_uint_value(conf->jpeg_quality, prev->jpeg_quality, 75);
|
|
1207
|
|
1208 if (conf->jqcv == NULL) {
|
|
1209 conf->jqcv = prev->jqcv;
|
|
1210 }
|
492
|
1211
|
642
|
1212 ngx_conf_merge_uint_value(conf->sharpen, prev->sharpen, 0);
|
|
1213
|
|
1214 if (conf->shcv == NULL) {
|
|
1215 conf->shcv = prev->shcv;
|
|
1216 }
|
|
1217
|
618
|
1218 ngx_conf_merge_uint_value(conf->angle, prev->angle, 0);
|
|
1219 if (conf->acv == NULL) {
|
|
1220 conf->acv = prev->acv;
|
|
1221 }
|
|
1222
|
528
|
1223 ngx_conf_merge_value(conf->transparency, prev->transparency, 1);
|
|
1224
|
484
|
1225 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
|
|
1226 1 * 1024 * 1024);
|
|
1227
|
|
1228 return NGX_CONF_OK;
|
|
1229 }
|
|
1230
|
|
1231
|
|
1232 static char *
|
|
1233 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1234 {
|
|
1235 ngx_http_image_filter_conf_t *imcf = conf;
|
|
1236
|
510
|
1237 ngx_str_t *value;
|
|
1238 ngx_int_t n;
|
|
1239 ngx_uint_t i;
|
|
1240 ngx_http_complex_value_t cv;
|
|
1241 ngx_http_compile_complex_value_t ccv;
|
484
|
1242
|
|
1243 value = cf->args->elts;
|
|
1244
|
|
1245 i = 1;
|
|
1246
|
|
1247 if (cf->args->nelts == 2) {
|
|
1248 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1249 imcf->filter = NGX_HTTP_IMAGE_OFF;
|
|
1250
|
|
1251 } else if (ngx_strcmp(value[i].data, "test") == 0) {
|
|
1252 imcf->filter = NGX_HTTP_IMAGE_TEST;
|
|
1253
|
|
1254 } else if (ngx_strcmp(value[i].data, "size") == 0) {
|
|
1255 imcf->filter = NGX_HTTP_IMAGE_SIZE;
|
|
1256
|
|
1257 } else {
|
|
1258 goto failed;
|
|
1259 }
|
|
1260
|
|
1261 return NGX_CONF_OK;
|
618
|
1262
|
|
1263 } else if (cf->args->nelts == 3) {
|
|
1264
|
|
1265 if (ngx_strcmp(value[i].data, "rotate") == 0) {
|
642
|
1266 if (imcf->filter != NGX_HTTP_IMAGE_RESIZE
|
|
1267 && imcf->filter != NGX_HTTP_IMAGE_CROP)
|
|
1268 {
|
|
1269 imcf->filter = NGX_HTTP_IMAGE_ROTATE;
|
|
1270 }
|
618
|
1271
|
|
1272 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1273
|
|
1274 ccv.cf = cf;
|
|
1275 ccv.value = &value[++i];
|
|
1276 ccv.complex_value = &cv;
|
|
1277
|
|
1278 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1279 return NGX_CONF_ERROR;
|
|
1280 }
|
|
1281
|
|
1282 if (cv.lengths == NULL) {
|
|
1283 n = ngx_http_image_filter_value(&value[i]);
|
|
1284
|
|
1285 if (n != 90 && n != 180 && n != 270) {
|
|
1286 goto failed;
|
|
1287 }
|
|
1288
|
|
1289 imcf->angle = (ngx_uint_t) n;
|
|
1290
|
|
1291 } else {
|
|
1292 imcf->acv = ngx_palloc(cf->pool,
|
|
1293 sizeof(ngx_http_complex_value_t));
|
|
1294 if (imcf->acv == NULL) {
|
|
1295 return NGX_CONF_ERROR;
|
|
1296 }
|
|
1297
|
|
1298 *imcf->acv = cv;
|
|
1299 }
|
|
1300
|
|
1301 return NGX_CONF_OK;
|
|
1302
|
|
1303 } else {
|
|
1304 goto failed;
|
|
1305 }
|
484
|
1306 }
|
|
1307
|
|
1308 if (ngx_strcmp(value[i].data, "resize") == 0) {
|
|
1309 imcf->filter = NGX_HTTP_IMAGE_RESIZE;
|
|
1310
|
|
1311 } else if (ngx_strcmp(value[i].data, "crop") == 0) {
|
|
1312 imcf->filter = NGX_HTTP_IMAGE_CROP;
|
|
1313
|
|
1314 } else {
|
|
1315 goto failed;
|
|
1316 }
|
|
1317
|
510
|
1318 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1319
|
|
1320 ccv.cf = cf;
|
|
1321 ccv.value = &value[++i];
|
|
1322 ccv.complex_value = &cv;
|
484
|
1323
|
510
|
1324 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1325 return NGX_CONF_ERROR;
|
|
1326 }
|
484
|
1327
|
510
|
1328 if (cv.lengths == NULL) {
|
|
1329 n = ngx_http_image_filter_value(&value[i]);
|
|
1330
|
|
1331 if (n == 0) {
|
484
|
1332 goto failed;
|
|
1333 }
|
|
1334
|
|
1335 imcf->width = (ngx_uint_t) n;
|
510
|
1336
|
|
1337 } else {
|
|
1338 imcf->wcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
|
|
1339 if (imcf->wcv == NULL) {
|
|
1340 return NGX_CONF_ERROR;
|
|
1341 }
|
|
1342
|
|
1343 *imcf->wcv = cv;
|
484
|
1344 }
|
|
1345
|
510
|
1346 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1347
|
|
1348 ccv.cf = cf;
|
|
1349 ccv.value = &value[++i];
|
|
1350 ccv.complex_value = &cv;
|
484
|
1351
|
510
|
1352 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1353 return NGX_CONF_ERROR;
|
|
1354 }
|
484
|
1355
|
510
|
1356 if (cv.lengths == NULL) {
|
|
1357 n = ngx_http_image_filter_value(&value[i]);
|
|
1358
|
|
1359 if (n == 0) {
|
484
|
1360 goto failed;
|
|
1361 }
|
|
1362
|
|
1363 imcf->height = (ngx_uint_t) n;
|
510
|
1364
|
|
1365 } else {
|
|
1366 imcf->hcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
|
|
1367 if (imcf->hcv == NULL) {
|
|
1368 return NGX_CONF_ERROR;
|
|
1369 }
|
|
1370
|
|
1371 *imcf->hcv = cv;
|
484
|
1372 }
|
|
1373
|
|
1374 return NGX_CONF_OK;
|
|
1375
|
|
1376 failed:
|
|
1377
|
|
1378 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
|
|
1379 &value[i]);
|
|
1380
|
|
1381 return NGX_CONF_ERROR;
|
|
1382 }
|
|
1383
|
|
1384
|
594
|
1385 static char *
|
|
1386 ngx_http_image_filter_jpeg_quality(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1387 void *conf)
|
|
1388 {
|
|
1389 ngx_http_image_filter_conf_t *imcf = conf;
|
|
1390
|
|
1391 ngx_str_t *value;
|
|
1392 ngx_int_t n;
|
|
1393 ngx_http_complex_value_t cv;
|
|
1394 ngx_http_compile_complex_value_t ccv;
|
|
1395
|
|
1396 value = cf->args->elts;
|
|
1397
|
|
1398 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1399
|
|
1400 ccv.cf = cf;
|
|
1401 ccv.value = &value[1];
|
|
1402 ccv.complex_value = &cv;
|
|
1403
|
|
1404 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1405 return NGX_CONF_ERROR;
|
|
1406 }
|
|
1407
|
|
1408 if (cv.lengths == NULL) {
|
|
1409 n = ngx_http_image_filter_value(&value[1]);
|
|
1410
|
|
1411 if (n <= 0) {
|
|
1412 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
642
|
1413 "invalid value \"%V\"", &value[1]);
|
594
|
1414 return NGX_CONF_ERROR;
|
|
1415 }
|
|
1416
|
|
1417 imcf->jpeg_quality = (ngx_uint_t) n;
|
|
1418
|
|
1419 } else {
|
|
1420 imcf->jqcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
|
|
1421 if (imcf->jqcv == NULL) {
|
|
1422 return NGX_CONF_ERROR;
|
|
1423 }
|
|
1424
|
|
1425 *imcf->jqcv = cv;
|
|
1426 }
|
|
1427
|
|
1428 return NGX_CONF_OK;
|
|
1429 }
|
|
1430
|
|
1431
|
642
|
1432 static char *
|
|
1433 ngx_http_image_filter_sharpen(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1434 void *conf)
|
|
1435 {
|
|
1436 ngx_http_image_filter_conf_t *imcf = conf;
|
|
1437
|
|
1438 ngx_str_t *value;
|
|
1439 ngx_int_t n;
|
|
1440 ngx_http_complex_value_t cv;
|
|
1441 ngx_http_compile_complex_value_t ccv;
|
|
1442
|
|
1443 value = cf->args->elts;
|
|
1444
|
|
1445 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1446
|
|
1447 ccv.cf = cf;
|
|
1448 ccv.value = &value[1];
|
|
1449 ccv.complex_value = &cv;
|
|
1450
|
|
1451 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1452 return NGX_CONF_ERROR;
|
|
1453 }
|
|
1454
|
|
1455 if (cv.lengths == NULL) {
|
|
1456 n = ngx_http_image_filter_value(&value[1]);
|
|
1457
|
|
1458 if (n < 0) {
|
|
1459 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1460 "invalid value \"%V\"", &value[1]);
|
|
1461 return NGX_CONF_ERROR;
|
|
1462 }
|
|
1463
|
|
1464 imcf->sharpen = (ngx_uint_t) n;
|
|
1465
|
|
1466 } else {
|
|
1467 imcf->shcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
|
|
1468 if (imcf->shcv == NULL) {
|
|
1469 return NGX_CONF_ERROR;
|
|
1470 }
|
|
1471
|
|
1472 *imcf->shcv = cv;
|
|
1473 }
|
|
1474
|
|
1475 return NGX_CONF_OK;
|
|
1476 }
|
|
1477
|
|
1478
|
484
|
1479 static ngx_int_t
|
|
1480 ngx_http_image_filter_init(ngx_conf_t *cf)
|
|
1481 {
|
|
1482 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
1483 ngx_http_top_header_filter = ngx_http_image_header_filter;
|
|
1484
|
|
1485 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
1486 ngx_http_top_body_filter = ngx_http_image_body_filter;
|
|
1487
|
|
1488 return NGX_OK;
|
|
1489 }
|