Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 3124:53a02ff924e8
release-0.8.15 tag
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 14 Sep 2009 13:07:18 +0000 |
parents | e2a510ac53db |
children | 28a6aa24b453 |
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, | |
3117 | 682 colors, transparent, red, green, blue, size; |
2788 | 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 | |
3117 | 711 if (transparent != -1 && colors) { |
712 red = gdImageRed(src, transparent); | |
713 green = gdImageGreen(src, transparent); | |
714 blue = gdImageBlue(src, transparent); | |
715 gdImageColorTransparent(src, -1); | |
716 | |
717 } else { | |
718 red = 0; green = 0; blue = 0; | |
719 } | |
720 | |
2788 | 721 dx = sx; |
722 dy = sy; | |
723 | |
724 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
725 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
726 if ((ngx_uint_t) dx > ctx->max_width) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
727 dy = dy * ctx->max_width / dx; |
2788 | 728 dy = dy ? dy : 1; |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
729 dx = ctx->max_width; |
2788 | 730 } |
731 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
732 if ((ngx_uint_t) dy > ctx->max_height) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
733 dx = dx * ctx->max_height / dy; |
2788 | 734 dx = dx ? dx : 1; |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
735 dy = ctx->max_height; |
2788 | 736 } |
737 | |
738 resize = 1; | |
739 | |
740 } else { /* NGX_HTTP_IMAGE_CROP */ | |
741 | |
742 resize = 0; | |
743 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
744 if ((ngx_uint_t) (dx * 100 / dy) |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
745 < ctx->max_width * 100 / ctx->max_height) |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
746 { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
747 if ((ngx_uint_t) dx > ctx->max_width) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
748 dy = dy * ctx->max_width / dx; |
2788 | 749 dy = dy ? dy : 1; |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
750 dx = ctx->max_width; |
2788 | 751 resize = 1; |
752 } | |
753 | |
754 } else { | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
755 if ((ngx_uint_t) dy > ctx->max_height) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
756 dx = dx * ctx->max_height / dy; |
2788 | 757 dx = dx ? dx : 1; |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
758 dy = ctx->max_height; |
2788 | 759 resize = 1; |
760 } | |
761 } | |
762 } | |
763 | |
764 if (resize) { | |
765 dst = ngx_http_image_new(r, dx, dy, colors); | |
766 if (dst == NULL) { | |
767 gdImageDestroy(src); | |
768 return NULL; | |
769 } | |
770 | |
771 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); | |
772 | |
773 gdImageDestroy(src); | |
774 | |
775 } else { | |
776 dst = src; | |
777 } | |
778 | |
779 if (conf->filter == NGX_HTTP_IMAGE_CROP) { | |
780 | |
781 src = dst; | |
782 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
783 if ((ngx_uint_t) dx > ctx->max_width) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
784 ox = dx - ctx->max_width; |
2788 | 785 |
786 } else { | |
787 ox = 0; | |
788 } | |
789 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
790 if ((ngx_uint_t) dy > ctx->max_height) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
791 oy = dy - ctx->max_height; |
2788 | 792 |
793 } else { | |
794 oy = 0; | |
795 } | |
796 | |
797 if (ox || oy) { | |
798 | |
799 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); | |
800 | |
801 if (dst == NULL) { | |
802 gdImageDestroy(src); | |
803 return NULL; | |
804 } | |
805 | |
806 ox /= 2; | |
807 oy /= 2; | |
808 | |
809 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
810 "image crop: %d x %d @ %d x %d", | |
811 dx, dy, ox, oy); | |
812 | |
813 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); | |
814 | |
815 gdImageDestroy(src); | |
816 } | |
817 } | |
818 | |
3117 | 819 if (transparent != -1 && colors) { |
820 gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue)); | |
821 } | |
2788 | 822 |
823 out = ngx_http_image_out(r, ctx->type, dst, &size); | |
824 | |
825 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
826 "image: %d x %d %d", sx, sy, colors); | |
827 | |
828 gdImageDestroy(dst); | |
829 ngx_pfree(r->pool, ctx->image); | |
830 | |
831 if (out == NULL) { | |
832 return NULL; | |
833 } | |
834 | |
835 cln = ngx_pool_cleanup_add(r->pool, 0); | |
836 if (cln == NULL) { | |
837 gdFree(out); | |
838 return NULL; | |
839 } | |
840 | |
841 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
842 if (b == NULL) { | |
843 gdFree(out); | |
844 return NULL; | |
845 } | |
846 | |
847 cln->handler = ngx_http_image_cleanup; | |
848 cln->data = out; | |
849 | |
850 b->pos = out; | |
851 b->last = out + size; | |
852 b->memory = 1; | |
853 b->last_buf = 1; | |
854 | |
855 ngx_http_image_length(r, b); | |
856 | |
857 return b; | |
858 } | |
859 | |
860 | |
861 static gdImagePtr | |
862 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
863 { | |
864 char *failed; | |
865 gdImagePtr img; | |
866 | |
867 img = NULL; | |
868 | |
869 switch (ctx->type) { | |
870 | |
871 case NGX_HTTP_IMAGE_JPEG: | |
872 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image); | |
873 failed = "gdImageCreateFromJpegPtr() failed"; | |
874 break; | |
875 | |
876 case NGX_HTTP_IMAGE_GIF: | |
877 img = gdImageCreateFromGifPtr(ctx->length, ctx->image); | |
878 failed = "gdImageCreateFromGifPtr() failed"; | |
879 break; | |
880 | |
881 case NGX_HTTP_IMAGE_PNG: | |
882 img = gdImageCreateFromPngPtr(ctx->length, ctx->image); | |
883 failed = "gdImageCreateFromPngPtr() failed"; | |
884 break; | |
885 | |
886 default: | |
887 failed = "unknown image type"; | |
888 break; | |
889 } | |
890 | |
891 if (img == NULL) { | |
892 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
893 } | |
894 | |
895 return img; | |
896 } | |
897 | |
898 | |
899 static gdImagePtr | |
900 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) | |
901 { | |
902 gdImagePtr img; | |
903 | |
904 if (colors == 0) { | |
905 img = gdImageCreateTrueColor(w, h); | |
906 | |
907 if (img == NULL) { | |
908 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
909 "gdImageCreateTrueColor() failed"); | |
910 return NULL; | |
911 } | |
912 | |
913 } else { | |
914 img = gdImageCreate(w, h); | |
915 | |
916 if (img == NULL) { | |
917 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
918 "gdImageCreate() failed"); | |
919 return NULL; | |
920 } | |
921 } | |
922 | |
923 return img; | |
924 } | |
925 | |
926 | |
927 static u_char * | |
928 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img, | |
929 int *size) | |
930 { | |
2848 | 931 char *failed; |
932 u_char *out; | |
933 ngx_http_image_filter_conf_t *conf; | |
2788 | 934 |
935 out = NULL; | |
936 | |
937 switch (type) { | |
938 | |
939 case NGX_HTTP_IMAGE_JPEG: | |
2848 | 940 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); |
941 out = gdImageJpegPtr(img, size, conf->jpeg_quality); | |
2788 | 942 failed = "gdImageJpegPtr() failed"; |
943 break; | |
944 | |
945 case NGX_HTTP_IMAGE_GIF: | |
946 out = gdImageGifPtr(img, size); | |
947 failed = "gdImageGifPtr() failed"; | |
948 break; | |
949 | |
950 case NGX_HTTP_IMAGE_PNG: | |
951 out = gdImagePngPtr(img, size); | |
952 failed = "gdImagePngPtr() failed"; | |
953 break; | |
954 | |
955 default: | |
956 failed = "unknown image type"; | |
957 break; | |
958 } | |
959 | |
960 if (out == NULL) { | |
961 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
962 } | |
963 | |
964 return out; | |
965 } | |
966 | |
967 | |
968 static void | |
969 ngx_http_image_cleanup(void *data) | |
970 { | |
971 gdFree(data); | |
972 } | |
973 | |
974 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
975 static ngx_uint_t |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
976 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
|
977 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
|
978 { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
979 ngx_str_t val; |
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 if (cv == NULL) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
982 return v; |
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 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
985 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
|
986 return 0; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
987 } |
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 return ngx_http_image_filter_value(&val); |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
990 } |
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 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
993 static ngx_uint_t |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
994 ngx_http_image_filter_value(ngx_str_t *value) |
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 ngx_int_t n; |
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 if (value->len == 1 && value->data[0] == '-') { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
999 return (ngx_uint_t) -1; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1000 } |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1001 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1002 n = ngx_atoi(value->data, value->len); |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1003 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1004 if (n > 0) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1005 return (ngx_uint_t) n; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1006 } |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1007 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1008 return 0; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1009 } |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1010 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1011 |
2788 | 1012 static void * |
1013 ngx_http_image_filter_create_conf(ngx_conf_t *cf) | |
1014 { | |
1015 ngx_http_image_filter_conf_t *conf; | |
1016 | |
1017 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t)); | |
1018 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
|
1019 return NULL; |
2788 | 1020 } |
1021 | |
1022 conf->filter = NGX_CONF_UNSET_UINT; | |
2848 | 1023 conf->jpeg_quality = NGX_CONF_UNSET; |
2788 | 1024 conf->buffer_size = NGX_CONF_UNSET_SIZE; |
1025 | |
1026 return conf; | |
1027 } | |
1028 | |
1029 | |
1030 static char * | |
1031 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1032 { | |
1033 ngx_http_image_filter_conf_t *prev = parent; | |
1034 ngx_http_image_filter_conf_t *conf = child; | |
1035 | |
1036 if (conf->filter == NGX_CONF_UNSET_UINT) { | |
1037 | |
1038 if (prev->filter == NGX_CONF_UNSET_UINT) { | |
1039 conf->filter = NGX_HTTP_IMAGE_OFF; | |
1040 | |
1041 } else { | |
1042 conf->filter = prev->filter; | |
1043 conf->width = prev->width; | |
1044 conf->height = prev->height; | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1045 conf->wcv = prev->wcv; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1046 conf->hcv = prev->hcv; |
2788 | 1047 } |
1048 } | |
1049 | |
2848 | 1050 /* 75 is libjpeg default quality */ |
1051 ngx_conf_merge_value(conf->jpeg_quality, prev->jpeg_quality, 75); | |
1052 | |
2788 | 1053 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
1054 1 * 1024 * 1024); | |
1055 | |
1056 return NGX_CONF_OK; | |
1057 } | |
1058 | |
1059 | |
1060 static char * | |
1061 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1062 { | |
1063 ngx_http_image_filter_conf_t *imcf = conf; | |
1064 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1065 ngx_str_t *value; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1066 ngx_int_t n; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1067 ngx_uint_t i; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1068 ngx_http_complex_value_t cv; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1069 ngx_http_compile_complex_value_t ccv; |
2788 | 1070 |
1071 value = cf->args->elts; | |
1072 | |
1073 i = 1; | |
1074 | |
1075 if (cf->args->nelts == 2) { | |
1076 if (ngx_strcmp(value[i].data, "off") == 0) { | |
1077 imcf->filter = NGX_HTTP_IMAGE_OFF; | |
1078 | |
1079 } else if (ngx_strcmp(value[i].data, "test") == 0) { | |
1080 imcf->filter = NGX_HTTP_IMAGE_TEST; | |
1081 | |
1082 } else if (ngx_strcmp(value[i].data, "size") == 0) { | |
1083 imcf->filter = NGX_HTTP_IMAGE_SIZE; | |
1084 | |
1085 } else { | |
1086 goto failed; | |
1087 } | |
1088 | |
1089 return NGX_CONF_OK; | |
1090 } | |
1091 | |
1092 if (ngx_strcmp(value[i].data, "resize") == 0) { | |
1093 imcf->filter = NGX_HTTP_IMAGE_RESIZE; | |
1094 | |
1095 } else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
1096 imcf->filter = NGX_HTTP_IMAGE_CROP; | |
1097 | |
1098 } else { | |
1099 goto failed; | |
1100 } | |
1101 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1102 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
|
1103 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1104 ccv.cf = cf; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1105 ccv.value = &value[++i]; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1106 ccv.complex_value = &cv; |
2788 | 1107 |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1108 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1109 return NGX_CONF_ERROR; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1110 } |
2788 | 1111 |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1112 if (cv.lengths == NULL) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1113 n = ngx_http_image_filter_value(&value[i]); |
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 if (n == 0) { |
2788 | 1116 goto failed; |
1117 } | |
1118 | |
1119 imcf->width = (ngx_uint_t) n; | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1120 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1121 } else { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1122 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
|
1123 if (imcf->wcv == NULL) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1124 return NGX_CONF_ERROR; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1125 } |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1126 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1127 *imcf->wcv = cv; |
2788 | 1128 } |
1129 | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1130 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
|
1131 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1132 ccv.cf = cf; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1133 ccv.value = &value[++i]; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1134 ccv.complex_value = &cv; |
2788 | 1135 |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1136 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1137 return NGX_CONF_ERROR; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1138 } |
2788 | 1139 |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1140 if (cv.lengths == NULL) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1141 n = ngx_http_image_filter_value(&value[i]); |
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 if (n == 0) { |
2788 | 1144 goto failed; |
1145 } | |
1146 | |
1147 imcf->height = (ngx_uint_t) n; | |
2998
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1148 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1149 } else { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1150 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
|
1151 if (imcf->hcv == NULL) { |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1152 return NGX_CONF_ERROR; |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1153 } |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1154 |
fa8503fd3647
variables support in image_filter
Igor Sysoev <igor@sysoev.ru>
parents:
2918
diff
changeset
|
1155 *imcf->hcv = cv; |
2788 | 1156 } |
1157 | |
1158 return NGX_CONF_OK; | |
1159 | |
1160 failed: | |
1161 | |
1162 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
1163 &value[i]); | |
1164 | |
1165 return NGX_CONF_ERROR; | |
1166 } | |
1167 | |
1168 | |
1169 static ngx_int_t | |
1170 ngx_http_image_filter_init(ngx_conf_t *cf) | |
1171 { | |
1172 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1173 ngx_http_top_header_filter = ngx_http_image_header_filter; | |
1174 | |
1175 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1176 ngx_http_top_body_filter = ngx_http_image_body_filter; | |
1177 | |
1178 return NGX_OK; | |
1179 } |