Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #4 from kaoru/counter

Initial try at counter (the image one)
  • Loading branch information...
commit 2bc162ee895860fba75eb58bb988f38178c218a0 2 parents 0508e98 + f10fe3a
Dave Cross authored August 17, 2013
BIN  counter/Ubuntu-L.ttf
Binary file not shown
106  counter/counter.psgi
... ...
@@ -0,0 +1,106 @@
  1
+#!/usr/bin/perl
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+use Dancer;
  8
+use DBI;
  9
+use File::Spec;
  10
+use FindBin;
  11
+use Imager;
  12
+use Imager::File::PNG;
  13
+use URI;
  14
+
  15
+my $dbh = init_db_connection();
  16
+
  17
+get '/' => sub {
  18
+    my $count = increment_and_get_count($dbh);
  19
+    return count_to_image($count);
  20
+};
  21
+
  22
+dance;
  23
+
  24
+#######################################
  25
+
  26
+sub init_db_connection {
  27
+    # connect to the SQLite database
  28
+    my $counter_db_file = File::Spec->catfile($FindBin::Bin, 'counter.db');
  29
+    my $dbh = DBI->connect(
  30
+        "dbi:SQLite:dbname=${counter_db_file}", # data source
  31
+        "",                                     # username
  32
+        "",                                     # password
  33
+        { RaiseError => 1 },                    # throw exceptions on failure
  34
+    ) or die "Failed to connect to SQLite DB: $DBI::errstr";
  35
+
  36
+    # maybe initialize
  37
+    maybe_initialize_db($dbh);
  38
+
  39
+    return $dbh;
  40
+}
  41
+
  42
+sub maybe_initialize_db {
  43
+    my $dbh = shift;
  44
+
  45
+    $dbh->do("CREATE TABLE IF NOT EXISTS Count(count INT DEFAULT 0);");
  46
+
  47
+    my $sth = $dbh->prepare("SELECT count FROM Count;");
  48
+    $sth->execute();
  49
+    if (!$sth->fetchrow()) {
  50
+        $dbh->do("INSERT INTO Count VALUES(0)");
  51
+    }
  52
+
  53
+    return;
  54
+}
  55
+
  56
+sub increment_and_get_count {
  57
+    my $dbh = shift;
  58
+
  59
+    $dbh->do("UPDATE Count SET count = count + 1;");
  60
+
  61
+    my $sth = $dbh->prepare("SELECT count FROM Count;");
  62
+    $sth->execute();
  63
+    my ($count) = $sth->fetchrow();
  64
+
  65
+    return $count;
  66
+}
  67
+
  68
+sub count_to_image {
  69
+    my $count = shift;
  70
+
  71
+    my $width = 20 * length($count);
  72
+
  73
+    my $image = Imager->new(
  74
+        xsize => $width,
  75
+        ysize => 30,
  76
+        channels => 4
  77
+    );
  78
+    $image->box(filled => 1, color => '#FFFFFF');
  79
+
  80
+    my $font_file = File::Spec->catfile($FindBin::Bin, 'Ubuntu-L.ttf');
  81
+    my $font = Imager::Font->new(
  82
+        file => $font_file,
  83
+        color => '#000000',
  84
+        size => 30
  85
+    );
  86
+
  87
+    $image->align_string(
  88
+        string => "$count",
  89
+        font => $font,
  90
+        x => 5,
  91
+        y => 15,
  92
+        halign => 'left',
  93
+        valign => 'center',
  94
+        aa => 1, # anti-aliasing
  95
+    ) or die $image->errstr;
  96
+
  97
+    my $image_data;
  98
+    $image->write(type => 'png', data => \$image_data)
  99
+        or die $image->errstr;
  100
+
  101
+    my $URI = URI->new("data:");
  102
+    $URI->media_type("image/png");
  103
+    $URI->data("$image_data");
  104
+
  105
+    return "<img src='$URI' alt='Counter: $count'>";
  106
+}

0 notes on commit 2bc162e

Please sign in to comment.
Something went wrong with that request. Please try again.