#include <qdir.h> #include <qfile.h> #include <qimage.h> int thumb_w; int thumb_h; int across; int quality; bool notext; int rcount; bool intable; QFile outfile; int help(); int version(); void output( QString out ); void process_directive( QString dir ); void process_text( QString text ); void process_file( QString file ); int main( int argc, char** argv ) { argc--; argv++; QString inf; if ( argc ) inf = argv[0]; else inf = "webthumb.txt"; if ( inf == "-h" ) return help(); if ( inf == "-V" ) return version(); QFile infile( inf ); if ( !infile.open( IO_ReadOnly ) ) { fprintf( stderr, "Unable to open %s\n", inf ); return 1; } QDir dir; dir.mkdir( "thumbs" ); //Default settings. thumb_w = 200; thumb_h = 200; across = 4; quality = -1; notext = false; rcount = 0; intable = false; QString line; while ( infile.readLine( line, 2048 ) != -1 ) { if ( line.endsWith( "\n" ) ) { line = line.left( line.length() - 1 ); } if ( line.startsWith( "\\" ) ) { process_directive( line.right( line.length() - 1 ) ); continue; } if ( line.startsWith( "#" ) ) { process_text( line.right( line.length() - 1 ) ); continue; } process_file( line ); } if ( intable ) { intable = false; if ( rcount ) { rcount = 0; output( "</tr>" ); } output( "</table>" ); } } void output( QString out ) { if ( outfile.name().length() ) { const char* bytes = out.ascii(); while ( *bytes ) { outfile.putch( *bytes ); bytes++; } outfile.putch( '\n' ); return; } printf( out.ascii() ); printf( "\n" ); } void process_directive( QString dir ) { if ( dir.startsWith( "output=", false ) ) { outfile.setName( dir.right( dir.length() - 7 ).stripWhiteSpace() ); if ( !outfile.open( IO_WriteOnly ) ) { fprintf( stderr, "Unable to open %s for write.\n", outfile.name().ascii() ); outfile.setName( "" ); }; return; } if ( dir.startsWith( "width=", false ) ) { thumb_w = dir.right( dir.length() - 6 ).toInt(); return; } if ( dir.startsWith( "height=", false ) ) { thumb_h = dir.right( dir.length() - 7 ).toInt(); return; } if ( dir.startsWith( "columns=", false ) ) { across = dir.right( dir.length() - 8 ).toInt(); return; } if ( dir.startsWith( "quality=", false ) ) { quality = dir.right( dir.length() - 8 ).toInt(); return; } if ( dir.startsWith( "titleonly", false ) ) { notext = true; return; } } void process_text( QString text ) { if ( intable ) { intable = false; if ( rcount ) { rcount = 0; output( "</tr>" ); } output( "</table>" ); } output( text ); } void process_file( QString file ) { if ( file.length() <= 5 ) return; if ( !intable ) { intable = true; rcount = 0; output( "<table>" ); } if ( !rcount ) { output( "<tr>" ); } file = file.simplifyWhiteSpace(); QString img_name = file.section( ' ', 0, 0 ); QString img_desc = file.section( ' ', 1 ); QImage img_data( img_name ); QImage thumb_data = img_data.smoothScale( thumb_w, thumb_h, QImage::ScaleMin ); QString thumb_name = img_name; thumb_name = thumb_name.replace( '.', '_' ) + "_thumb.jpg"; thumb_name = thumb_name.prepend( "thumbs/" ); thumb_data.save( thumb_name, "JPEG", quality ); //open cell and link QString cell = "<td><a href=\"" + img_name + "\">"; //img tag cell += "<img src=\"" + thumb_name; cell += "\" title=\"" + img_desc + "\">"; //description if ( !notext ) cell += "<br>" + img_desc; //close link and cell cell += "</a></td>"; output( cell ); rcount++; if ( rcount >= across ) { output( "</tr>" ); rcount = 0; } } int help() { printf( "\n" ); printf( "Usage: webthumb [input file]\n" ); printf( "Input file defaults to webthumb.txt.\n" ); printf( "Syntax for input file:\n" ); printf( "--------------------------\n" ); printf( "IMAGE Image Caption : thumbnail IMAGE with caption 'Image Caption'\n" ); printf( "--------------------------\n" ); printf( "#<insert html here> : pass html straight through to output file\n" ); printf( "--------------------------\n" ); printf( "\\variable=value : set configuration variable\n" ); printf( " output=FILE (specifies HTML file to write to, default stdout)\n" ); printf( " height=NUM (specifies max height of thumbnails, default 200)\n" ); printf( " width=NUM (specifies max width of thumbnails, default 200)\n" ); printf( " columns=NUM (specifies number of columns in table, default 4)\n" ); printf( " quality=NUM (specifies non-default quality on a scale of 0-100)\n" ); printf( " titleonly (uses caption as HTML title but not as text)\n" ); printf( "\n" ); return 0; } int version() { printf( "\n" ); printf( "WebThumb version $Change: 4996 $\n" ); printf( "A LeadToGold.com concoction\n" ); printf( "\n" ); return 0; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#12 | 5984 | Sam Stafford |
Fix a couple of bugs and made "titlesonly" the default; setting "captions" overrides it. The new captions setting also forces "columns" to a non-zero value, since the linebreaks in the captions break the non-table mode. |
||
#11 | 5970 | Sam Stafford |
New table-less output mode for Webthumb. Does all the same thumbnailing and padding but generates a big wrapping row instead of a table. This is the new default; if you want a set number of columns henceforth you'll have to use \columns explicitly. |
||
#10 | 5687 | Sam Stafford |
New "\link=" feature. Webthumb also now handles images in subdirectories; the thumbnails all still go into the same "thumbs" folder in the current directory, flattened by having slashes converted into underscores. |
||
#9 | 5463 | Sam Stafford | Correct for potential rounding problem. | ||
#8 | 5461 | Sam Stafford |
A whole mess of new configuration variables, mostly to do with spacing out thumbnails in more visually pleasing fashions. |
||
#7 | 5460 | Sam Stafford |
Add new "imgattrib" variable, to allow handy things like changing the border style on thumbnail images. Also added a little error checking on variables (invalid variables now produce console warnings), and an ifdef to make WebThumb output native line endings. |
||
#6 | 5458 | Sam Stafford | Change version string to remove RCS keyword cruft and include a date. | ||
#5 | 5457 | Sam Stafford |
Add "titlesonly" synonym (since I'm always forgetting which it is), and horizontally center images where appropriate. (Also converted VS projects to latest version.) |
||
#4 | 4996 | Sam Stafford | Put quotes around "href" fields as well. | ||
#3 | 4994 | Sam Stafford |
Make WebThumb more HTML spec compliant (Firefox gets fussy about the lack of quotes), add a new option ("titleonly"), add a -V so I can check versions easily. |
||
#2 | 4933 | Sam Stafford | Changed WebThumb build to CONSOLE, added -h usage information. | ||
#1 | 4845 | Sam Stafford |
WebThumb, a quick 'n dirty little tool for generating thumbnail web pages. |