GD-SecurityImage
view release on metacpan or search on metacpan
lib/GD/SecurityImage/GD.pm view on Meta::CPAN
return;
}
sub out {
# return $image_data, $image_mime_type, $random_number
my($self, @args) = @_;
my %opt = @args % 2 ? () : @args;
my $i = $self->{image};
my $type;
if ( $opt{force} && $i->can($opt{force}) ){
$type = $opt{force};
}
else {
# Define the output format.
foreach my $f ( FORMATS ) {
if ( $i->can( $f ) ) {
$type = $f;
last;
}
}
}
my @iargs = ();
if ( $opt{'compress'} ) {
push @iargs, MAXCOMPRESS if $type eq 'png';
push @iargs, $opt{'compress'} if $type eq 'jpeg';
}
return $i->$type(@iargs), $type, $self->{_RANDOM_NUMBER_};
}
sub gdbox_empty { return shift->{GDBOX_EMPTY} }
sub gdfx {
# Sets the font for simple GD usage.
# Unfortunately, Image::Magick does not have a similar interface.
my $self = shift;
my $font = shift || return;
$font = lc $font;
# GD' s standard fonts
my %f = map { lc $_ => $_ } GDFONTS;
if ( exists $f{$font} ) {
$font = $f{$font};
return GD::Font->$font();
}
}
sub _insert_text_ttf_scramble {
my($self, $key, $ctext) = @_;
require Math::Trig;
my @char;
my $anglex;
my $total = 0;
my $space = [ $self->ttf_info( 0, 'A' ), 0, q{ } ];
my @randomy;
my $sy = $space->[CHY] || 1;
## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
push @randomy, $_, - $_ foreach $sy*1.2,$sy, $sy/2, $sy/4, $sy/8;
## use critic
foreach (split m{}xms, $key) { # get char parameters
$anglex = $self->random_angle;
$total += $space->[CHX];
push @char, [$self->ttf_info($anglex, $_), $anglex, $_], $space, $space, $space;
}
$total *= 2;
my @config = ($ctext, $self->{font}, $self->{ptsize});
my($x,$y);
foreach my $box (reverse @char) {
$x = $self->{width} / 2 + ($box->[CHX] - $total);
$y = $self->{height} / 2 + $box->[CHY];
$y += $randomy[int rand @randomy];
$self->{image}->stringFT(@config, Math::Trig::deg2rad($box->[CHAR]), $x, $y, $box->[ANGLE]);
$total -= $space->[CHX];
}
return;
}
sub _insert_text_ttf_normal {
my($self, $key, $ctext) = @_;
require Math::Trig;
# don' t draw. we just need info...
my $info = sub {
my $txt = shift;
my $ang = shift || 0;
$ang = Math::Trig::deg2rad($ang) if $ang;
my @box = GD::Image->stringFT(
$ctext, $self->{font}, $self->{ptsize}, $ang, 0, 0, $txt
);
if ( not @box ) { # use fake values instead of die-ing
$self->{GDBOX_EMPTY} = 1; # set this for error checking.
$#box = BOX_SIZE;
# lets initialize to silence the warnings
$box[$_] = 1 for 0..$#box;
}
return @box;
};
my(@box, $x, $y);
my $tl = $self->{_TEXT_LOCATION_};
if ( $tl->{_place_} ) {
# put the text to one of the four corners in the image
my $white = $self->cconvert( [ RGB_WHITE ] );
my $black = $self->cconvert($ctext);
if ( $tl->{gd} ) { # draw with standard gd fonts
$self->place_gd($key, $tl->{x}, $tl->{y});
return; # by-pass ttf method call...
}
else {
@box = $info->($key);
$x = $tl->{x} eq 'left'
? 0
: ( $self->{width} - ($box[LOWRIGHTX] - $box[LOWLEFTX]) )
;
$y = $tl->{y} eq 'up'
? ( $box[LOWLEFTY] - $box[UPLEFTY] )
: $self->{height} - 2
;
if ($tl->{strip}) {
$self->add_strip(
$x, $y, $box[LOWRIGHTX] - $box[LOWLEFTX], $box[LOWLEFTY] - $box[UPLEFTY]
);
}
}
}
else {
@box = $info->($key);
$x = ($self->{width} - ($box[LOWRIGHTX] - $box[LOWLEFTX])) / 2;
$y = ($self->{height} - ($box[UPLEFTY] - $box[LOWLEFTY])) / 2;
}
# this needs a fix. adjust x,y
$self->{angle} = $self->{angle} ? Math::Trig::deg2rad($self->{angle}) : 0;
$self->{image}->stringFT( $ctext, $self->{font}, $self->{ptsize}, $self->{angle}, $x, $y, $key );
return;
}
sub _insert_text_gd_scramble {
my($self, $key, $ctext) = @_;
# without ttf, we can only have 0 and 90 degrees.
my @char;
my @styles = qw(string stringUp);
my $style = $styles[int rand @styles];
foreach (split m{}xms, $key) { # get char parameters
push @char, [ $_, $style ], [ q{ }, 'string' ];
$style = $style eq 'string' ? 'stringUp' : 'string';
}
my $sw = $self->{gd_font}->width;
my $sh = $self->{gd_font}->height;
my($x, $y, $m);
my $total = $sw * @char;
foreach my $c (@char) {
$m = $c->[1];
$x = ($self->{width} - $total) / 2;
$y = $self->{height}/2 + ($m eq 'string' ? -$sh : $sh/2) / 2;
$total -= $sw * 2;
$self->{image}->$m($self->{gd_font}, $x, $y, $c->[0], $ctext);
}
return;
}
sub _insert_text_gd_normal {
my($self, $key, $ctext) = @_;
my $sw = $self->{gd_font}->width * length $key;
my $sh = $self->{gd_font}->height;
my $x = ($self->{width} - $sw) / 2;
my $y = ($self->{height} - $sh) / 2;
$self->{image}->string($self->{gd_font}, $x, $y, $key, $ctext);
return;
}
sub insert_text {
# Draw text using GD
my $self = shift;
my $method = shift;
my $key = $self->{_RANDOM_NUMBER_};
my $ctext = $self->{_COLOR_}{text};
if ($method eq 'ttf') {
$self->{scramble} ? $self->_insert_text_ttf_scramble( $key, $ctext )
: $self->_insert_text_ttf_normal( $key, $ctext )
;
}
else {
$self->{scramble} ? $self->_insert_text_gd_scramble( $key, $ctext )
: $self->_insert_text_gd_normal( $key, $ctext )
;
}
return;
}
sub place_gd {
my($self, $key, $tx, $ty) = @_;
my $tl = $self->{_TEXT_LOCATION_};
my $black = $self->cconvert($self->{_COLOR_}{text});
my $white = $self->cconvert($tl->{scolor});
my $font = GD::Font->Tiny;
my $fx = (length($key)+1)*$font->width;
my $x1 = $self->{width} - $fx;
my $y1 = $ty eq 'up' ? 0 : $self->{height} - $font->height;
if ($ty eq 'up') {
if($tx eq 'left') {
$self->filledRectangle(0, $y1 , $fx , $font->height+2, $black);
$self->filledRectangle(1, $y1+1, $fx-1, $font->height+1, $white);
}
( run in 1.411 second using v1.01-cache-2.11-cpan-71847e10f99 )