Created
February 22, 2013 13:42
-
-
Save issm/5013455 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.10.0; | |
use warnings; | |
use utf8; | |
use Plack::Request; | |
use Encode; | |
use GD::Barcode::QRcode; | |
use Try::Tiny; | |
my $app = sub { | |
my $env = shift; | |
my $req = Plack::Request->new($env); | |
my $t = $req->param('t') // ''; | |
my $e = $req->param('e') // 'M'; | |
my $p = $req->param('p') // 0; | |
my $r = $req->param('r') || 1; | |
my $qr; | |
my $msg; | |
for my $v ( 1 .. 40 ) { | |
try { | |
$qr = GD::Barcode::QRcode->new( | |
encode_utf8($t), | |
{ Ecc => $e, Version => $v }, | |
); | |
} catch { | |
$msg = shift; | |
}; | |
last if $qr; | |
} | |
# error | |
if ( ! $qr ) { | |
return [ | |
500, | |
[ 'Content-Type' => 'text/plain' ], | |
[ $msg ], | |
]; | |
} | |
my $img_qr = $qr->plot(); | |
my $pr = 4 * $r; # 4 は 生成時に付加されるパディング [px] | |
my ($w0, $h0) = $img_qr->getBounds(); | |
my ($w1, $h1) = ( int($w0 * $r), int($h0 * $r) ); | |
my $img = GD::Image->new( | |
$w1 - 2 * ($pr - $p), | |
$h1 - 2 * ($pr - $p), | |
); | |
$img->copyResized( | |
$img_qr, | |
- ($pr - $p), - ($pr - $p), | |
0, 0, | |
$w1, $h1, | |
$w0, $h0, | |
); | |
return [ | |
200, | |
[ 'Content-Type' => 'image/png' ], | |
[ $img->png ], | |
]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment