1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
// This is a roxen module. Copyright © 1999 - 2009, Roxen IS. 
// 
 
#include <module.h> 
inherit "module"; 
constant thread_safe=1; 
 
roxen.ImageCache the_cache; 
 
constant cvs_version = "$Id$"; 
constant module_type = MODULE_TAG; 
constant module_name = "Graphics: Image converter"; 
constant module_doc  = "Provides the tag <tt>&lt;cimg&gt;</tt> that can be used " 
"to convert images between different image formats."; 
 
 
mapping tagdocumentation() 
{ 
  Stdio.File file=Stdio.File(); 
  if(!file->open(__FILE__,"r")) return 0; 
  mapping doc = compile_string("#define manual\n"+file->read(), __FILE__)->tagdoc; 
  foreach(({ "cimg", "cimg-url" }), string tag) 
    doc[tag] += the_cache->documentation(tag + 
                                         " src='/internal-roxen-testimage'"); 
  return doc; 
} 
 
#ifdef manual 
constant tagdoc=(["cimg":#"<desc tag='tag'><p><short> 
 Manipulates and converts images between different image 
 formats.</short> Provides the tag <tag>cimg</tag> that makes it is 
 possible to convert, resize, crop and in other ways transform 
 images. It is possible to pass attributes, such as the alt attribute,  
 to the resulting tag by including them in the cimg tag. </p> 
</desc> 
 
<attr name='src' value='url' required='required'><p> 
 The path to the indata file.</p> 
 
<ex><cimg src='/internal-roxen-testimage'/></ex> 
</attr> 
 
<attr name='filename' value='string'><p> 
Append the filename value to the path. Recommended is not to append file suffix 
to the filename since there are settings for handling that automatically through 
this module settings. 
</p><p>This is useful if you want to have images indexed, since many search engines 
uses the filename as a description of the image.</p> 
<ex><cimg-url src='/internal-roxen-testimage' filename='Roxen Test Image'/></ex> 
</attr> 
 
<attr name='data' value='imagedata'><p> 
 Insert images from other sources, e.g. databases through entities or 
 variables.</p> 
<ex-box><emit source='sql' query='select imagedata from images where id=37'> 
<cimg data='&sql.imagedata:none;'/> 
</emit></ex-box> 
</attr> 
 
<attr name='process-all-layers'><p>Set this flag to make all image layers 
visible regardless of their original state.</p> 
</attr> 
 
<attr name='include-layers' value='layer-glob-list'><p>Comma-separated list 
of glob expressions which is matched against layer names. All matching 
layers are made visible regardless of their original state.</p> 
</attr> 
 
<attr name='exclude-layers' value='layer-glob-list'><p>Comma-separated list 
of glob expressions which is matched against layer names. All matching 
layers are hidden regardless of their original state.</p> 
</attr> 
 
<attr name='exclude-invisible-layers'><p>Set this flag to 
automatically exclude layers that are not shown in the original 
image. This is only useful in combination with the 
'process-all-layers' attribute.</p> 
</attr> 
 
<h1>Timeout</h1> 
 
<p>The generated image will by default never expire, but 
in some circumstances it may be pertinent to limit the 
time the image and its associated data is kept. Its 
possible to set an (advisory) timeout on the image data 
using the following attributes.</p> 
 
<attr name='unix-time' value='number'><p> 
Set the base expiry time to this absolute time.</p><p> 
If left out, the other attributes are relative to current time.</p> 
</attr> 
 
<attr name='years' value='number'><p> 
Add this number of years to the time this entry is valid.</p> 
</attr> 
 
<attr name='months' value='number'><p> 
Add this number of months to the time this entry is valid.</p> 
</attr> 
 
<attr name='weeks' value='number'><p> 
Add this number of weeks to the time this entry is valid.</p> 
</attr> 
 
<attr name='days' value='number'><p> 
Add this number of days to the time this entry is valid.</p> 
</attr> 
 
<attr name='hours' value='number'><p> 
Add this number of hours to the time this entry is valid.</p> 
</attr> 
 
<attr name='beats' value='number'><p> 
Add this number of beats to the time this entry is valid.</p> 
</attr> 
 
<attr name='minutes' value='number'><p> 
Add this number of minutes to the time this entry is valid.</p> 
</attr> 
 
<attr name='seconds' value='number'><p> 
Add this number of seconds to the time this entry is valid.</p> 
</attr> 
", 
 
"cimg-url":#"<desc tag='tag'><p><short> 
 This tag generates an URL to the manipulated picture.</short> 
 <tag>cimg-url</tag> takes the same attributes as <xref 
 href='cimg.tag' />, including the image cache attributes. The use for 
 the tag is to insert image-URLs into various places, e.g. a 
 submit-box.</p> 
</desc> 
 
<attr name='src' value='url' required='required'><p> 
 The path to the indata file.</p> 
 
<ex><cimg-url src='/internal-roxen-testimage'/></ex> 
</attr> 
 
<attr name='data' value='imagedata'><p> 
 Insert images from other sources, e.g. databases through entities or 
 variables.</p> 
<ex-box><emit source='sql' query='select imagedata from images where id=37'> 
  <cimg-url data='&sql.imagedata;'/> 
</emit></ex-box> 
</attr> 
 
<h1>Timeout</h1> 
 
<p>The generated image will by default never expire, but 
in some circumstances it may be pertinent to limit the 
time the image and its associated data is kept. Its 
possible to set an (advisory) timeout on the image data 
using the following attributes.</p> 
 
<attr name='unix-time' value='number'><p> 
Set the base expiry time to this absolute time.</p><p> 
If left out, the other attributes are relative to current time.</p> 
</attr> 
 
<attr name='years' value='number'><p> 
Add this number of years to the time this entry is valid.</p> 
</attr> 
 
<attr name='months' value='number'><p> 
Add this number of months to the time this entry is valid.</p> 
</attr> 
 
<attr name='weeks' value='number'><p> 
Add this number of weeks to the time this entry is valid.</p> 
</attr> 
 
<attr name='days' value='number'><p> 
Add this number of days to the time this entry is valid.</p> 
</attr> 
 
<attr name='hours' value='number'><p> 
Add this number of hours to the time this entry is valid.</p> 
</attr> 
 
<attr name='beats' value='number'><p> 
Add this number of beats to the time this entry is valid.</p> 
</attr> 
 
<attr name='minutes' value='number'><p> 
Add this number of minutes to the time this entry is valid.</p> 
</attr> 
 
<attr name='seconds' value='number'><p> 
Add this number of seconds to the time this entry is valid.</p> 
</attr>", 
 
"emit#cimg":({ #"<desc type='plugin'><p><short> 
 Entitybased version of <xref href='../graphics/cimg.tag' />.</short> 
 Takes the same attributes as <tag>cimg</tag>.</p> 
</desc> 
 
<attr name='nodata' value='yes | no'><p> 
 Controls suppression of <ent>_.data</ent> in the output. Useful for 
 reducing memory consumption in cached emit tags. The default value 
 is 'no'.</p> 
</attr>", 
 
([ 
"&_.type;":#"<desc type='entity'><p> 
 Returns the image's content-type.</p> 
</desc>", 
 
"&_.src;":#"<desc type='entity'><p> 
 Returns the path to the indata file.</p> 
</desc>", 
 
"&_.file-size;":#"<desc type='entity'><p> 
 Returns the image's file size.</p> 
</desc>", 
 
"&_.xsize;":#"<desc type='entity'><p> 
 Returns the width of the image.</p> 
</desc>", 
 
"&_.ysize;":#"<desc type='entity'><p> 
 Returns the height of the image.</p> 
</desc>", 
 
"&_.data;":#"<desc type='entity'><p> 
 Returns the imagedata given through other sources, like databases 
 through entities.</p> 
</desc>" 
]) 
 
}), 
 
]); 
#endif 
 
