Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Aug 17, 2013
@kaoru kaoru Initial try at counter (the image one)
Lots of duplicated code from text_counter :-(

But using Imager to produce a PNG, and then using URI to write out a
data URI to embed the image in the HTML itself, was super easy!
f10fe3a
@davorg Merge pull request #4 from kaoru/counter
Initial try at counter (the image one)
2bc162e
Showing with 106 additions and 0 deletions.
  1. BIN  counter/Ubuntu-L.ttf
  2. +106 −0 counter/counter.psgi
View
BIN  counter/Ubuntu-L.ttf
Binary file not shown
View
106 counter/counter.psgi
@@ -0,0 +1,106 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use 5.010;
+
+use Dancer;
+use DBI;
+use File::Spec;
+use FindBin;
+use Imager;
+use Imager::File::PNG;
+use URI;
+
+my $dbh = init_db_connection();
+
+get '/' => sub {
+ my $count = increment_and_get_count($dbh);
+ return count_to_image($count);
+};
+
+dance;
+
+#######################################
+
+sub init_db_connection {
+ # connect to the SQLite database
+ my $counter_db_file = File::Spec->catfile($FindBin::Bin, 'counter.db');
+ my $dbh = DBI->connect(
+ "dbi:SQLite:dbname=${counter_db_file}", # data source
+ "", # username
+ "", # password
+ { RaiseError => 1 }, # throw exceptions on failure
+ ) or die "Failed to connect to SQLite DB: $DBI::errstr";
+
+ # maybe initialize
+ maybe_initialize_db($dbh);
+
+ return $dbh;
+}
+
+sub maybe_initialize_db {
+ my $dbh = shift;
+
+ $dbh->do("CREATE TABLE IF NOT EXISTS Count(count INT DEFAULT 0);");
+
+ my $sth = $dbh->prepare("SELECT count FROM Count;");
+ $sth->execute();
+ if (!$sth->fetchrow()) {
+ $dbh->do("INSERT INTO Count VALUES(0)");
+ }
+
+ return;
+}
+
+sub increment_and_get_count {
+ my $dbh = shift;
+
+ $dbh->do("UPDATE Count SET count = count + 1;");
+
+ my $sth = $dbh->prepare("SELECT count FROM Count;");
+ $sth->execute();
+ my ($count) = $sth->fetchrow();
+
+ return $count;
+}
+
+sub count_to_image {
+ my $count = shift;
+
+ my $width = 20 * length($count);
+
+ my $image = Imager->new(
+ xsize => $width,
+ ysize => 30,
+ channels => 4
+ );
+ $image->box(filled => 1, color => '#FFFFFF');
+
+ my $font_file = File::Spec->catfile($FindBin::Bin, 'Ubuntu-L.ttf');
+ my $font = Imager::Font->new(
+ file => $font_file,
+ color => '#000000',
+ size => 30
+ );
+
+ $image->align_string(
+ string => "$count",
+ font => $font,
+ x => 5,
+ y => 15,
+ halign => 'left',
+ valign => 'center',
+ aa => 1, # anti-aliasing
+ ) or die $image->errstr;
+
+ my $image_data;
+ $image->write(type => 'png', data => \$image_data)
+ or die $image->errstr;
+
+ my $URI = URI->new("data:");
+ $URI->media_type("image/png");
+ $URI->data("$image_data");
+
+ return "<img src='$URI' alt='Counter: $count'>";
+}

No commit comments for this range

Something went wrong with that request. Please try again.