File: ImageRef.pm

package info (click to toggle)
faqomatic 2.721-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,984 kB
  • ctags: 548
  • sloc: perl: 13,356; sh: 69; makefile: 47
file content (403 lines) | stat: -rw-r--r-- 12,612 bytes parent folder | download | duplicates (2)
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
##############################################################################
# The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved.      #
#                                                                            #
# This program is free software; you can redistribute it and/or              #
# modify it under the terms of the GNU General Public License                #
# as published by the Free Software Foundation; either version 2             #
# of the License, or (at your option) any later version.                     #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.#
#                                                                            #
# Jon Howell can be contacted at:                                            #
# 6211 Sudikoff Lab, Dartmouth College                                       #
# Hanover, NH  03755-3510                                                    #
# jonh@cs.dartmouth.edu                                                      #
#                                                                            #
# An electronic copy of the GPL is available at:                             #
# http://www.gnu.org/copyleft/gpl.html                                       #
#                                                                            #
##############################################################################

use strict;

###
### ImageRef.pm
###
### Generates references to the images encoded in ImageData.pm
### (note this only makes sense for built-in images, not those sumbitted
### into the serve/bags/ directory.)
###

package FAQ::OMatic::ImageRef;

use FAQ::OMatic::Bags;

my %img_type = ();		# type of each image. constant; no mod_perl
						# cache problem
my %img_prop = ();		# properties of each image. constant; no mod_perl
						# cache problem

sub getImage {
	my $name = shift;

	require FAQ::OMatic::ImageData;	# put off loading this file unless someone
							# actually requests data (meaning we're running
							# img.pm). No reason any other invocation should
							# have to load all that image data up.
	my $data = $FAQ::OMatic::ImageData::img{$name};
	return '' if (not defined $data);

	$data =~ s/\n//gs;	# get rid of line terminators
	return pack("H".length($data), $data);
}

sub validImage {
	my $name = shift;

	return defined($img_type{$name});
}

sub getType {
	my $name = shift;

	my $typek = $img_type{$name};
	return '' if (not defined $typek);

	return "jpeg"	if ($typek eq 'jpg');
	return "gif"	if ($typek eq 'gif');
	return "beats-me";
}

sub getBagForImage {
	my $name = shift;
	my $typek = $img_type{$name};
	die "undefined name" if (not defined $name);
	die "undefined typek for img $name" if (not defined $typek);
	return "$name.$typek";
}

sub getImageUrl {
	my $name = shift;
	my $params = shift;
	my $forceBagWrite = shift || '';

	my $bagName = getBagForImage($name);

	my $bagUrl = FAQ::OMatic::makeBagRef($bagName, $params);

	my $bagPath = $FAQ::OMatic::Config::bagsDir.$bagName;
	if (-f $bagPath and not $forceBagWrite) {
		return $bagUrl;
	}
	if (not defined $FAQ::OMatic::Config::bagsDir) {
		# fail obviously if bagsDir not configured -- this
		# happens when upgrading versions
		return "x:";
	}

	# attempt to cache this image file in $bagsDir
	if (not open(CACHEIMAGE, ">$bagPath")) {
		FAQ::OMatic::gripe('problem',
			"write to $bagPath failed: $!");
		# TODO: write to cache failed. Notify admin?
		# in the meantime, supply user with dynamically-generated image file
		return FAQ::OMatic::makeAref('-command'=>'img',
			'-blastAll'=>1,
			'-changedParams'=>{'name'=>$name},
			'-refType'=>'url');
	}

	# bag is saved, now write a .desc file
	my $bagDesc = FAQ::OMatic::Bags::getBagDesc($bagName);

	$bagDesc->setProperty('SizeWidth', $img_prop{$name}{'SizeWidth'});
	$bagDesc->setProperty('SizeHeight', $img_prop{$name}{'SizeHeight'});
	$bagDesc->setProperty('SizeBytes', $img_prop{$name}{'SizeBytes'});
	$bagDesc->setProperty('Alt', $img_prop{$name}{'Alt'});
	FAQ::OMatic::Bags::saveBagDesc($bagDesc);

	print CACHEIMAGE getImage($name);
	close CACHEIMAGE;
	return $bagUrl;
}

