From 97e431536395c948b580b638e1f35b7b4eebdbeb Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 22 Sep 2013 00:09:34 +0200 Subject: [PATCH] Initial commit --- gen-sizes.sh | 8 ++++ meme.fcgi | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100755 gen-sizes.sh create mode 100755 meme.fcgi diff --git a/gen-sizes.sh b/gen-sizes.sh new file mode 100755 index 0000000..9b0b665 --- /dev/null +++ b/gen-sizes.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# Generate a `meme-sizes.lst` file containing width, height and filename for +# every .jpg file in the current directory. +# +# This file is read by the SVG meme generator to gather information +# about known meme bases. + +identify -format "%w %h %f\n" *.jpg > meme-sizes.lst diff --git a/meme.fcgi b/meme.fcgi new file mode 100755 index 0000000..939a8f4 --- /dev/null +++ b/meme.fcgi @@ -0,0 +1,107 @@ +#!/usr/bin/perl -w + +use strict; + +use CGI::Fast; + +use HTML::Entities; + +use File::Basename; + +my %sizes; + +my $script_path = $ENV{'SCRIPT_FILENAME'} ? dirname($ENV{'SCRIPT_FILENAME'}) : dirname($0); + +my $sz_fname = $script_path . '/meme-sizes.lst'; + +open FILE, $sz_fname or die $!; + +while (my $line = ) { + chomp($line); + next unless $line; + my ($width, $height, $fname) = split(/ /, $line, 3); + $sizes{$fname} = [$width, $height]; +} + +close FILE; + + +# params: img, width, height, text + +my $svg_template=< + + + +%4\$s +SVG + +# template: y-pos, font-size, text +my $txt_template=<%3\$s +TXT + +while (my $q = new CGI::Fast) { + my $img = $q->param('m') || (keys %sizes)[0]; + if (!defined $sizes{$img}) { + print $q->header(-status=>404), + $q->start_html("Unknown meme base"), + $q->h1("Unknown meme base!"); + print "

Sorry, '" . encode_entities($img) . "' is not a known meme base. ". + "You want one of the following instead:

    "; + foreach (keys %sizes) { + print "
  • " . encode_entities($_) . "
  • "; + } + print "
"; + # foreach (keys %ENV) { + # print "

$_=$ENV{$_}

" + # } + print $q->end_html(); + next; + } + + print $q->header( + -type => 'image/svg+xml', + -charset => 'UTF-8' + ); + + my ($width, $height) = @{$sizes{$img}}; + + my $sep = $q->param('s') || '/'; # line separator + + my @t = $q->param('t') || ('TEST TOP//TEST BOTTOM'); + + my $divisions = 7; + my @lines = (); + foreach (@t) { + foreach (split /\Q$sep\E/) { + push @lines, $_; + } + } + + $divisions = @lines if @lines > $divisions; + + my $fontsize = int($height/$divisions + 0.5); + my $offset = int(100/$divisions + 0.5); + my $fillers = grep { $_ eq '' } @lines; + my $real_lines = @lines - $fillers; + my $filler_size = $fillers ? int((98 - $offset*$real_lines)/$fillers) : 0; + + my $dy = 0; + my $txt = ''; + foreach (@lines) { + if ($_ eq '') { + $dy += $filler_size; + next; + } + $dy += $offset; + $txt .= sprintf($txt_template, $dy, $fontsize, $_); + } + + printf($svg_template, $img, $width, $height, $txt); +} -- 2.32.0.93.g670b81a890