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