|
|
|
#include <module.h> |
inherit "module"; |
constant thread_safe=1; |
|
roxen.ImageCache the_cache; |
|
constant cvs_version = "$Id: cimg.pike,v 1.20 2000/06/03 04:10:30 nilsson Exp $"; |
constant module_type = MODULE_PARSER; |
constant module_name = "Image converter"; |
constant module_doc = "Provides the tag <tt><cimg></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())->tagdoc; |
string imagecache=the_cache->documentation("cimg src='internal-roxen-robodog'"); |
|
doc->cimg+=imagecache; |
doc["cimg-url"]=imagecache; |
return doc; |
} |
|
#ifdef manual |
constant tagdoc=(["cimg":#" |
<desc tag><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.</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'> |
<emit source='sql' query='select imagedata from images where id=37'> |
<cimg data='&sql.imagedata;'/> |
</emit> |
</ex> |
</attr>", |
|
"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'> |
<emit source='sql' query='select imagedata from images where id=37'> |
<cimg data='&sql.imagedata;'/> |
</emit> |
</ex> |
</attr>", |
]); |
#endif |
|
|
void start() |
{ |
the_cache = roxen.ImageCache( "cimg", generate_image ); |
} |
|
mapping(string:function) query_action_buttons() { |
return ([ "Clear cache":flush_cache ]); |
} |
|
void flush_cache() { |
the_cache->flush(); |
} |
|
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]/2, Roxen.sizetostring(s[1])); |
} |
|
mapping generate_image( mapping args, RequestID id ) |
{ |
if( args->data ) |
return roxen.low_decode_image( args->data ); |
else |
return roxen.low_load_image( args->src, id ); |
} |
|
mapping find_internal( string f, RequestID id ) |
{ |
return the_cache->http_file_answer( f, id ); |
} |
|
mapping get_my_args( mapping args, RequestID id ) |
{ |
mapping a= |
([ |
"src":Roxen.fix_relative( args->src, id ), |
"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, |
]); |
|
a->stat = id->conf->stat_file( a->src,id ); |
|
a["background-color"] = id->misc->defines->bgcolor |
|| "#eeeeee"; |
|
foreach( glob( "*-*", indices(args)), string n ) |
a[n] = args[n]; |
|
return a; |
} |
|
class TagCImg { |
inherit RXML.Tag; |
constant name = "cimg"; |
constant flags = RXML.FLAG_EMPTY_ELEMENT; |
mapping(string:RXML.Type) req_arg_types = ([ "src" : RXML.t_text ]); |
|
class Frame { |
inherit RXML.Frame; |
|
array do_return(RequestID id) { |
mapping a = get_my_args( args, id ); |
args -= a; |
args->src = query_internal_location()+the_cache->store( a,id ); |
if( mapping size = the_cache->metadata( a, id, 1 ) ) |
{ |
|
args->width = size->xsize; |
args->height = size->ysize; |
} |
result = Roxen.make_tag( "img", args ); |
return 0; |
} |
} |
} |
|
class TagCImgURL { |
inherit RXML.Tag; |
constant name = "cimg-url"; |
constant flags = RXML.FLAG_EMPTY_ELEMENT; |
mapping(string:RXML.Type) req_arg_types = ([ "src" : RXML.t_text ]); |
|
class Frame { |
inherit RXML.Frame; |
|
array do_return(RequestID id) { |
result = query_internal_location()+the_cache->store(get_my_args(args,id),id); |
return 0; |
} |
} |
} |
|
|