sub getImageRef {
	my $name = shift;
	my $imgargs = shift;
	my $params = shift;

	return ' ' if (defined($FAQ::OMatic::Config::noImages));

	my $url = getImageUrl($name, $params);
	return "[bad image $name]" if (not $url);

	# look up size information to append to img tag
	my $bagName = getBagForImage($name);
	my $sw = FAQ::OMatic::Bags::getBagProperty($bagName, 'SizeWidth', '');
	my $swt = ($sw ne '') ? " width=$sw" : '';
	my $sh = FAQ::OMatic::Bags::getBagProperty($bagName, 'SizeHeight', '');
	my $sht = ($sh ne '') ? " height=$sh" : '';
	my $alt = FAQ::OMatic::Bags::getBagProperty($bagName, 'Alt', $name);

	if (wantarray) {
		return ("<img src=\"$url\" alt=$alt $imgargs$swt$sht>", $sw, $sh);
	} else {
		return "<img src=\"$url\" alt=\"$alt \" $imgargs$swt$sht>";
	}
}

sub getImageRefCA {
	my $name = shift;
	my $imgargs = shift;
	my $ca = shift;	 # Category or Answer
	my $params = shift;

	$name = ($ca ? 'cat' : 'ans').$name;

	return getImageRef($name, $imgargs, $params);
}

sub bagAllImages {
	# writes every image in ImageData to the bags dir
	my $imgname;
	foreach $imgname (sort keys %img_type) {
		FAQ::OMatic::maintenance::hprint(
				"<br>bagging ".getBagForImage($imgname)."\n");
		FAQ::OMatic::maintenance::hflush();
		getImageUrl($imgname, {}, 'forceBagWrite');
	}
}

# these properties are manually-defined, so they're not in the
# section of this file that gets automatically regenerated.
$img_prop{'ans-also'}{'Alt'}		= '(Xref)';
$img_prop{'ans'}{'Alt'}				= '(Answer)';
$img_prop{'ans-small'}{'Alt'}		= '(Answer)';
$img_prop{'baglink'}{'Alt'}			= '(Download)';
$img_prop{'cat-also'}{'Alt'}		= '(Xref)';
$img_prop{'cat-small'}{'Alt'}		= '(Category)';
$img_prop{'cat'}{'Alt'}				= '(Category)';
$img_prop{'help-small'}{'Alt'}		= '(?)';
$img_prop{'help'}{'Alt'}			= '(?)';

# to regenerate:
#:.,$-2!(cd ../../../img; ../dev-bin/encodeBin.pl -desc *)
$img_type{'ans-also'} = 'gif';

$img_prop{'ans-also'}{'SizeWidth'} = '21';
$img_prop{'ans-also'}{'SizeHeight'} = '14';
$img_prop{'ans-also'}{'SizeBytes'} = '128';

$img_type{'ans-del-part'} = 'gif';

$img_prop{'ans-del-part'}{'SizeWidth'} = '23';
$img_prop{'ans-del-part'}{'SizeHeight'} = '28';
$img_prop{'ans-del-part'}{'SizeBytes'} = '183';

$img_type{'ans-dup-ans'} = 'gif';

$img_prop{'ans-dup-ans'}{'SizeWidth'} = '24';
$img_prop{'ans-dup-ans'}{'SizeHeight'} = '23';
$img_prop{'ans-dup-ans'}{'SizeBytes'} = '182';

$img_type{'ans-dup-part'} = 'gif';

$img_prop{'ans-dup-part'}{'SizeWidth'} = '23';
$img_prop{'ans-dup-part'}{'SizeHeight'} = '32';
$img_prop{'ans-dup-part'}{'SizeBytes'} = '194';

$img_type{'ans-edit-part'} = 'gif';

$img_prop{'ans-edit-part'}{'SizeWidth'} = '32';
$img_prop{'ans-edit-part'}{'SizeHeight'} = '31';
$img_prop{'ans-edit-part'}{'SizeBytes'} = '285';