int do_ext; 
 
void create() 
{ 
  defvar("ext", Variable.Flag(0, VAR_MORE, 
                              "Append format to generated images", 
                              "Append the image format (.gif, .png, " 
                              ".jpg, etc) to the generated images. " 
                              "This is not necessary, but might seem " 
                              "nicer, especially to people who try " 
                              "to mirror your site.")); 
  defvar ("default_args", 
          Variable.Mapping (([]), 0, 
                            "Default Arguments", 
                            "Arguments to add implicitly to cimg/cimg-url/" 
                            "emit#cimg calls. Explicit arguments will take " 
                            "precedence over any arguments specified here.")); 
} 
 
void start() 
{ 
  //  Reuse previous cache object if possible 
  if (the_cache) { 
    //  Update reference to callback function in case we've been reloaded 
    the_cache->set_draw_function(generate_image); 
  } else { 
    the_cache = roxen.ImageCache( "cimg", generate_image ); 
  } 
  do_ext = query("ext"); 
} 
 
void stop() 
{ 
  //  Force cache object to be recreated in start() 
  destruct(the_cache); 
} 
 
mapping(string:function) query_action_buttons() { 
  return ([ "Clear Cache":flush_cache ]); 
} 
 
void flush_cache() { 
  the_cache->flush(); 
   
  //  It's possible that user code contains a number of stale URLs in 
  //  e.g. <cache> blocks so we can just as well flush the RAM cache to 
  //  reduce the risk of broken images. 
  cache.flush_memory_cache(); 
} 
 
