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