annotate src/http/modules/ngx_http_image_filter_module.c @ 3744:77b18d36d123

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