string status() { 
  array s=the_cache->status(); 
  return sprintf("<b>Images in cache:</b> %d images<br />\n" 
                 "<b>Cache size:</b> %s", 
                 s[0], Roxen.sizetostring(s[1])); 
} 
 
array(Image.Layer)|mapping generate_image( mapping args, RequestID id ) 
{ 
  array layers; 
 
  //  Photoshop layers: don't let individual layers expand the image 
  //  beyond the bounds of the overall image. 
  mapping opts = ([ "crop_to_bounds" : 1 ]); 
   
  if( args["process-all-layers"] ) 
    opts->draw_all_layers = 1; 
 
  if( args["jpeg-shrink" ] ) 
  { 
    opts->scale_denom = (int)args["jpeg-shrink" ]; 
    opts->scale_num = 1; 
  } 
   
  if( args->data ) 
    layers = roxen.decode_layers( args->data, opts ); 
  else 
  { 
    mixed tmp; 
#if constant(Sitebuilder) && constant(Sitebuilder.sb_start_use_imagecache) 
    //  Let SiteBuilder get a chance to decode its argument data 
    if (Sitebuilder.sb_start_use_imagecache) { 
      Sitebuilder.sb_start_use_imagecache(args, id); 
      tmp = roxen.load_layers(args->src, id, opts); 
      Sitebuilder.sb_end_use_imagecache(args, id); 
    } else 
#endif 
    { 
      tmp = roxen.load_layers(args->src, id, opts); 
    } 
     
    if (mappingp(tmp)) { 
      if (tmp->error == Protocols.HTTP.HTTP_UNAUTH) 
        return tmp; 
      else 
        layers = 0; 
    } 
    else layers = tmp; 
  } 
 
  if(!layers) 
  { 
    if( args->data ) 
      RXML.run_error("Failed to decode specified data\n"); 
    else 
      RXML.run_error("Failed to load specified image [%O]\n", args->src); 
  } 
 
  if (!sizeof(filter(layers->image(), objectp))) 
    RXML.run_error("Failed to decode layers in specified image [%O]\n", 
                   args->src); 
  if(!args["exclude-invisible-layers"]) 
     layers->set_misc_value( "visible",1 ); 
  foreach( layers, Image.Layer lay ) 
    if( !lay->get_misc_value( "name" ) ) 
      lay->set_misc_value( "name", "Background" ); 
 
  if( args["exclude-layers"] ) 
  { 
    foreach( args["exclude-layers"] / ",", string match ) 
      foreach( layers, Image.Layer lay ) 
        if( glob( match, lay->get_misc_value( "name" ) ) ) 
          lay->set_misc_value( "visible", 0 ); 
  } 
 
  if( args["include-layers"] ) 
  { 
    foreach( args["include-layers"] / ",", string match ) 
      foreach( layers, Image.Layer lay ) 
        if( glob( match, lay->get_misc_value( "name" ) ) ) 
          lay->set_misc_value( "visible", 1 ); 
  } 
 
  array res = ({}); 
  foreach( layers, Image.Layer l ) 
  { 
    if( l->get_misc_value( "visible" ) ) 
      res += ({ l }); 
  } 
  return res; 
} 
 
mapping find_internal( string f, RequestID id ) 
{ 
  // It's not enough to 
  //  1. Only do this check when the ext flag is set, old URLs might 
  //     live in caches 
  // 
  //  2. Check str[-4] for '.', consider .jpeg .tiff etc. 
  // 
  //  3. Also handle / if filename attribute is used in either tag 
  // 
  // However, . is not a valid character in the ID, so just cutting at 
  // the first one works as well as also cutting at the first /. 
  sscanf (f, "%[^./]", f); 
  return the_cache->http_file_answer( f, id ); 
} 
 
mapping get_my_args( mapping args, RequestID id ) 
{ 
  mapping a= 
  ([ 
    "quant":args->quant, 
    "crop":args->crop, 
    "format":args->format, 
    "maxwidth":args->maxwidth, 
    "maxheight":args->maxheight, 
    "scale":args->scale, 
    "dither":args->dither, 
    "gamma":args->gamma, 
    "data":args->data, 
  ]); 
 
  if( args->src ) { 
      a->src = Roxen.fix_relative( args->src, id ); 
      array(int)|Stat st = (id->conf->try_stat_file(a->src, id)); 
      if (st) 
      { 
        string fn = id->conf->real_file( a->src, id ); 
        if( fn ) Roxen.add_cache_stat_callback( id, fn, st[ST_MTIME] ); 
        a->mtime = (string) (a->stat = st[ST_MTIME]); 
        a->filesize = (string) st[ST_SIZE]; 
         
#if constant(Sitebuilder) && constant(Sitebuilder.sb_start_use_imagecache) 
      //  The file we called try_stat_file() on above may be a SiteBuilder 
        //  file. If so we need to extend the argument data with e.g. 
        //  current language fork. 
        if (Sitebuilder.sb_prepare_imagecache) 
          a = Sitebuilder.sb_prepare_imagecache(a, a->src, id); 
#endif 
      } 
  } 
 
  a["background-color"] = id->misc->defines->bgcolor || "#eeeeee"; 
 
  foreach( glob( "*-*", indices(args)), string n ) { 
    if (!has_prefix(n, "data-")) 
      a[n] = args[n]; 
  } 
 
  return a; 
} 
 
