Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 3720:c3a37fc8d1fe
use an empty name as default server_name instread of hostname
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 29 Jul 2010 10:00:04 +0000 |
parents | dd1570b6f237 |
children | 1a728cc43bb1 |
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; | |
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 | 534 |
535 if (ctx == NULL) { | |
536 b->pos = (u_char *) "{}" CRLF; | |
537 b->last = b->pos + sizeof("{}" CRLF) - 1; | |
538 | |
539 ngx_http_image_length(r, b); | |
540 | |
541 return b; | |
542 } | |
543 | |
544 len = sizeof("{ \"img\" : " | |
545 "{ \"width\": , \"height\": , \"type\": \"jpeg\" } }" CRLF) - 1 | |
546 + 2 * NGX_SIZE_T_LEN; | |
547 | |
548 b->pos = ngx_pnalloc(r->pool, len); | |
549 if (b->pos == NULL) { | |
550 return NULL; | |
551 } | |
552 | |
553 b->last = ngx_sprintf(b->pos, | |
554 "{ \"img\" : " | |
555 "{ \"width\": %uz," | |
556 " \"height\": %uz," | |
557 " \"type\": \"%s\" } }" CRLF, | |
558 ctx->width, ctx->height, | |
559 ngx_http_image_types[ctx->type - 1].data + 6); | |
560 | |
561 ngx_http_image_length(r, b); | |
562 | |
563 return b; | |
564 } | |
565 | |
566 | |
567 static ngx_buf_t * | |
568 ngx_http_image_asis(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
569 { | |
570 ngx_buf_t *b; | |
571 | |
572 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
573 if (b == NULL) { | |
574 return NULL; | |
575 } | |
576 | |
577 b->pos = ctx->image; | |
578 b->last = ctx->last; | |
579 b->memory = 1; | |
580 b->last_buf = 1; | |
581 | |
582 ngx_http_image_length(r, b); | |
583 | |
584 return b; | |
585 } | |
586 | |
587 | |
588 static void | |
589 ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b) | |
590 { | |
591 r->headers_out.content_length_n = b->last - b->pos; | |
592 | |
593 if (r->headers_out.content_length) { | |
594 r->headers_out.content_length->hash = 0; | |
595 } | |
596 | |
597 r->headers_out.content_length = NULL; | |
598 } | |
599 | |
600 | |
601 static ngx_int_t | |
602 ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
603 { | |
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 | 606 ngx_uint_t width, height; |
607 | |
608 p = ctx->image; | |
609 | |
610 switch (ctx->type) { | |
611 | |
612 case NGX_HTTP_IMAGE_JPEG: | |
613 | |
614 p += 2; | |
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 | 619 |
620 while (p < last) { | |
621 | |
622 if (p[0] == 0xff && p[1] != 0xff) { | |
623 | |
624 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3393 | 625 "JPEG: %02xd %02xd", p[0], p[1]); |
2788 | 626 |
627 p++; | |
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 | 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 | 635 } |
636 | |
637 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
638 "JPEG: %02xd %02xd", p[1], p[2]); | |
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 | 648 |
649 continue; | |
650 } | |
651 | |
652 p++; | |
653 } | |
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 | 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 | 665 |
666 break; | |
667 | |
668 case NGX_HTTP_IMAGE_GIF: | |
669 | |
670 if (ctx->length < 10) { | |
671 return NGX_DECLINED; | |
672 } | |
673 | |
674 width = p[7] * 256 + p[6]; | |
675 height = p[9] * 256 + p[8]; | |
676 | |
677 break; | |
678 | |
679 case NGX_HTTP_IMAGE_PNG: | |
680 | |
681 if (ctx->length < 24) { | |
682 return NGX_DECLINED; | |
683 } | |
684 | |
685 width = p[18] * 256 + p[19]; | |
686 height = p[22] * 256 + p[23]; | |
687 | |
688 break; | |
689 | |
690 default: | |
691 | |
692 return NGX_DECLINED; | |
693 } | |
694 | |
695 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
696 "image size: %d x %d", width, height); | |
697 | |
698 ctx->width = width; | |
699 ctx->height = height; | |
700 | |
701 return NGX_OK; | |
702 } | |
703 | |
704 | |
705 static ngx_buf_t * | |
706 ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
707 { | |
3134 | 708 int sx, sy, dx, dy, ox, oy, size, |
709 colors, palette, transparent, | |
710 red, green, blue; | |
2788 | 711 u_char *out; |
712 ngx_buf_t *b; | |
713 ngx_uint_t resize; | |
714 gdImagePtr src, dst; | |
715 ngx_pool_cleanup_t *cln; | |
716 ngx_http_image_filter_conf_t *conf; | |
717 | |
718 src = ngx_http_image_source(r, ctx); | |
719 | |
720 if (src == NULL) { | |
721 return NULL; | |
722 } | |
723 | |
724 sx = gdImageSX(src); | |
725 sy = gdImageSY(src); | |
726 | |
727 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
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 | 733 gdImageDestroy(src); |
734 return ngx_http_image_asis(r, ctx); | |
735 } | |
736 | |
737 colors = gdImageColorsTotal(src); | |
3134 | 738 |
739 if (colors && conf->transparency) { | |
740 transparent = gdImageGetTransparent(src); | |
741 | |
742 if (transparent != -1) { | |
743 palette = colors; | |
744 red = gdImageRed(src, transparent); | |
745 green = gdImageGreen(src, transparent); | |
746 blue = gdImageBlue(src, transparent); | |
2788 | 747 |
3134 | 748 goto transparent; |
749 } | |
750 } | |
3117 | 751 |
3134 | 752 palette = 0; |
753 transparent = -1; | |
754 red = 0; | |
755 green = 0; | |
756 blue = 0; | |
757 | |
758 transparent: | |
759 | |
760 gdImageColorTransparent(src, -1); | |
3117 | 761 |
2788 | 762 dx = sx; |
763 dy = sy; | |
764 | |
765 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
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 | 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 | 771 } |
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 | 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 | 777 } |
778 | |
779 resize = 1; | |
780 | |
781 } else { /* NGX_HTTP_IMAGE_CROP */ | |
782 | |
783 resize = 0; | |
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 | 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 | 792 resize = 1; |
793 } | |
794 | |
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 | 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 | 800 resize = 1; |
801 } | |
802 } | |
803 } | |
804 | |
805 if (resize) { | |
3134 | 806 dst = ngx_http_image_new(r, dx, dy, palette); |
2788 | 807 if (dst == NULL) { |
808 gdImageDestroy(src); | |
809 return NULL; | |
810 } | |
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 | 817 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); |
818 | |
3134 | 819 if (colors) { |
820 gdImageTrueColorToPalette(dst, 1, 256); | |
821 } | |
822 | |
2788 | 823 gdImageDestroy(src); |
824 | |
825 } else { | |
826 dst = src; | |
827 } | |
828 | |
829 if (conf->filter == NGX_HTTP_IMAGE_CROP) { | |
830 | |
831 src = dst; | |
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 | 835 |
836 } else { | |
837 ox = 0; | |
838 } | |
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 | 842 |
843 } else { | |
844 oy = 0; | |
845 } | |
846 | |
847 if (ox || oy) { | |
848 | |
849 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); | |
850 | |
851 if (dst == NULL) { | |
852 gdImageDestroy(src); | |
853 return NULL; | |
854 } | |
855 | |
856 ox /= 2; | |
857 oy /= 2; | |
858 | |
859 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
860 "image crop: %d x %d @ %d x %d", | |
861 dx, dy, ox, oy); | |
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 | 868 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); |
869 | |
3134 | 870 if (colors) { |
871 gdImageTrueColorToPalette(dst, 1, 256); | |
872 } | |
873 | |
2788 | 874 gdImageDestroy(src); |
875 } | |
876 } | |
877 | |
3117 | 878 if (transparent != -1 && colors) { |
879 gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue)); | |
880 } | |
2788 | 881 |
882 out = ngx_http_image_out(r, ctx->type, dst, &size); | |
883 | |
884 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
885 "image: %d x %d %d", sx, sy, colors); | |
886 | |
887 gdImageDestroy(dst); | |
888 ngx_pfree(r->pool, ctx->image); | |
889 | |
890 if (out == NULL) { | |
891 return NULL; | |
892 } | |
893 | |
894 cln = ngx_pool_cleanup_add(r->pool, 0); | |
895 if (cln == NULL) { | |
896 gdFree(out); | |
897 return NULL; | |
898 } | |
899 | |
900 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
901 if (b == NULL) { | |
902 gdFree(out); | |
903 return NULL; | |
904 } | |
905 | |
906 cln->handler = ngx_http_image_cleanup; | |
907 cln->data = out; | |
908 | |
909 b->pos = out; | |
910 b->last = out + size; | |
911 b->memory = 1; | |
912 b->last_buf = 1; | |
913 | |
914 ngx_http_image_length(r, b); | |
915 | |
916 return b; | |
917 } | |
918 | |
919 | |
920 static gdImagePtr | |
921 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
922 { | |
923 char *failed; | |
924 gdImagePtr img; | |
925 | |
926 img = NULL; | |
927 | |
928 switch (ctx->type) { | |
929 | |
930 case NGX_HTTP_IMAGE_JPEG: | |
931 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image); | |
932 failed = "gdImageCreateFromJpegPtr() failed"; | |
933 break; | |
934 | |
935 case NGX_HTTP_IMAGE_GIF: | |
936 img = gdImageCreateFromGifPtr(ctx->length, ctx->image); | |
937 failed = "gdImageCreateFromGifPtr() failed"; | |
938 break; | |
939 | |
940 case NGX_HTTP_IMAGE_PNG: | |
941 img = gdImageCreateFromPngPtr(ctx->length, ctx->image); | |
942 failed = "gdImageCreateFromPngPtr() failed"; | |
943 break; | |
944 | |
945 default: | |
946 failed = "unknown image type"; | |
947 break; | |
948 } | |
949 | |
950 if (img == NULL) { | |
951 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
952 } | |
953 | |
954 return img; | |
955 } | |
956 | |
957 | |
958 static gdImagePtr | |
959 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) | |
960 { | |
961 gdImagePtr img; | |
962 | |
963 if (colors == 0) { | |
964 img = gdImageCreateTrueColor(w, h); | |
965 | |
966 if (img == NULL) { | |
967 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
968 "gdImageCreateTrueColor() failed"); | |
969 return NULL; | |
970 } | |
971 | |
972 } else { | |
973 img = gdImageCreate(w, h); | |
974 | |
975 if (img == NULL) { | |
976 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
977 "gdImageCreate() failed"); | |
978 return NULL; | |
979 } | |
980 } | |
981 | |
982 return img; | |
983 } | |
984 | |
985 | |
986 static u_char * | |
987 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img, | |
988 int *size) | |
989 { | |
2848 | 990 char *failed; |
991 u_char *out; | |
992 ngx_http_image_filter_conf_t *conf; | |
2788 | 993 |
994 out = NULL; | |
995 | |
996 switch (type) { | |
997 | |
998 case NGX_HTTP_IMAGE_JPEG: | |
2848 | 999 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); |
1000 out = gdImageJpegPtr(img, size, conf->jpeg_quality); | |
2788 | 1001 failed = "gdImageJpegPtr() failed"; |
1002 break; | |
1003 | |
1004 case NGX_HTTP_IMAGE_GIF: | |
1005 out = gdImageGifPtr(img, size); | |
1006 failed = "gdImageGifPtr() failed"; | |
1007 break; | |
1008 | |
1009 case NGX_HTTP_IMAGE_PNG: | |
1010 out = gdImagePngPtr(img, size); | |
1011 failed = "gdImagePngPtr() failed"; | |
1012 break; | |
1013 | |
1014 default: | |
1015 failed = "unknown image type"; | |
1016 break; | |
1017 } | |
1018 | |
1019 if (out == NULL) { | |
1020 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
1021 } | |
1022 | |
1023 return out; | |
1024 } | |
1025 | |
1026 | |
1027 static void | |
1028 ngx_http_image_cleanup(void *data) | |
1029 { | |
1030 gdFree(data); | |
1031 } | |
1032 | |
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 | 1071 static void * |
1072 ngx_http_image_filter_create_conf(ngx_conf_t *cf) | |
1073 { | |
1074 ngx_http_image_filter_conf_t *conf; | |
1075 | |
1076 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t)); | |
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 | 1079 } |
1080 | |
1081 conf->filter = NGX_CONF_UNSET_UINT; | |
2848 | 1082 conf->jpeg_quality = NGX_CONF_UNSET; |
3134 | 1083 conf->transparency = NGX_CONF_UNSET; |
2788 | 1084 conf->buffer_size = NGX_CONF_UNSET_SIZE; |
1085 | |
1086 return conf; | |
1087 } | |
1088 | |
1089 | |
1090 static char * | |
1091 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1092 { | |
1093 ngx_http_image_filter_conf_t *prev = parent; | |
1094 ngx_http_image_filter_conf_t *conf = child; | |
1095 | |
1096 if (conf->filter == NGX_CONF_UNSET_UINT) { | |
1097 | |
1098 if (prev->filter == NGX_CONF_UNSET_UINT) { | |
1099 conf->filter = NGX_HTTP_IMAGE_OFF; | |
1100 | |
1101 } else { | |
1102 conf->filter = prev->filter; | |
1103 conf->width = prev->width; | |
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 | 1107 } |
1108 } | |
1109 | |
2848 | 1110 /* 75 is libjpeg default quality */ |
1111 ngx_conf_merge_value(conf->jpeg_quality, prev->jpeg_quality, 75); | |
1112 | |
3134 | 1113 ngx_conf_merge_value(conf->transparency, prev->transparency, 1); |
1114 | |
2788 | 1115 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
1116 1 * 1024 * 1024); | |
1117 | |
1118 return NGX_CONF_OK; | |
1119 } | |
1120 | |
1121 | |
1122 static char * | |
1123 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1124 { | |
1125 ngx_http_image_filter_conf_t *imcf = conf; | |
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 | 1132 |
1133 value = cf->args->elts; | |
1134 | |
1135 i = 1; | |
1136 | |
1137 if (cf->args->nelts == 2) { | |
1138 if (ngx_strcmp(value[i].data, "off") == 0) { | |
1139 imcf->filter = NGX_HTTP_IMAGE_OFF; | |
1140 | |
1141 } else if (ngx_strcmp(value[i].data, "test") == 0) { | |
1142 imcf->filter = NGX_HTTP_IMAGE_TEST; | |
1143 | |
1144 } else if (ngx_strcmp(value[i].data, "size") == 0) { | |
1145 imcf->filter = NGX_HTTP_IMAGE_SIZE; | |
1146 | |
1147 } else { | |
1148 goto failed; | |
1149 } | |
1150 | |
1151 return NGX_CONF_OK; | |
1152 } | |
1153 | |
1154 if (ngx_strcmp(value[i].data, "resize") == 0) { | |
1155 imcf->filter = NGX_HTTP_IMAGE_RESIZE; | |
1156 | |
1157 } else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
1158 imcf->filter = NGX_HTTP_IMAGE_CROP; | |
1159 | |
1160 } else { | |
1161 goto failed; | |
1162 } | |
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 | 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 | 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 | 1178 goto failed; |
1179 } | |
1180 | |
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 | 1190 } |
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 | 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 | 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 | 1206 goto failed; |
1207 } | |
1208 | |
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 | 1218 } |
1219 | |
1220 return NGX_CONF_OK; | |
1221 | |
1222 failed: | |
1223 | |
1224 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
1225 &value[i]); | |
1226 | |
1227 return NGX_CONF_ERROR; | |
1228 } | |
1229 | |
1230 | |
1231 static ngx_int_t | |
1232 ngx_http_image_filter_init(ngx_conf_t *cf) | |
1233 { | |
1234 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1235 ngx_http_top_header_filter = ngx_http_image_header_filter; | |
1236 | |
1237 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1238 ngx_http_top_body_filter = ngx_http_image_body_filter; | |
1239 | |
1240 return NGX_OK; | |
1241 } |