Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 2812:dcb1b4d6ff54
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 06 May 2009 14:11:03 +0000 |
parents | d82d08314f78 |
children | 43fe53832da7 |
rev | line source |
---|---|
2788 | 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> | |
10 #include "gd.h" | |
11 | |
12 | |
13 #define NGX_HTTP_IMAGE_OFF 0 | |
14 #define NGX_HTTP_IMAGE_TEST 1 | |
15 #define NGX_HTTP_IMAGE_SIZE 2 | |
16 #define NGX_HTTP_IMAGE_RESIZE 3 | |
17 #define NGX_HTTP_IMAGE_CROP 4 | |
18 | |
19 | |
20 #define NGX_HTTP_IMAGE_START 0 | |
21 #define NGX_HTTP_IMAGE_READ 1 | |
22 #define NGX_HTTP_IMAGE_PROCESS 2 | |
23 #define NGX_HTTP_IMAGE_DONE 3 | |
24 | |
25 | |
26 #define NGX_HTTP_IMAGE_NONE 0 | |
27 #define NGX_HTTP_IMAGE_JPEG 1 | |
28 #define NGX_HTTP_IMAGE_GIF 2 | |
29 #define NGX_HTTP_IMAGE_PNG 3 | |
30 | |
31 | |
32 #define NGX_HTTP_IMAGE_BUFFERED 0x08 | |
33 | |
34 | |
35 typedef struct { | |
36 ngx_uint_t filter; | |
37 ngx_uint_t width; | |
38 ngx_uint_t height; | |
39 | |
40 size_t buffer_size; | |
41 } ngx_http_image_filter_conf_t; | |
42 | |
43 | |
44 typedef struct { | |
45 u_char *image; | |
46 u_char *last; | |
47 | |
48 size_t length; | |
49 | |
50 ngx_uint_t width; | |
51 ngx_uint_t height; | |
52 | |
53 ngx_uint_t phase; | |
54 ngx_uint_t type; | |
55 } ngx_http_image_filter_ctx_t; | |
56 | |
57 | |
58 static ngx_uint_t ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in); | |
59 static ngx_int_t ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in); | |
60 static ngx_buf_t *ngx_http_image_process(ngx_http_request_t *r); | |
61 static ngx_buf_t *ngx_http_image_json(ngx_http_request_t *r, | |
62 ngx_http_image_filter_ctx_t *ctx); | |
63 static ngx_buf_t *ngx_http_image_asis(ngx_http_request_t *r, | |
64 ngx_http_image_filter_ctx_t *ctx); | |
65 static void ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b); | |
66 static ngx_int_t ngx_http_image_size(ngx_http_request_t *r, | |
67 ngx_http_image_filter_ctx_t *ctx); | |
68 | |
69 static ngx_buf_t *ngx_http_image_resize(ngx_http_request_t *r, | |
70 ngx_http_image_filter_ctx_t *ctx); | |
71 static gdImagePtr ngx_http_image_source(ngx_http_request_t *r, | |
72 ngx_http_image_filter_ctx_t *ctx); | |
73 static gdImagePtr ngx_http_image_new(ngx_http_request_t *r, int w, int h, | |
74 int colors); | |
75 static u_char *ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, | |
76 gdImagePtr img, int *size); | |
77 static void ngx_http_image_cleanup(void *data); | |
78 | |
79 | |
80 static void *ngx_http_image_filter_create_conf(ngx_conf_t *cf); | |
81 static char *ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, | |
82 void *child); | |
83 static char *ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, | |
84 void *conf); | |
85 static ngx_int_t ngx_http_image_filter_init(ngx_conf_t *cf); | |
86 | |
87 | |
88 static ngx_command_t ngx_http_image_filter_commands[] = { | |
89 | |
90 { ngx_string("image_filter"), | |
91 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13, | |
92 ngx_http_image_filter, | |
93 NGX_HTTP_LOC_CONF_OFFSET, | |
94 0, | |
95 NULL }, | |
96 | |
97 { ngx_string("image_filter_buffer"), | |
98 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
99 ngx_conf_set_size_slot, | |
100 NGX_HTTP_LOC_CONF_OFFSET, | |
101 offsetof(ngx_http_image_filter_conf_t, buffer_size), | |
102 NULL }, | |
103 | |
104 ngx_null_command | |
105 }; | |
106 | |
107 | |
108 static ngx_http_module_t ngx_http_image_filter_module_ctx = { | |
109 NULL, /* preconfiguration */ | |
110 ngx_http_image_filter_init, /* postconfiguration */ | |
111 | |
112 NULL, /* create main configuration */ | |
113 NULL, /* init main configuration */ | |
114 | |
115 NULL, /* create server configuration */ | |
116 NULL, /* merge server configuration */ | |
117 | |
118 ngx_http_image_filter_create_conf, /* create location configuration */ | |
119 ngx_http_image_filter_merge_conf /* merge location configuration */ | |
120 }; | |
121 | |
122 | |
123 ngx_module_t ngx_http_image_filter_module = { | |
124 NGX_MODULE_V1, | |
125 &ngx_http_image_filter_module_ctx, /* module context */ | |
126 ngx_http_image_filter_commands, /* module directives */ | |
127 NGX_HTTP_MODULE, /* module type */ | |
128 NULL, /* init master */ | |
129 NULL, /* init module */ | |
130 NULL, /* init process */ | |
131 NULL, /* init thread */ | |
132 NULL, /* exit thread */ | |
133 NULL, /* exit process */ | |
134 NULL, /* exit master */ | |
135 NGX_MODULE_V1_PADDING | |
136 }; | |
137 | |
138 | |
139 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
140 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
141 | |
142 | |
143 static ngx_str_t ngx_http_image_types[] = { | |
144 ngx_string("image/jpeg"), | |
145 ngx_string("image/gif"), | |
146 ngx_string("image/png") | |
147 }; | |
148 | |
149 | |
150 static ngx_int_t | |
151 ngx_http_image_header_filter(ngx_http_request_t *r) | |
152 { | |
153 off_t len; | |
154 ngx_http_image_filter_ctx_t *ctx; | |
155 ngx_http_image_filter_conf_t *conf; | |
156 | |
157 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) { | |
158 return ngx_http_next_header_filter(r); | |
159 } | |
160 | |
161 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
162 | |
163 if (conf->filter == NGX_HTTP_IMAGE_OFF) { | |
164 return ngx_http_next_header_filter(r); | |
165 } | |
166 | |
167 if (r->headers_out.content_type.len | |
168 >= sizeof("multipart/x-mixed-replace") - 1 | |
169 && ngx_strncasecmp(r->headers_out.content_type.data, | |
170 (u_char *) "multipart/x-mixed-replace", | |
171 sizeof("multipart/x-mixed-replace") - 1) | |
172 == 0) | |
173 { | |
174 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
175 "image filter: multipart/x-mixed-replace response"); | |
176 | |
177 return NGX_ERROR; | |
178 } | |
179 | |
180 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_image_filter_ctx_t)); | |
181 if (ctx == NULL) { | |
182 return NGX_ERROR; | |
183 } | |
184 | |
185 ngx_http_set_ctx(r, ctx, ngx_http_image_filter_module); | |
186 | |
187 len = r->headers_out.content_length_n; | |
188 | |
2795
d82d08314f78
fix building ngx_http_image_filter_module on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
2788
diff
changeset
|
189 if (len != -1 && len > (off_t) conf->buffer_size) { |
2788 | 190 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
191 "image filter: too big response: %O", len); | |
192 | |
193 return NGX_ERROR; | |
194 } | |
195 | |
196 if (len == -1) { | |
197 ctx->length = conf->buffer_size; | |
198 | |
199 } else { | |
200 ctx->length = (size_t) len; | |
201 } | |
202 | |
203 if (r->headers_out.refresh) { | |
204 r->headers_out.refresh->hash = 0; | |
205 } | |
206 | |
207 r->main_filter_need_in_memory = 1; | |
208 r->allow_ranges = 0; | |
209 | |
210 return NGX_OK; | |
211 } | |
212 | |
213 | |
214 static ngx_int_t | |
215 ngx_http_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
216 { | |
217 ngx_int_t rc; | |
218 ngx_str_t *ct; | |
219 ngx_chain_t out; | |
220 ngx_http_image_filter_ctx_t *ctx; | |
221 ngx_http_image_filter_conf_t *conf; | |
222 | |
223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "image filter"); | |
224 | |
225 if (in == NULL) { | |
226 return ngx_http_next_body_filter(r, in); | |
227 } | |
228 | |
229 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
230 | |
231 if (ctx == NULL) { | |
232 return ngx_http_next_body_filter(r, in); | |
233 } | |
234 | |
235 switch (ctx->phase) { | |
236 | |
237 case NGX_HTTP_IMAGE_START: | |
238 | |
239 ctx->type = ngx_http_image_test(r, in); | |
240 | |
241 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
242 | |
243 if (ctx->type == NGX_HTTP_IMAGE_NONE) { | |
244 | |
245 if (conf->filter == NGX_HTTP_IMAGE_SIZE) { | |
246 out.buf = ngx_http_image_json(r, NULL); | |
247 | |
248 if (out.buf) { | |
249 out.next = NULL; | |
250 in = &out; | |
251 | |
252 break; | |
253 } | |
254 } | |
255 | |
256 return ngx_http_filter_finalize_request(r, | |
257 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); | |
258 } | |
259 | |
260 /* override content type */ | |
261 | |
262 ct = &ngx_http_image_types[ctx->type - 1]; | |
263 r->headers_out.content_type_len = ct->len; | |
264 r->headers_out.content_type = *ct; | |
265 | |
266 if (conf->filter == NGX_HTTP_IMAGE_TEST) { | |
267 break; | |
268 } | |
269 | |
270 ctx->phase = NGX_HTTP_IMAGE_READ; | |
271 | |
272 /* fall through */ | |
273 | |
274 case NGX_HTTP_IMAGE_READ: | |
275 | |
276 rc = ngx_http_image_read(r, in); | |
277 | |
278 if (rc == NGX_AGAIN) { | |
279 return NGX_OK; | |
280 } | |
281 | |
282 if (rc == NGX_ERROR) { | |
283 return ngx_http_filter_finalize_request(r, | |
284 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); | |
285 } | |
286 | |
287 /* fall through */ | |
288 | |
289 case NGX_HTTP_IMAGE_PROCESS: | |
290 | |
291 out.buf = ngx_http_image_process(r); | |
292 | |
293 if (out.buf == NULL) { | |
294 return ngx_http_filter_finalize_request(r, | |
295 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); | |
296 } | |
297 | |
298 out.next = NULL; | |
299 in = &out; | |
300 | |
301 break; | |
302 | |
303 default: /* NGX_HTTP_IMAGE_DONE */ | |
304 | |
305 return ngx_http_next_body_filter(r, in); | |
306 } | |
307 | |
308 ctx->phase = NGX_HTTP_IMAGE_DONE; | |
309 | |
310 rc = ngx_http_next_header_filter(r); | |
311 | |
312 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
313 return rc; | |
314 } | |
315 | |
316 return ngx_http_next_body_filter(r, in); | |
317 } | |
318 | |
319 | |
320 static ngx_uint_t | |
321 ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in) | |
322 { | |
323 u_char *p; | |
324 | |
325 p = in->buf->pos; | |
326 | |
327 if (in->buf->last - p < 16) { | |
328 return NGX_HTTP_IMAGE_NONE; | |
329 } | |
330 | |
331 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
332 "image filter: \"%c%c\"", p[0], p[1]); | |
333 | |
334 if (p[0] == 0xff && p[1] == 0xd8) { | |
335 | |
336 /* JPEG */ | |
337 | |
338 return NGX_HTTP_IMAGE_JPEG; | |
339 | |
340 } else if (p[0] == 'G' && p[1] == 'I' && p[2] == 'F' && p[3] == '8' | |
341 && p[4] == '9' && p[5] == 'a') | |
342 { | |
343 /* GIF */ | |
344 | |
345 return NGX_HTTP_IMAGE_GIF; | |
346 | |
347 } else if (p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' && p[3] == 'G' | |
348 && p[4] == 0x0d && p[5] == 0x0a && p[6] == 0x1a && p[7] == 0x0a) | |
349 { | |
350 /* PNG */ | |
351 | |
352 return NGX_HTTP_IMAGE_PNG; | |
353 } | |
354 | |
355 return NGX_HTTP_IMAGE_NONE; | |
356 } | |
357 | |
358 | |
359 static ngx_int_t | |
360 ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in) | |
361 { | |
362 u_char *p; | |
363 size_t size, rest; | |
364 ngx_buf_t *b; | |
365 ngx_chain_t *cl; | |
366 ngx_http_image_filter_ctx_t *ctx; | |
367 | |
368 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
369 | |
370 if (ctx->image == NULL) { | |
371 ctx->image = ngx_palloc(r->pool, ctx->length); | |
372 if (ctx->image == NULL) { | |
373 return NGX_ERROR; | |
374 } | |
375 | |
376 ctx->last = ctx->image; | |
377 } | |
378 | |
379 p = ctx->last; | |
380 | |
381 for (cl = in; cl; cl = cl->next) { | |
382 | |
383 b = cl->buf; | |
384 size = b->last - b->pos; | |
385 | |
386 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
387 "image buf: %uz", size); | |
388 | |
389 rest = ctx->image + ctx->length - p; | |
390 size = (rest < size) ? rest : size; | |
391 | |
392 p = ngx_cpymem(p, b->pos, size); | |
393 b->pos += size; | |
394 | |
395 if (b->last_buf) { | |
396 ctx->last = p; | |
397 return NGX_OK; | |
398 } | |
399 } | |
400 | |
401 ctx->last = p; | |
402 r->connection->buffered |= NGX_HTTP_IMAGE_BUFFERED; | |
403 | |
404 return NGX_AGAIN; | |
405 } | |
406 | |
407 | |
408 static ngx_buf_t * | |
409 ngx_http_image_process(ngx_http_request_t *r) | |
410 { | |
411 ngx_buf_t *b; | |
412 ngx_int_t rc; | |
413 ngx_http_image_filter_ctx_t *ctx; | |
414 ngx_http_image_filter_conf_t *conf; | |
415 | |
416 r->connection->buffered &= ~NGX_HTTP_IMAGE_BUFFERED; | |
417 | |
418 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
419 | |
420 rc = ngx_http_image_size(r, ctx); | |
421 | |
422 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
423 | |
424 if (conf->filter == NGX_HTTP_IMAGE_SIZE) { | |
425 | |
426 b = ngx_http_image_json(r, rc == NGX_OK ? ctx : NULL); | |
427 | |
428 } else if (rc == NGX_OK | |
429 && ctx->width <= conf->width | |
430 && ctx->height <= conf->height) | |
431 { | |
432 b = ngx_http_image_asis(r, ctx); | |
433 | |
434 } else { | |
435 b = ngx_http_image_resize(r, ctx); | |
436 } | |
437 | |
438 return b; | |
439 } | |
440 | |
441 | |
442 static ngx_buf_t * | |
443 ngx_http_image_json(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
444 { | |
445 size_t len; | |
446 ngx_buf_t *b; | |
447 | |
448 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
449 if (b == NULL) { | |
450 return NULL; | |
451 } | |
452 | |
453 b->memory = 1; | |
454 b->last_buf = 1; | |
455 | |
456 ngx_http_clean_header(r); | |
457 | |
458 r->headers_out.status = NGX_HTTP_OK; | |
459 r->headers_out.content_type.len = sizeof("text/plain") - 1; | |
460 r->headers_out.content_type.data = (u_char *) "text/plain"; | |
461 | |
462 if (ctx == NULL) { | |
463 b->pos = (u_char *) "{}" CRLF; | |
464 b->last = b->pos + sizeof("{}" CRLF) - 1; | |
465 | |
466 ngx_http_image_length(r, b); | |
467 | |
468 return b; | |
469 } | |
470 | |
471 len = sizeof("{ \"img\" : " | |
472 "{ \"width\": , \"height\": , \"type\": \"jpeg\" } }" CRLF) - 1 | |
473 + 2 * NGX_SIZE_T_LEN; | |
474 | |
475 b->pos = ngx_pnalloc(r->pool, len); | |
476 if (b->pos == NULL) { | |
477 return NULL; | |
478 } | |
479 | |
480 b->last = ngx_sprintf(b->pos, | |
481 "{ \"img\" : " | |
482 "{ \"width\": %uz," | |
483 " \"height\": %uz," | |
484 " \"type\": \"%s\" } }" CRLF, | |
485 ctx->width, ctx->height, | |
486 ngx_http_image_types[ctx->type - 1].data + 6); | |
487 | |
488 ngx_http_image_length(r, b); | |
489 | |
490 return b; | |
491 } | |
492 | |
493 | |
494 static ngx_buf_t * | |
495 ngx_http_image_asis(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
496 { | |
497 ngx_buf_t *b; | |
498 | |
499 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
500 if (b == NULL) { | |
501 return NULL; | |
502 } | |
503 | |
504 b->pos = ctx->image; | |
505 b->last = ctx->last; | |
506 b->memory = 1; | |
507 b->last_buf = 1; | |
508 | |
509 ngx_http_image_length(r, b); | |
510 | |
511 return b; | |
512 } | |
513 | |
514 | |
515 static void | |
516 ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b) | |
517 { | |
518 r->headers_out.content_length_n = b->last - b->pos; | |
519 | |
520 if (r->headers_out.content_length) { | |
521 r->headers_out.content_length->hash = 0; | |
522 } | |
523 | |
524 r->headers_out.content_length = NULL; | |
525 } | |
526 | |
527 | |
528 static ngx_int_t | |
529 ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
530 { | |
531 u_char *p, *last; | |
532 ngx_uint_t width, height; | |
533 | |
534 p = ctx->image; | |
535 | |
536 switch (ctx->type) { | |
537 | |
538 case NGX_HTTP_IMAGE_JPEG: | |
539 | |
540 p += 2; | |
541 last = ctx->image + ctx->length - 10; | |
542 | |
543 while (p < last) { | |
544 | |
545 if (p[0] == 0xff && p[1] != 0xff) { | |
546 | |
547 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
548 "JPEG: %02xd %02xd", *p, *(p + 1)); | |
549 | |
550 p++; | |
551 | |
552 if (*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3 | |
553 || *p == 0xc9 || *p == 0xca || *p == 0xcb) | |
554 { | |
555 goto found; | |
556 } | |
557 | |
558 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
559 "JPEG: %02xd %02xd", p[1], p[2]); | |
560 | |
561 p += p[1] * 256 + p[2]; | |
562 | |
563 continue; | |
564 } | |
565 | |
566 p++; | |
567 } | |
568 | |
569 return NGX_DECLINED; | |
570 | |
571 found: | |
572 | |
573 width = p[6] * 256 + p[7]; | |
574 height = p[4] * 256 + p[5]; | |
575 | |
576 break; | |
577 | |
578 case NGX_HTTP_IMAGE_GIF: | |
579 | |
580 if (ctx->length < 10) { | |
581 return NGX_DECLINED; | |
582 } | |
583 | |
584 width = p[7] * 256 + p[6]; | |
585 height = p[9] * 256 + p[8]; | |
586 | |
587 break; | |
588 | |
589 case NGX_HTTP_IMAGE_PNG: | |
590 | |
591 if (ctx->length < 24) { | |
592 return NGX_DECLINED; | |
593 } | |
594 | |
595 width = p[18] * 256 + p[19]; | |
596 height = p[22] * 256 + p[23]; | |
597 | |
598 break; | |
599 | |
600 default: | |
601 | |
602 return NGX_DECLINED; | |
603 } | |
604 | |
605 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
606 "image size: %d x %d", width, height); | |
607 | |
608 ctx->width = width; | |
609 ctx->height = height; | |
610 | |
611 return NGX_OK; | |
612 } | |
613 | |
614 | |
615 static ngx_buf_t * | |
616 ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
617 { | |
618 int sx, sy, dx, dy, ox, oy, | |
619 colors, transparent, size; | |
620 u_char *out; | |
621 ngx_buf_t *b; | |
622 ngx_uint_t resize; | |
623 gdImagePtr src, dst; | |
624 ngx_pool_cleanup_t *cln; | |
625 ngx_http_image_filter_conf_t *conf; | |
626 | |
627 src = ngx_http_image_source(r, ctx); | |
628 | |
629 if (src == NULL) { | |
630 return NULL; | |
631 } | |
632 | |
633 sx = gdImageSX(src); | |
634 sy = gdImageSY(src); | |
635 | |
636 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
637 | |
638 if ((ngx_uint_t) sx <= conf->width && (ngx_uint_t) sy <= conf->height) { | |
639 gdImageDestroy(src); | |
640 return ngx_http_image_asis(r, ctx); | |
641 } | |
642 | |
643 colors = gdImageColorsTotal(src); | |
644 transparent = gdImageGetTransparent(src); | |
645 | |
646 dx = sx; | |
647 dy = sy; | |
648 | |
649 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
650 | |
651 if ((ngx_uint_t) dx > conf->width) { | |
652 dy = dy * conf->width / dx; | |
653 dy = dy ? dy : 1; | |
654 dx = conf->width; | |
655 } | |
656 | |
657 if ((ngx_uint_t) dy > conf->height) { | |
658 dx = dx * conf->height / dy; | |
659 dx = dx ? dx : 1; | |
660 dy = conf->height; | |
661 } | |
662 | |
663 resize = 1; | |
664 | |
665 } else { /* NGX_HTTP_IMAGE_CROP */ | |
666 | |
667 resize = 0; | |
668 | |
669 if ((ngx_uint_t) (dx * 100 / dy) < conf->width * 100 / conf->height) { | |
670 | |
671 if ((ngx_uint_t) dx > conf->width) { | |
672 dy = dy * conf->width / dx; | |
673 dy = dy ? dy : 1; | |
674 dx = conf->width; | |
675 resize = 1; | |
676 } | |
677 | |
678 } else { | |
679 if ((ngx_uint_t) dy > conf->height) { | |
680 dx = dx * conf->height / dy; | |
681 dx = dx ? dx : 1; | |
682 dy = conf->height; | |
683 resize = 1; | |
684 } | |
685 } | |
686 } | |
687 | |
688 if (resize) { | |
689 dst = ngx_http_image_new(r, dx, dy, colors); | |
690 if (dst == NULL) { | |
691 gdImageDestroy(src); | |
692 return NULL; | |
693 } | |
694 | |
695 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); | |
696 | |
697 gdImageDestroy(src); | |
698 | |
699 } else { | |
700 dst = src; | |
701 } | |
702 | |
703 if (conf->filter == NGX_HTTP_IMAGE_CROP) { | |
704 | |
705 src = dst; | |
706 | |
707 if ((ngx_uint_t) dx > conf->width) { | |
708 ox = dx - conf->width; | |
709 | |
710 } else { | |
711 ox = 0; | |
712 } | |
713 | |
714 if ((ngx_uint_t) dy > conf->height) { | |
715 oy = dy - conf->height; | |
716 | |
717 } else { | |
718 oy = 0; | |
719 } | |
720 | |
721 if (ox || oy) { | |
722 | |
723 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); | |
724 | |
725 if (dst == NULL) { | |
726 gdImageDestroy(src); | |
727 return NULL; | |
728 } | |
729 | |
730 ox /= 2; | |
731 oy /= 2; | |
732 | |
733 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
734 "image crop: %d x %d @ %d x %d", | |
735 dx, dy, ox, oy); | |
736 | |
737 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); | |
738 | |
739 gdImageDestroy(src); | |
740 } | |
741 } | |
742 | |
743 gdImageColorTransparent(dst, transparent); | |
744 | |
745 out = ngx_http_image_out(r, ctx->type, dst, &size); | |
746 | |
747 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
748 "image: %d x %d %d", sx, sy, colors); | |
749 | |
750 gdImageDestroy(dst); | |
751 ngx_pfree(r->pool, ctx->image); | |
752 | |
753 if (out == NULL) { | |
754 return NULL; | |
755 } | |
756 | |
757 cln = ngx_pool_cleanup_add(r->pool, 0); | |
758 if (cln == NULL) { | |
759 gdFree(out); | |
760 return NULL; | |
761 } | |
762 | |
763 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
764 if (b == NULL) { | |
765 gdFree(out); | |
766 return NULL; | |
767 } | |
768 | |
769 cln->handler = ngx_http_image_cleanup; | |
770 cln->data = out; | |
771 | |
772 b->pos = out; | |
773 b->last = out + size; | |
774 b->memory = 1; | |
775 b->last_buf = 1; | |
776 | |
777 ngx_http_image_length(r, b); | |
778 | |
779 return b; | |
780 } | |
781 | |
782 | |
783 static gdImagePtr | |
784 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
785 { | |
786 char *failed; | |
787 gdImagePtr img; | |
788 | |
789 img = NULL; | |
790 | |
791 switch (ctx->type) { | |
792 | |
793 case NGX_HTTP_IMAGE_JPEG: | |
794 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image); | |
795 failed = "gdImageCreateFromJpegPtr() failed"; | |
796 break; | |
797 | |
798 case NGX_HTTP_IMAGE_GIF: | |
799 img = gdImageCreateFromGifPtr(ctx->length, ctx->image); | |
800 failed = "gdImageCreateFromGifPtr() failed"; | |
801 break; | |
802 | |
803 case NGX_HTTP_IMAGE_PNG: | |
804 img = gdImageCreateFromPngPtr(ctx->length, ctx->image); | |
805 failed = "gdImageCreateFromPngPtr() failed"; | |
806 break; | |
807 | |
808 default: | |
809 failed = "unknown image type"; | |
810 break; | |
811 } | |
812 | |
813 if (img == NULL) { | |
814 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
815 } | |
816 | |
817 return img; | |
818 } | |
819 | |
820 | |
821 static gdImagePtr | |
822 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) | |
823 { | |
824 gdImagePtr img; | |
825 | |
826 if (colors == 0) { | |
827 img = gdImageCreateTrueColor(w, h); | |
828 | |
829 if (img == NULL) { | |
830 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
831 "gdImageCreateTrueColor() failed"); | |
832 return NULL; | |
833 } | |
834 | |
835 } else { | |
836 img = gdImageCreate(w, h); | |
837 | |
838 if (img == NULL) { | |
839 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
840 "gdImageCreate() failed"); | |
841 return NULL; | |
842 } | |
843 } | |
844 | |
845 return img; | |
846 } | |
847 | |
848 | |
849 static u_char * | |
850 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img, | |
851 int *size) | |
852 { | |
853 char *failed; | |
854 u_char *out; | |
855 | |
856 out = NULL; | |
857 | |
858 switch (type) { | |
859 | |
860 case NGX_HTTP_IMAGE_JPEG: | |
861 out = gdImageJpegPtr(img, size, /* default quality */ -1); | |
862 failed = "gdImageJpegPtr() failed"; | |
863 break; | |
864 | |
865 case NGX_HTTP_IMAGE_GIF: | |
866 out = gdImageGifPtr(img, size); | |
867 failed = "gdImageGifPtr() failed"; | |
868 break; | |
869 | |
870 case NGX_HTTP_IMAGE_PNG: | |
871 out = gdImagePngPtr(img, size); | |
872 failed = "gdImagePngPtr() failed"; | |
873 break; | |
874 | |
875 default: | |
876 failed = "unknown image type"; | |
877 break; | |
878 } | |
879 | |
880 if (out == NULL) { | |
881 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
882 } | |
883 | |
884 return out; | |
885 } | |
886 | |
887 | |
888 static void | |
889 ngx_http_image_cleanup(void *data) | |
890 { | |
891 gdFree(data); | |
892 } | |
893 | |
894 | |
895 static void * | |
896 ngx_http_image_filter_create_conf(ngx_conf_t *cf) | |
897 { | |
898 ngx_http_image_filter_conf_t *conf; | |
899 | |
900 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t)); | |
901 if (conf == NULL) { | |
902 return NGX_CONF_ERROR; | |
903 } | |
904 | |
905 conf->filter = NGX_CONF_UNSET_UINT; | |
906 conf->buffer_size = NGX_CONF_UNSET_SIZE; | |
907 | |
908 return conf; | |
909 } | |
910 | |
911 | |
912 static char * | |
913 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
914 { | |
915 ngx_http_image_filter_conf_t *prev = parent; | |
916 ngx_http_image_filter_conf_t *conf = child; | |
917 | |
918 if (conf->filter == NGX_CONF_UNSET_UINT) { | |
919 | |
920 if (prev->filter == NGX_CONF_UNSET_UINT) { | |
921 conf->filter = NGX_HTTP_IMAGE_OFF; | |
922 | |
923 } else { | |
924 conf->filter = prev->filter; | |
925 conf->width = prev->width; | |
926 conf->height = prev->height; | |
927 } | |
928 } | |
929 | |
930 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, | |
931 1 * 1024 * 1024); | |
932 | |
933 return NGX_CONF_OK; | |
934 } | |
935 | |
936 | |
937 static char * | |
938 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
939 { | |
940 ngx_http_image_filter_conf_t *imcf = conf; | |
941 | |
942 ngx_str_t *value; | |
943 ngx_int_t n; | |
944 ngx_uint_t i; | |
945 | |
946 value = cf->args->elts; | |
947 | |
948 i = 1; | |
949 | |
950 if (cf->args->nelts == 2) { | |
951 if (ngx_strcmp(value[i].data, "off") == 0) { | |
952 imcf->filter = NGX_HTTP_IMAGE_OFF; | |
953 | |
954 } else if (ngx_strcmp(value[i].data, "test") == 0) { | |
955 imcf->filter = NGX_HTTP_IMAGE_TEST; | |
956 | |
957 } else if (ngx_strcmp(value[i].data, "size") == 0) { | |
958 imcf->filter = NGX_HTTP_IMAGE_SIZE; | |
959 | |
960 } else { | |
961 goto failed; | |
962 } | |
963 | |
964 return NGX_CONF_OK; | |
965 } | |
966 | |
967 if (ngx_strcmp(value[i].data, "resize") == 0) { | |
968 imcf->filter = NGX_HTTP_IMAGE_RESIZE; | |
969 | |
970 } else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
971 imcf->filter = NGX_HTTP_IMAGE_CROP; | |
972 | |
973 } else { | |
974 goto failed; | |
975 } | |
976 | |
977 i++; | |
978 | |
979 if (value[i].len == 1 && value[i].data[0] == '-') { | |
980 imcf->width = (ngx_uint_t) -1; | |
981 | |
982 } else { | |
983 n = ngx_atoi(value[i].data, value[i].len); | |
984 if (n == NGX_ERROR) { | |
985 goto failed; | |
986 } | |
987 | |
988 imcf->width = (ngx_uint_t) n; | |
989 } | |
990 | |
991 i++; | |
992 | |
993 if (value[i].len == 1 && value[i].data[0] == '-') { | |
994 imcf->height = (ngx_uint_t) -1; | |
995 | |
996 } else { | |
997 n = ngx_atoi(value[i].data, value[i].len); | |
998 if (n == NGX_ERROR) { | |
999 goto failed; | |
1000 } | |
1001 | |
1002 imcf->height = (ngx_uint_t) n; | |
1003 } | |
1004 | |
1005 return NGX_CONF_OK; | |
1006 | |
1007 failed: | |
1008 | |
1009 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
1010 &value[i]); | |
1011 | |
1012 return NGX_CONF_ERROR; | |
1013 } | |
1014 | |
1015 | |
1016 static ngx_int_t | |
1017 ngx_http_image_filter_init(ngx_conf_t *cf) | |
1018 { | |
1019 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1020 ngx_http_top_header_filter = ngx_http_image_header_filter; | |
1021 | |
1022 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1023 ngx_http_top_body_filter = ngx_http_image_body_filter; | |
1024 | |
1025 return NGX_OK; | |
1026 } |