mapping check_args( mapping args ) 
{ 
  args = query ("default_args") + args; 
  if( !args->format ) 
    args->format = "png"; 
  if( !(args->src || args->data) ) 
    RXML.parse_error("Required attribute 'src' or 'data' missing\n"); 
  if (args->src == "") 
    RXML.parse_error("Attribute 'src' cannot be empty\n"); 
  if( args->src && args->data ) 
    RXML.parse_error("Only one of 'src' and 'data' may be specified\n"); 
 
  return args; 
} 
 
class TagCimgplugin 
{ 
  inherit RXML.Tag; 
  constant name = "emit"; 
  constant plugin_name = "cimg"; 
 
  array get_dataset( mapping args, RequestID id ) 
  { 
    mapping res = ([ ]); 
    mapping a = get_my_args( check_args( args ), id ); 
    string data; 
 
    int timeout = Roxen.timeout_dequantifier(args); 
 
      // Store misc->cacheable since the image cache can raise it and 
      // disable protocol cache. 
      int cacheable = id->misc->cacheable; 
      int no_proto_cache = id->misc->no_proto_cache; 
#ifdef DEBUG_CACHEABLE 
      report_debug("%s:%d saved cacheable flags\n", __FILE__, __LINE__); 
#endif 
      res->src=(query_absolute_internal_location(id)+ 
                the_cache->store( a, id, timeout )); 
      if(args->filename && sizeof(args->filename)) 
        res->src += "/" + Roxen.http_encode_url(args->filename); 
      if(do_ext) 
        res->src += "." + a->format; 
      data = the_cache->data( a, id , 0 ); 
      res["file-size"] = strlen(data); 
      res["file-size-kb"] = strlen(data)/1024; 
      if (lower_case(args->nodata || "no") == "no") 
        res["data"] = data; 
      res |= the_cache->metadata( a, id, 0, timeout ); // enforce generation 
#ifdef DEBUG_CACHEABLE 
      report_debug("%s:%d restored cacheable flags\n", __FILE__, __LINE__); 
#endif 
      id->misc->cacheable = cacheable; 
      id->misc->no_proto_cache = no_proto_cache; 
      return ({ res }); 
  } 
} 
 
class TagCImg  
{ 
  inherit RXML.Tag; 
  constant name = "cimg"; 
  constant flags = RXML.FLAG_EMPTY_ELEMENT; 
 
  class Frame 
  { 
    inherit RXML.Frame; 
 
    array do_return(RequestID id)  
    { 
      int timeout = Roxen.timeout_dequantifier(args); 
      mapping a = get_my_args( check_args( args ), id ); 
      args -= a; 
      string ext = ""; 
      string filename = ""; 
      if(args->filename && sizeof(args->filename)) 
        filename += "/" + Roxen.http_encode_url(m_delete(args, "filename")); 
      if(do_ext) 
        ext = "." + a->format; 
      args->src = query_absolute_internal_location( id ) 
        + the_cache->store( a, id, timeout ) + filename + ext; 
      int no_draw = !id->misc->generate_images; 
      mapping size; 
      if( !args->width && !args->height 
          && (size = the_cache->metadata( a, id, no_draw, timeout )) ) 
      { 
        // image in cache (no_draw above prevents generation on-the-fly) 
        args->width = size->xsize; 
        args->height = size->ysize; 
      } 
      int xml=!args->noxml; 
      m_delete(args, "noxml"); 
      result = Roxen.make_tag( "img", args, xml ); 
      return 0; 
    } 
  } 
} 
 
class TagCImgURL { 
  inherit RXML.Tag; 
  constant name = "cimg-url"; 
  constant flags = RXML.FLAG_EMPTY_ELEMENT; 
 
  class Frame  
  { 
    inherit RXML.Frame; 
 
    array do_return(RequestID id) 
    { 
      int timeout = Roxen.timeout_dequantifier(args); 
      string filename = ""; 
      mapping a = get_my_args (check_args (args), id); 
       
      if(args->filename && sizeof(args->filename)) 
        filename = "/" + Roxen.http_encode_url(args->filename); 
      result = query_absolute_internal_location(id) 
        + the_cache->store(a, id, timeout) 
             + filename 
             + (do_ext ? "." + a->format : ""); 
      return 0; 
    } 
  } 
}