File: thumbnail.pike

package info (click to toggle)
libroxen-thumbnail 1.4-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 36 kB
  • sloc: makefile: 38
file content (252 lines) | stat: -rw-r--r-- 6,269 bytes parent folder | download | duplicates (3)
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
/*
 * Description:
 *   This is a roxen module.
 *   Gives a <thumbnail> tag which displays a resized/cached image
 *
 * Copyright: GPL
 *
 * Authors:
 *   Chris Jantzen
 *
 * Bugs:
 *   - It does not scale images *up* to the thumbnail size. (option?)
 *   - Anybody in the world can use it. (needs referrer restrictions?)
 *   - Does Pike understand more than PNM/GIF/JPEG? It seems hackish
 *     to have to try all the different decode methods by hand.
 *
 * To Do:
 *   - It may be nice to have an href argument to autogenerate a link.
 *   - It may be worthwhile to pass through unsupported arguments so
 *     people can use more JavaScript.
 *   - (actually these are thoughts that I'm only lukewarm to)
 *
 * History:
 *   08 Oct 1998 v0.1 chris
 *     - First internal release
 *
 *   09 Oct 1998 v0.2 chris
 *     - First public release (many cosmetics)
 *
 * Comments:
 *   Some people may balk at my whitespace style. <boo-hoo> If you look
 *   closely, you'll probably note fragments of code that were liberally
 *   lifted from various other modules. ^_^
 *
 */

constant cvs_version = "$Id: (0.2) Module not currently stored in any CVS$";

#include <module.h>
inherit "module";
inherit "roxenlib";

import Image;
constant thread_safe = 1;

// HEX to Array Color conversion.
array (int) mkcolor(string color)
{
  int c = (int) ("0x"+(color-" "));
  return ({((c >> 16) & 0xff), ((c >>  8) & 0xff), (c & 0xff)});
}

// MODULE_LOCATION functions
mapping find_file(string f, object id)
{
  string filename, bg;
  int width, height, rot, trans;

  if(sscanf(f, "%d/%d/%s/%d/%d/$%s", trans, rot, bg, height, width,
	    filename) != 6 )
    return 0;

  // Retrieve thumbnail from cache. Load and scale, if it fails.
  object result = cache_lookup("thumbnail", f);

  if(!result) {

    int err;
    mixed t;
    string buff;

    t = id->conf->real_file(filename, id);

    if(!t) {
      return 0;
    }

    buff = Stdio.read_bytes(t);

    if(!buff) {
      return 0;
    }

    object ourimage;

    if(catch(ourimage = PNM.decode(buff))) {
      if(catch(ourimage = GIF.decode(buff))) {
	if(catch(ourimage = JPEG.decode(buff))) {
	  return 0;
	}
      }
    }

    float scale = 1.0;
    int xoffs = 0;
    int yoffs = 0;
    float xsize = (float) ourimage->xsize();
    float ysize = (float) ourimage->ysize();

    // Apply Transforms
    // result = result->copy(0, 0, result->xsize()-1, result->ysize()-1);
    if(height || width) {

      if(height && (float) height < ysize*scale) {
	scale = ((float) height)/ysize;
      }
      if(width && (float) width < xsize*scale) {
	scale = ((float) width)/xsize;
      }
      ourimage = ourimage->scale(scale);

      if(ysize*scale < (float) height) {
	yoffs = (int) (((float) height-ysize*scale)/2);
      }
      if(xsize*scale < (float) width) {
	xoffs = (int) (((float) width-xsize*scale)/2);
      }
      
    }
    
    result = image((width?width:(int) (xsize*scale)),
		   (height?height:(int) (ysize*scale)), @mkcolor(bg));

    result = result->paste(ourimage, xoffs, yoffs);

    if(rot)
      result = result->rotate(rot, @mkcolor(bg));
    
    cache_set("thumbnails", f, result);
  }

  // Apply Color Transform
  object ct = cache_lookup("thumbnail_coltables", filename);
  if(!ct) {
    // Make a suitable color table for this thumbnail.
    ct = colortable(result->copy(0, 0, result->xsize()-1, result->ysize()-1),
		    64)->cubicles(20, 20, 20);
    cache_set("thumbnail_coltables", filename, ct);
  }

  if(trans)
    return http_string_answer(GIF.encode_trans(result, ct, @mkcolor(bg)), 
			      "image/gif");
  else
    return http_string_answer(GIF.encode(result, ct), "image/gif");
}

// MODULE_TAG functions
string tag_thumbnail(string tag, mapping args, object id, object file, mapping defines)
{
  string url;
  if(!args->src) {
    url = "<br clear=\"all\"><p>Thumbnail tag needs src argument!</p>";
  }
  else {
    // Protect from people abusing the module
    int height = 0, width = 0;

    if(args->height) {
      height = (int) args->height;
      if(height > 512) {
	height = 512;
      }
    }
    if(args->width) {
      width = (int) args->width;
      if(width > 512) {
	width = 512;
      }
    }

    // Get the path if not absolute
    string filename = "";

    if(args->src[0..0] != "/") {
      array(string) parts = id->not_query/"/";
      for(int i = 0; i < sizeof(parts)-1; i++) {
	filename += parts[i]+"/";
      }
    }
    filename += args->src;

    url = "<img _parsed=\"1\" src=\""+query("mountpoint")+
      (args->trans?"1":"0")+"/"+
      (args->rot?args->rot:"0")+"/"+
      (args->bg?args->bg-"#":"ffffff")+"/"+
      height+"/"+width+"/$"+filename+"\""+
      (args->border?(" border=\""+args->border+"\""):"")+
      (args->align?(" align=\""+args->align+"\""):"")+">";
  }

  usecount++;
  return url;
}


// Required functions for Roxen modules--

int usecount;

string starttime = ctime(time());

void create()
{
  defvar("mountpoint", "/thumbnail/", "Mount point", TYPE_LOCATION, 
	 "Thumbnail result location in virtual filesystem "
	 "(trailing slash important).");

}

array register_module()
{
  return(({MODULE_PARSER | MODULE_LOCATION | MODULE_PROVIDER,
	     "Thumbnail tag",
	     "Automatic thumbnail tag (with caching)<P>"
	     "<PRE>&lt; thumbnail\n"
	     "      src=\"filename\"    | Image file location in virtual filesystem (required)\n"
	     "      height=\"y\"        | Vertical bounding size (in pixels)\n"
	     "      width=\"x\"         | Horizontal bounding size (in pixels)\n"
	     "      border=...        | Like &lt;IMG BORDER...&gt;\n"
	     "      align=...         | Like &lt;IMG ALIGN...&gt;\n"
	     "      bg=\"#rrggbb\"      | Background for thumbnail\n"
	     "      rot=\"n\"           | Rotate by n degrees\n"
	     "      trans &gt;           | Make background color transparent</PRE><P>",
	     0,
             1}));
}

string status()
{
   return "Called " + usecount + " times since " + starttime;
}

void start() // read the definitions from the config interface
{
  return;
}

string query_location()
{
  return query("mountpoint");
}

string query_provides()
{
  return "thumbnail";
} 

mapping query_tag_callers()
{
  return (["thumbnail":tag_thumbnail, "thumbnail_url":tag_thumbnail]);
}