Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 3592:fdd0943b277d stable-0.7
merge r3488:
revert partially r1555 and fix the error "memcached sent invalid trailer"
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 07 Jun 2010 11:49:03 +0000 |
parents | d87088142876 |
children |
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 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
43 ngx_flag_t transparency; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
44 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
45 ngx_http_complex_value_t *wcv; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
46 ngx_http_complex_value_t *hcv; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
61 ngx_uint_t max_width; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
62 ngx_uint_t max_height; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
63 |
2788 | 64 ngx_uint_t phase; |
65 ngx_uint_t type; | |
3576 | 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); | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
92 static ngx_uint_t ngx_http_image_filter_get_value(ngx_http_request_t *r, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
93 ngx_http_complex_value_t *cv, ngx_uint_t v); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
121 { ngx_string("image_filter_transparency"), |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
122 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
123 ngx_conf_set_flag_slot, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
124 NGX_HTTP_LOC_CONF_OFFSET, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
125 offsetof(ngx_http_image_filter_conf_t, transparency), |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
126 NULL }, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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' | |
2943 | 405 && p[5] == 'a') |
2788 | 406 { |
2943 | 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) { | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
489 return ngx_http_image_json(r, rc == NGX_OK ? ctx : NULL); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
490 } |
2788 | 491 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
492 ctx->max_width = ngx_http_image_filter_get_value(r, conf->wcv, conf->width); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
493 if (ctx->max_width == 0) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
494 return NULL; |
2788 | 495 } |
496 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
497 ctx->max_height = ngx_http_image_filter_get_value(r, conf->hcv, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
498 conf->height); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
499 if (ctx->max_height == 0) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
500 return NULL; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
501 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
502 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
503 if (rc == NGX_OK |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
504 && ctx->width <= ctx->max_width |
3576 | 505 && ctx->height <= ctx->max_height |
506 && !ctx->force) | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
507 { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
508 return ngx_http_image_asis(r, ctx); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
509 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
510 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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; | |
3576 | 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; | |
3576 | 617 width = 0; |
618 height = 0; | |
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, | |
3576 | 626 "JPEG: %02xd %02xd", p[0], p[1]); |
2788 | 627 |
628 p++; | |
629 | |
3576 | 630 if ((*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3 |
631 || *p == 0xc9 || *p == 0xca || *p == 0xcb) | |
632 && (width == 0 || height == 0)) | |
2788 | 633 { |
3576 | 634 width = p[6] * 256 + p[7]; |
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 | |
3576 | 641 len = p[1] * 256 + p[2]; |
642 | |
643 if (*p >= 0xe1 && *p <= 0xef) { | |
644 /* application data, e.g., EXIF, Adobe XMP, etc. */ | |
645 app += len; | |
646 } | |
647 | |
648 p += len; | |
2788 | 649 |
650 continue; | |
651 } | |
652 | |
653 p++; | |
654 } | |
655 | |
3576 | 656 if (width == 0 || height == 0) { |
657 return NGX_DECLINED; | |
658 } | |
2788 | 659 |
3576 | 660 if (ctx->length / 20 < app) { |
661 /* force conversion if application data consume more than 5% */ | |
662 ctx->force = 1; | |
663 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
664 "app data size: %uz", app); | |
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 { | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
709 int sx, sy, dx, dy, ox, oy, size, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
710 colors, palette, transparent, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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 | |
3576 | 730 if (!ctx->force |
731 && (ngx_uint_t) sx <= ctx->max_width | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
732 && (ngx_uint_t) sy <= ctx->max_height) |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
733 { |
2788 | 734 gdImageDestroy(src); |
735 return ngx_http_image_asis(r, ctx); | |
736 } | |
737 | |
738 colors = gdImageColorsTotal(src); | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
739 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
740 if (colors && conf->transparency) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
741 transparent = gdImageGetTransparent(src); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
742 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
743 if (transparent != -1) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
744 palette = colors; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
745 red = gdImageRed(src, transparent); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
746 green = gdImageGreen(src, transparent); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
747 blue = gdImageBlue(src, transparent); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
748 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
749 goto transparent; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
750 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
751 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
752 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
753 palette = 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
754 transparent = -1; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
755 red = 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
756 green = 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
757 blue = 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
758 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
759 transparent: |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
760 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
761 gdImageColorTransparent(src, -1); |
2788 | 762 |
763 dx = sx; | |
764 dy = sy; | |
765 | |
766 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
767 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
768 if ((ngx_uint_t) dx > ctx->max_width) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
769 dy = dy * ctx->max_width / dx; |
2788 | 770 dy = dy ? dy : 1; |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
771 dx = ctx->max_width; |
2788 | 772 } |
773 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
774 if ((ngx_uint_t) dy > ctx->max_height) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
775 dx = dx * ctx->max_height / dy; |
2788 | 776 dx = dx ? dx : 1; |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
786 if ((ngx_uint_t) (dx * 100 / dy) |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
787 < ctx->max_width * 100 / ctx->max_height) |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
788 { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
789 if ((ngx_uint_t) dx > ctx->max_width) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
790 dy = dy * ctx->max_width / dx; |
2788 | 791 dy = dy ? dy : 1; |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
792 dx = ctx->max_width; |
2788 | 793 resize = 1; |
794 } | |
795 | |
796 } else { | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
797 if ((ngx_uint_t) dy > ctx->max_height) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
798 dx = dx * ctx->max_height / dy; |
2788 | 799 dx = dx ? dx : 1; |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
800 dy = ctx->max_height; |
2788 | 801 resize = 1; |
802 } | |
803 } | |
804 } | |
805 | |
806 if (resize) { | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
807 dst = ngx_http_image_new(r, dx, dy, palette); |
2788 | 808 if (dst == NULL) { |
809 gdImageDestroy(src); | |
810 return NULL; | |
811 } | |
812 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
813 if (colors == 0) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
814 gdImageSaveAlpha(dst, 1); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
815 gdImageAlphaBlending(dst, 0); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
816 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
817 |
2788 | 818 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); |
819 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
820 if (colors) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
821 gdImageTrueColorToPalette(dst, 1, 256); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
822 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
834 if ((ngx_uint_t) dx > ctx->max_width) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
835 ox = dx - ctx->max_width; |
2788 | 836 |
837 } else { | |
838 ox = 0; | |
839 } | |
840 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
841 if ((ngx_uint_t) dy > ctx->max_height) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
864 if (colors == 0) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
865 gdImageSaveAlpha(dst, 1); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
866 gdImageAlphaBlending(dst, 0); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
867 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
868 |
2788 | 869 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); |
870 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
871 if (colors) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
872 gdImageTrueColorToPalette(dst, 1, 256); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
873 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
874 |
2788 | 875 gdImageDestroy(src); |
876 } | |
877 } | |
878 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
879 if (transparent != -1 && colors) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
880 gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue)); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1035 static ngx_uint_t |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1036 ngx_http_image_filter_get_value(ngx_http_request_t *r, |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1037 ngx_http_complex_value_t *cv, ngx_uint_t v) |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1038 { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1039 ngx_str_t val; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1040 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1041 if (cv == NULL) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1042 return v; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1043 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1044 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1045 if (ngx_http_complex_value(r, cv, &val) != NGX_OK) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1046 return 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1047 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1048 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1049 return ngx_http_image_filter_value(&val); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1050 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1051 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1052 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1053 static ngx_uint_t |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1054 ngx_http_image_filter_value(ngx_str_t *value) |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1055 { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1056 ngx_int_t n; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1057 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1058 if (value->len == 1 && value->data[0] == '-') { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1059 return (ngx_uint_t) -1; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1060 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1061 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1062 n = ngx_atoi(value->data, value->len); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1063 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1064 if (n > 0) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1065 return (ngx_uint_t) n; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1066 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1067 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1068 return 0; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1069 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1070 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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) { | |
3237
2efa8d2fcde1
merge r2903, r2911, r2912, r3002:
Igor Sysoev <igor@sysoev.ru>
parents:
2943
diff
changeset
|
1079 return NULL; |
2788 | 1080 } |
1081 | |
1082 conf->filter = NGX_CONF_UNSET_UINT; | |
2848 | 1083 conf->jpeg_quality = NGX_CONF_UNSET; |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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; | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1106 conf->wcv = prev->wcv; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1114 ngx_conf_merge_value(conf->transparency, prev->transparency, 1); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1128 ngx_str_t *value; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1129 ngx_int_t n; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1130 ngx_uint_t i; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1131 ngx_http_complex_value_t cv; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1165 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1166 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1167 ccv.cf = cf; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1168 ccv.value = &value[++i]; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1169 ccv.complex_value = &cv; |
2788 | 1170 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1171 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1172 return NGX_CONF_ERROR; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1173 } |
2788 | 1174 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1175 if (cv.lengths == NULL) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1176 n = ngx_http_image_filter_value(&value[i]); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1177 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1178 if (n == 0) { |
2788 | 1179 goto failed; |
1180 } | |
1181 | |
1182 imcf->width = (ngx_uint_t) n; | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1183 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1184 } else { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1185 imcf->wcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1186 if (imcf->wcv == NULL) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1187 return NGX_CONF_ERROR; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1188 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1189 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1190 *imcf->wcv = cv; |
2788 | 1191 } |
1192 | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1193 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1194 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1195 ccv.cf = cf; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1196 ccv.value = &value[++i]; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1197 ccv.complex_value = &cv; |
2788 | 1198 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1199 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1200 return NGX_CONF_ERROR; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1201 } |
2788 | 1202 |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1203 if (cv.lengths == NULL) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1204 n = ngx_http_image_filter_value(&value[i]); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1205 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1206 if (n == 0) { |
2788 | 1207 goto failed; |
1208 } | |
1209 | |
1210 imcf->height = (ngx_uint_t) n; | |
3244
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1211 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1212 } else { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1213 imcf->hcv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1214 if (imcf->hcv == NULL) { |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1215 return NGX_CONF_ERROR; |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1216 } |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
diff
changeset
|
1217 |
8b2d478de54b
merge r2999, r3118, r3134, r3135:
Igor Sysoev <igor@sysoev.ru>
parents:
3237
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 } |