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