f668602000-03-02Martin Nilsson // This is a roxen module. Copyright © 1999 - 2000, Roxen IS.
2e0a351999-12-09Martin Nilsson //
37bdda1999-05-18Per Hedbor #include <module.h> inherit "module"; inherit "roxenlib";
3b85e91999-11-15Per Hedbor constant thread_safe=1;
37bdda1999-05-18Per Hedbor  roxen.ImageCache the_cache;
9e5d9c2000-07-05Per Hedbor constant cvs_version = "$Id: cimg.pike,v 1.20 2000/07/05 12:36:23 per Exp $";
c043f82000-02-10Martin Nilsson constant module_type = MODULE_PARSER; constant module_name = "Image converter";
f6589a2000-06-01Martin Nilsson constant module_doc = "Provides the tag <tt>&lt;cimg&gt;</tt> that can be used " "to convert images between different image formats.";
6992802000-05-18Kenneth Johansson 
f6589a2000-06-01Martin Nilsson mapping tagdocumentation() { Stdio.File file=Stdio.File(); if(!file->open(__FILE__,"r")) return 0; mapping doc=compile_string("#define manual\n"+file->read())->tagdoc; string imagecache=the_cache->documentation("cimg src='internal-roxen-robodog'");
6992802000-05-18Kenneth Johansson 
f6589a2000-06-01Martin Nilsson  doc->cimg+=imagecache; doc["cimg-url"]=imagecache; return doc; }
6992802000-05-18Kenneth Johansson 
f6589a2000-06-01Martin Nilsson #ifdef manual
6992802000-05-18Kenneth Johansson constant tagdoc=(["cimg":#" <desc tag><short>Convert and manuipulate images between different image formats.</short> The <tag>cimg</tag> makes it is possible to convert, alter size, and transform images between many formats.</desc> <attr name='src' value='uri' required> The path to the indata file. <ex><cimg src='internal-roxen-robodog'/></ex> </attr> <attr name='data' value='imagedata'> Insert images from other sources, e.g. databases through entities or variables. <ex type='box'>
216db12000-06-01Kenneth Johansson &lt;emit source='sql' query='select imagedata from images where id=37'&gt; &lt;cimg data='&sql.imagedata;'/&gt; &lt/emit&gt;
6992802000-05-18Kenneth Johansson </ex>
f6589a2000-06-01Martin Nilsson </attr>",
6992802000-05-18Kenneth Johansson  "cimg-url":#"<desc tag><short>This tag generates an URI to the manipulated picture.</short> <tag>cimg-url</tag> takes the same attributes as <tag>cimg</tag> including the image cache attributes. The use for the tag is to insert image-URI's into various places, e.g. a submit-box. </desc> <attr name='src' value='uri' required> The path to the indata file. <ex><cimg-url src='internal-roxen-robodog'/></ex> </attr> <attr name='data' value='imagedata'> Insert images from other sources, e.g. databases through entities or variables. <ex type='box'>
216db12000-06-01Kenneth Johansson &lt;emit source='sql' query='select imagedata from images where id=37'&gt; &lt;cimg data='&sql.imagedata;'/&gt; &lt/emit&gt;
6992802000-05-18Kenneth Johansson </ex>
f6589a2000-06-01Martin Nilsson </attr>", ]);
2e0a351999-12-09Martin Nilsson #endif
37bdda1999-05-18Per Hedbor void start() { the_cache = roxen.ImageCache( "cimg", generate_image ); } mapping generate_image( mapping args, RequestID id ) {
34a9b81999-08-06Peter Bortas  if( args->data ) return roxen.low_decode_image( args->data ); else return roxen.low_load_image( args->src, id );
37bdda1999-05-18Per Hedbor } mapping find_internal( string f, RequestID id ) { return the_cache->http_file_answer( f, id ); }
34a9b81999-08-06Peter Bortas mapping get_my_args( mapping args, object id )
37bdda1999-05-18Per Hedbor {
36eceb2000-01-30Per Hedbor  mapping a= ([
34a9b81999-08-06Peter Bortas  "src":(args->src?fix_relative( args->src, id ):0),
37bdda1999-05-18Per Hedbor  "quant":args->quant,
77926b2000-04-11Per Hedbor  "crop":args->crop,
c526921999-05-18Per Hedbor  "format":args->format, "maxwidth":args->maxwidth, "maxheight":args->maxheight, "scale":args->scale, "dither":args->dither,
b941aa1999-06-25Per Hedbor  "gamma":args->gamma,
34a9b81999-08-06Peter Bortas  "data":args->data,
37bdda1999-05-18Per Hedbor  ]);
77926b2000-04-11Per Hedbor 
9e5d9c2000-07-05Per Hedbor  if( a->src ) catch { a->stat = (id->conf->stat_file( a->src,id ) || file_stat( a->src ))[ ST_MTIME ]; }; a["background-color"] = id->misc->defines->bgcolor || "#eeeeee";
77926b2000-04-11Per Hedbor 
c526921999-05-18Per Hedbor  foreach( glob( "*-*", indices(args)), string n ) a[n] = args[n];
77926b2000-04-11Per Hedbor 
34a9b81999-08-06Peter Bortas  return a; } string tag_cimg( string t, mapping args, RequestID id ) { mapping a = get_my_args( args, id );
37bdda1999-05-18Per Hedbor  args -= a;
54d2fa1999-11-24Per Hedbor  args->src = query_internal_location()+the_cache->store( a,id );
36eceb2000-01-30Per Hedbor  if( mapping size = the_cache->metadata( a, id, 1 ) )
37bdda1999-05-18Per Hedbor  { // image in cache (1 above prevents generation on-the-fly) args->width = size->xsize; args->height = size->ysize; } return make_tag( "img", args ); }
698c121999-05-19Peter Bortas  string tag_cimg_url( string t, mapping args, RequestID id ) {
54d2fa1999-11-24Per Hedbor  return query_internal_location()+the_cache->store(get_my_args(args,id),id);
698c121999-05-19Peter Bortas }