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