2 # Ikiwiki qrcode plugin encodes things into qrcode and is released
3 # under the terms of the GPL version 2, or any later version at your
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
8 package IkiWiki::Plugin::qrcode;
12 qrcode -- embed qr codes in IkiWiki
16 qrcode embed qr codes in IkiWIki
19 [[!qrcode code="http://www.donarmstrong.com"]]
24 =head2 Available options
45 use Imager::QRCode qw(plot_qrcode);
49 use Encode qw(decode);
50 use Digest::MD5 qw(md5_hex);
54 hook(type => "getsetup", id => $id, call => \&getsetup);
55 hook(type => "preprocess", id => $id, call => \&preprocess);
58 my $link = "http://git.donarmstrong.com/?p=ikiwiki_plugins.git;a=blob;f=qrcode.pm;hb=HEAD";
60 return(plugin => {safe => 1,
64 description => "qrcode plugin",
70 return(md5_hex(map {decode('utf8',$_)} @_));
76 if (not exists $param{code}
77 or not defined $param{code}
78 or not length $param{code}) {
79 # default to the link to qrcode for the time being
84 if (exists $param{fig}) {
85 $param{size} = '4' unless exists $param{width} and defined $param{width};
86 $param{margin} = '3' unless exists $param{height} and defined $param{height};
87 for (qw(size margin)) {
88 if ($param{$_} !~ /^\d+$/) {
89 error("invalid $_; must be an integer: $param{$_}");
92 my $md5 = code_md5($param{code},$param{size},$param{margin});
93 $image_loc = "$param{page}/${md5}.png";
94 will_render($param{page},$image_loc);
95 my $url = qq(\n\n<img class="qrcode" src=").urlto($image_loc,$param{destpage}).qq(" alt=").html_escape($param{code}).qq("/>\n);
96 if (-e "$config{destdir}/$image_loc") {
99 # this makes sure that we can write to the file result
100 writefile($image_loc, $config{destdir}, "");
101 my $img = plot_qrcode(decode('utf8',$param{code}),
102 {size => $param{size},
103 margin => $param{margin},
105 $img->write(file => "$config{destdir}/$image_loc");