Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / LabelDesigner.pm
blob046315e3d05385a83eb7256a16aa77925fa3565d
1 package SGN::Controller::LabelDesigner;
3 use Moose;
4 use File::Slurp;
5 use Barcode::Code128;
6 use CXGN::QRcode;
7 use URI::Encode qw(uri_encode uri_decode);
8 use URI::FromHash 'uri';
10 BEGIN { extends 'Catalyst::Controller'; }
12 sub interactive_barcoder_main :Path('/tools/label_designer') Args(0) {
13 my $self = shift;
14 my $c = shift;
16 if (!$c->user()) { # redirect to login page
17 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
18 return;
21 $c->stash->{template} = '/tools/label_designer/index.mas';
24 sub barcode_preview :Path('/tools/label_designer/preview') {
25 my $self = shift;
26 my $c = shift;
27 my $uri = URI::Encode->new( { encode_reserved => 0 } );
28 my $content = $uri->decode($c->req->param("content"));
29 my $type = $uri->decode($c->req->param("type"));
30 my $size = $uri->decode($c->req->param("size"));
32 #print STDERR "Content is $content and type is $type and size is $size\n";
34 if ($type eq 'Code128') {
36 print STDERR "Creating barcode 128\n";
38 my $barcode_object = Barcode::Code128->new();
39 $barcode_object->option("scale", $size);
40 $barcode_object->option("font_align", "center");
41 $barcode_object->option("padding", 5);
42 $barcode_object->option("show_text", 0);
43 $barcode_object->barcode($content);
44 my $barcode = $barcode_object->gd_image();
46 $c->res->headers->content_type('image/png');
47 $c->res->body($barcode->png());
49 } elsif ($type eq 'QRCode') {
51 print STDERR "Creating QR Code\n";
53 $c->tempfiles_subdir('barcode');
54 my ($file_location, $uri) = $c->tempfile( TEMPLATE => [ 'barcode', 'bc-XXXXX'], SUFFIX=>'.jpg');
56 my $barcode_generator = CXGN::QRcode->new(
57 text => $content,
58 size => $size,
59 margin => 0,
60 version => 0,
61 level => 'M'
63 my $barcode_file = $barcode_generator->get_barcode_file($file_location);
65 my $qrcode_path = $c->path_to($uri);
67 $c->res->headers->content_type('image/jpg');
68 my $output = read_file($qrcode_path);
69 $c->res->body($output);
75 return 1;