line-height: 4ex;
vertical-align: text-top;
}
+dd > img {
+ max-width: 300px;
+ display: block;
+}
</style>
EOT
});
$row = {%post{ pairkeys @wordcols }};
$_ = length ? $_ : undef for values %{$row};
eval {
- my %res = (returning => $Request ? '*' : 'lang, cat');
+ my %res = (returning => '*');
my $query = $find ? $db->update(word => $row, $find, \%res) :
$db->insert(word => $row, \%res);
$row = $query->hash;
} or Alert("Entry could not be saved", $@);
+
+ my $imgpath = "data/word/org/$row->{id}.jpg";
+ if (my $download = $row->{source} and !-e $imgpath) {
+ require LWP::UserAgent;
+ my $ua = LWP::UserAgent->new;
+ $ua->agent('/');
+ my $status = $ua->mirror($download, $imgpath);
+ $status->is_success or Alert([
+ "Source image not found",
+ "Download from <q>$download</q> failed: ".$status->status_line,
+ ]);
+ }
+
+ my $thumbpath = "data/word/eng/$row->{form}.jpg";
+ if (-e $imgpath) {
+ my @cmds = @{ $row->{thumb} // [] };
+ unshift @cmds, -gravity => @cmds ? 'northwest' : 'center';
+ unshift @cmds, 'convert';
+ push @cmds, -resize => '300x200^', -extent => '300x200';
+ push @cmds, '-strip', -quality => '60%', -interlace => 'plane';
+ push @cmds, $imgpath, $thumbpath;
+ my $status = system @cmds;
+ $status == 0 or Alert([
+ "Thumbnail image not generated",
+ "Failed to convert source image, error code ".($status >> 8),
+ ], "@cmds");
+ }
+
+ $row = {map {$_ => $row->{$_}} qw(lang cat)} if not $Request;
}
elsif ($find) {
$row = $db->select(word => '*', $find)->hash
or Abort("Word not found", 404);
}
-my $title = $find ? "entry <small>#$Request</small>" : 'new entry';
+my $title = $row ? "entry <small>#$row->{id}</small>" : 'new entry';
:>
<h1>Words <:= $title :></h1>
printf '<dt><label for="%s">%s</label></dt>'
. '<dd><input id="%1$s" name="%1$s" value="%s" />',
$col->key, $col->value, $val;
+ -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
+ $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
+ $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
+ ();
say '</dd>';
}
:>