$img_type{'ans-ins-part'} = 'gif';

$img_prop{'ans-ins-part'}{'SizeWidth'} = '32';
$img_prop{'ans-ins-part'}{'SizeHeight'} = '32';
$img_prop{'ans-ins-part'}{'SizeBytes'} = '261';

$img_type{'ans-opts'} = 'gif';

$img_prop{'ans-opts'}{'SizeWidth'} = '23';
$img_prop{'ans-opts'}{'SizeHeight'} = '28';
$img_prop{'ans-opts'}{'SizeBytes'} = '131';

$img_type{'ans-reorder'} = 'gif';

$img_prop{'ans-reorder'}{'SizeWidth'} = '23';
$img_prop{'ans-reorder'}{'SizeHeight'} = '28';
$img_prop{'ans-reorder'}{'SizeBytes'} = '191';

$img_type{'ans-small'} = 'gif';

$img_prop{'ans-small'}{'SizeWidth'} = '18';
$img_prop{'ans-small'}{'SizeHeight'} = '14';
$img_prop{'ans-small'}{'SizeBytes'} = '100';

$img_type{'ans-title'} = 'gif';

$img_prop{'ans-title'}{'SizeWidth'} = '32';
$img_prop{'ans-title'}{'SizeHeight'} = '32';
$img_prop{'ans-title'}{'SizeBytes'} = '257';

$img_type{'ans-to-cat'} = 'gif';

$img_prop{'ans-to-cat'}{'SizeWidth'} = '29';
$img_prop{'ans-to-cat'}{'SizeHeight'} = '26';
$img_prop{'ans-to-cat'}{'SizeBytes'} = '223';

$img_type{'ans'} = 'gif';

$img_prop{'ans'}{'SizeWidth'} = '23';
$img_prop{'ans'}{'SizeHeight'} = '28';
$img_prop{'ans'}{'SizeBytes'} = '150';

$img_type{'baglink'} = 'gif';

$img_prop{'baglink'}{'SizeWidth'} = '21';
$img_prop{'baglink'}{'SizeHeight'} = '14';
$img_prop{'baglink'}{'SizeBytes'} = '103';

$img_type{'cat-also'} = 'gif';

$img_prop{'cat-also'}{'SizeWidth'} = '25';
$img_prop{'cat-also'}{'SizeHeight'} = '14';
$img_prop{'cat-also'}{'SizeBytes'} = '139';

$img_type{'cat-del-part'} = 'gif';

$img_prop{'cat-del-part'}{'SizeWidth'} = '32';
$img_prop{'cat-del-part'}{'SizeHeight'} = '27';
$img_prop{'cat-del-part'}{'SizeBytes'} = '208';

$img_type{'cat-dup-ans'} = 'gif';

$img_prop{'cat-dup-ans'}{'SizeWidth'} = '26';
$img_prop{'cat-dup-ans'}{'SizeHeight'} = '23';
$img_prop{'cat-dup-ans'}{'SizeBytes'} = '201';

$img_type{'cat-dup-part'} = 'gif';

$img_prop{'cat-dup-part'}{'SizeWidth'} = '32';
$img_prop{'cat-dup-part'}{'SizeHeight'} = '27';
$img_prop{'cat-dup-part'}{'SizeBytes'} = '201';

$img_type{'cat-edit-part'} = 'gif';

$img_prop{'cat-edit-part'}{'SizeWidth'} = '32';
$img_prop{'cat-edit-part'}{'SizeHeight'} = '31';
$img_prop{'cat-edit-part'}{'SizeBytes'} = '286';

$img_type{'cat-ins-part'} = 'gif';

$img_prop{'cat-ins-part'}{'SizeWidth'} = '32';
$img_prop{'cat-ins-part'}{'SizeHeight'} = '27';
$img_prop{'cat-ins-part'}{'SizeBytes'} = '250';

$img_type{'cat-new-ans'} = 'gif';

