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