blib/lib/Mojolicious/Plugin/CaptchaRenderer.pm

filter: Statementstmt 94.4% 17 / 18 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 92.3% 24 / 26 CC 2 SCAR 3.1
CC
2
Cov
90%
BEGIN
2.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package Mojolicious::Plugin::CaptchaRenderer;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.1
11 0 ▶ die 'Module Image::Magick not properly installed' unless eval { require Image::Magick; 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Image::Magick; 1 } } 1 0 1
12 }
13  
14 our $VERSION = 0.02;
15  
16 sub register {
17 my ($self,$app,$conf) = @_;
18
19 $conf = {
20 color => 'black',
21 bgcolor => 'white',
22 size => 60,
23 wave_amplitude => 7,
24 wave_length => 80,
25 %$conf,
26 };
27
28 $app->renderer->add_helper(
29 captcha => sub {
30 my ($self,$code) = @_;
31
32 my $img = Image::Magick->new(size => '400x400', magick => 'png');
33 my $x; $x = $img->Read('gradient:#ffffff-#ffffff');
34
35 $x = $img->Annotate(
36 pointsize => $conf->{'size'},
37 fill => $conf->{'color'},
38 text => $code,
39 geometry => '+0+' . $conf->{'size'},
40 $conf->{'font'} ? (font => $conf->{'font'}) : (),
41 );
42
43 warn $x if $x;
44 $x = $img->Wave(amplitude => $conf->{'wave_amplitude'}, wavelength => $conf->{'wave_length'});
45 warn $x if $x;
46 $x = $img->Trim;
47 warn $x if $x;
48
49 my $body = '';
50
51 {
52 my $fh = File::Temp->new(UNLINK => 1, DIR => $ENV{MOJO_TMPDIR} || File::Spec->tmpdir);
53 $x = $img->Write('png:' . $fh->filename);
54 open $fh, '<', $fh->filename;
55 local $/;
56 $body = <$fh>;
57 }
58 return $body;
59 }
60 );
61 }
62  
63 1;
64  
65 =head1 NAME
66  
67 Mojolicious::Plugin::CaptchaRenderer - captcha renderer for Mojolicious framework
68  
69 =head1 VERSION
70  
71 0.02
72  
73 =head1 SYNOPSIS
74  
75 # Mojolicious::Lite
76 plugin captcha_renderer => { size => 20, color => 'blue', wave_amplitude => 4};
77 get '/img/code.png' => sub {
78 my $self = shift;
79 $self->render_data($self->captcha('cool captcha code'));
80 }
81
82 # Mojolicious
83 $self->plugin(captcha_renderer => { size => 20, color => 'blue', wave_amplitude => 4});
84
85 package MyApp::MyController;
86
87 sub my_action {
88 my $self = shift;
89 $self->render_data($self->captcha('cool captcha code'));
90 }
91
92 =head1 OPTIONS
93  
94 =over 4
95  
96 =item size - font size. By default 60
97  
98 =item color - font color. By default black
99  
100 =item font - font name. By default undef
101  
102 =item bgcolor - captcha background color. By default white
103  
104 =item wave_amplitude - amplitude of wave in captcha. By default 7
105  
106 =item wave_length - length of wave in captcha. By dafault 80
107  
108 =back
109  
110 =head1 SUPPORT
111  
112 =over 4
113  
114 =item * Repository
115  
116 L<http://github.com/konstantinov/Mojolicious-Plugin-CaptchaRenderer>
117  
118 =back
119  
120 =head1 SEE ALSO
121  
122 L<Mojolicious>, L<Mojolicious::Plugin>, L<Mojolicious::Lite>
123  
124 =head1 COPYRIGHT & LICENSE
125  
126 Copyright 2010 Dmitry Konstantinov. All right reserved.
127  
128 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
↑ Summary