$img_prop{'cat-new-ans'}{'SizeWidth'} = '32';
$img_prop{'cat-new-ans'}{'SizeHeight'} = '32';
$img_prop{'cat-new-ans'}{'SizeBytes'} = '253';

$img_type{'cat-new-cat'} = 'gif';

$img_prop{'cat-new-cat'}{'SizeWidth'} = '32';
$img_prop{'cat-new-cat'}{'SizeHeight'} = '32';
$img_prop{'cat-new-cat'}{'SizeBytes'} = '245';

$img_type{'cat-opts'} = 'gif';

$img_prop{'cat-opts'}{'SizeWidth'} = '32';
$img_prop{'cat-opts'}{'SizeHeight'} = '27';
$img_prop{'cat-opts'}{'SizeBytes'} = '165';

$img_type{'cat-reorder'} = 'gif';

$img_prop{'cat-reorder'}{'SizeWidth'} = '32';
$img_prop{'cat-reorder'}{'SizeHeight'} = '27';
$img_prop{'cat-reorder'}{'SizeBytes'} = '207';

$img_type{'cat-small'} = 'gif';

$img_prop{'cat-small'}{'SizeWidth'} = '18';
$img_prop{'cat-small'}{'SizeHeight'} = '14';
$img_prop{'cat-small'}{'SizeBytes'} = '104';

$img_type{'cat-title'} = 'gif';

$img_prop{'cat-title'}{'SizeWidth'} = '32';
$img_prop{'cat-title'}{'SizeHeight'} = '32';
$img_prop{'cat-title'}{'SizeBytes'} = '232';

$img_type{'cat'} = 'gif';

$img_prop{'cat'}{'SizeWidth'} = '32';
$img_prop{'cat'}{'SizeHeight'} = '27';
$img_prop{'cat'}{'SizeBytes'} = '185';

$img_type{'checked-large'} = 'gif';

$img_prop{'checked-large'}{'SizeWidth'} = '20';
$img_prop{'checked-large'}{'SizeHeight'} = '24';
$img_prop{'checked-large'}{'SizeBytes'} = '139';

$img_type{'checked'} = 'gif';

$img_prop{'checked'}{'SizeWidth'} = '16';
$img_prop{'checked'}{'SizeHeight'} = '17';
$img_prop{'checked'}{'SizeBytes'} = '104';

$img_type{'help-small'} = 'gif';

$img_prop{'help-small'}{'SizeWidth'} = '16';
$img_prop{'help-small'}{'SizeHeight'} = '12';
$img_prop{'help-small'}{'SizeBytes'} = '108';

$img_type{'help'} = 'gif';

$img_prop{'help'}{'SizeWidth'} = '32';
$img_prop{'help'}{'SizeHeight'} = '24';
$img_prop{'help'}{'SizeBytes'} = '181';

$img_prop{'picker'}{'SizeWidth'} = '256';
$img_prop{'picker'}{'SizeHeight'} = '128';
$img_prop{'picker'}{'SizeBytes'} = '3189';

$img_type{'picker'} = 'jpg';

$img_type{'space-large'} = 'gif';

$img_prop{'space-large'}{'SizeWidth'} = '20';
$img_prop{'space-large'}{'SizeHeight'} = '25';
$img_prop{'space-large'}{'SizeBytes'} = '61';

$img_type{'space'} = 'gif';

$img_prop{'space'}{'SizeWidth'} = '16';
$img_prop{'space'}{'SizeHeight'} = '16';
$img_prop{'space'}{'SizeBytes'} = '55';

$img_type{'unchecked'} = 'gif';

$img_prop{'unchecked'}{'SizeWidth'} = '16';
$img_prop{'unchecked'}{'SizeHeight'} = '16';
$img_prop{'unchecked'}{'SizeBytes'} = '79';

$img_type{'cat-to-ans'} = 'gif';

$img_prop{'cat-to-ans'}{'SizeWidth'} = '29';
$img_prop{'cat-to-ans'}{'SizeHeight'} = '26';
$img_prop{'cat-to-ans'}{'SizeBytes'} = '213';


1;