summaryrefslogtreecommitdiffstats
path: root/kjsembed/docs/examples/imageinfo/imagegallery.js
blob: dba3951c550ffd195a1c88cecbf4fd41044a20c9 (plain)
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
#!/usr/bin/env kjscmd

//
// Script to create an image gallery for a set of images.
//

var default_title = 'Image Gallery';
var default_intro =   'Here are some images, the larger ones have been scaled down to '
                    + 'ensure this page loads tquickly. Click on an image or filename '
                    + 'to see it full size.';
var default_width = 250;
var default_height = 160;

function write_header( title, intro )
{
    println( '<html>' );
    println( '<head>' );
    println( '<title>'+title+'</title>' );
    println( '</head>' );
    println( '<body>' );
    println( '<h1 align="center">'+title+'</h1>' );
    println( '<hr>' );
    println( '<p>'+intro+'</p>' );
    println( '<table border=0 width="90%" cellpadding="12" cellspacing="1">' );
}

function write_footer()
{
    println( '</table>' );
    println( '</body></html>' );
}

function write_image( name, w, h, thumb, tw, th, desc )
{
    println( '<tr>' );

    print( '<td align="center">' );
    print( '<a href="'+name+'">' );
    print( '<img border=0 width='+tw+' height='+th+' src="'+thumb+'">' );
    print( '</a>' );
    println( '</td>' );

    print( '<td width="60%" valign="top" align="justify">' );
    print( '<b><a href="'+name+'">'+name+'</a></b>'+' ('+w+'x'+h+')' );
//    println( '<hr>' );
    println( '<p>'+desc+'</p>' );
    println( '</td>' );

    println( '</tr>' );
}

// Create a thumbnail and write the img tag.
function process_image( name, desc )
{
    var img = new Image();
    img.load( name );
    if ( !img.isOk() ) {
	warn( 'Failed to load image '+name);
	return null;
    }

    var w = img.width();
    var h = img.height();

    if ( (w > default_width) || (h > default_height) ) {
	img.smoothScaleMin( default_width, default_height );
    }

    var tw = img.width();
    var th = img.height();

    var thumb = 'thumb-'+name;
    img.save( thumb );

    if ( !img.isOk() ) {
	warn( 'Failed to save thumbnail '+thumb);
	return null;
    }

    write_image( name, w, h, thumb, tw, th, desc );
}

if ( application.args.length == 0 ) {
    System.stderr.println( 'Usage:' );
    System.stderr.println( '\timagegallery imgfile ...' );
}
else {
    write_header( default_title, default_intro );

    for ( var i = 0 ; i < application.args.length ; i++ ) {
	var name = application.args[i];
	if ( name.match( /^thumb-/ ) ) {
	    // Ignore thumbnails
	}
	else {

	    var desc_file = name.replace( /\.[^\.]+$/, '.htm' );
	    var desc;
	    try {
		desc = System.readFile( desc_file );
	    }
            catch(x) {
		desc = '<i>No Description</i>';
            }
	    process_image( name, desc );
	}
    }

    write_footer();
}