WebTools

 view release on metacpan or  search on metacpan

lib/modules/ImagePwd.pm  view on Meta::CPAN

package ImagePwd;

use Image::Magick;

use strict;

##############################################
# ImagePwd module written by Julian Lishev
# This module is part of WebTools package!
# Privacy and terms are same as WebTools!
##############################################
# Prerequirment: Image::Magick (PerlMagick)
##############################################


=head2 $img = ImagePwd::new (%inp);

 %inp hash can contain follow members:
 
   len      - Count of password string.
   width    - Width of generated image.
   height   - Height of generated image.
   f_max    - Maximum size of font.
   f_min    - Minimum size of font.
   fixed    - If you want to use one color for all chars please set this
              to '1' other else to '0'
   rot      - Maximum(minimum) rotate angle (0 up to 90)
   fonts    - Pointer to array of fonts that will be used for image password.
              (NT users should specify full path to fonts!)
   bgcolor  - Use only if you want to fix used bgcolor for image!!!
   color    - Use only if you want to fix used color for text!!!
   quality  - Quality of new generated image (variate of 0 up to 128)
   password - This member contain 'password' with length 'len'
   cell     - Set to '1' if you want over image to be placed cells,
              drawn with random color.
   
Note: Font sizes variates from 'f_min' up to 'f_max' pixels.

=cut

BEGIN
 {
  use vars qw($VERSION @ISA @EXPORT);
  $VERSION = "1.16";
  @ISA = qw(Exporter);
  @EXPORT = qw();
  srand();
 }

sub new
{ 
 my $proto = shift;
 my $class = ref($proto) || $proto;
 my $this = {};
 
 my %inp = @_;

 $this->{'len'} = $inp{'len'} || '4';
 $this->{'len'} =~ s/^.*?(\d*).*?$/$1/sgi;

 $this->{'width'} = $inp{'width'} || '200';
 $this->{'width'} =~ s/^.*?(\d*).*?$/$1/sgi;

 $this->{'height'} = $inp{'height'} || '60';
 $this->{'height'} =~ s/^.*?(\d*).*?$/$1/sgi;

 $this->{'f_max'} = $inp{'f_max'} || 32;
 $this->{'f_max'} =~ s/^.*?(\d*).*?$/$1/sgi;
 
 $this->{'f_min'} = $inp{'f_min'} || 14;
 $this->{'f_min'} =~ s/^.*?(\d*).*?$/$1/sgi;
 

lib/modules/ImagePwd.pm  view on Meta::CPAN

 if(defined($_[0]))
  {
   my $code = '$this->{'."'$name'".'} = $_[0];';
   eval $code;
   return($_[0]);
  }
 else
  {
   my $code = '$code = $this->{'."'$name'".'};';
   eval $code;
   return($code);
  }
}

sub len       { shift->_set_val('len', @_); }
sub width     { shift->_set_val('width', @_); }
sub height    { shift->_set_val('height', @_); }
sub f_max     { shift->_set_val('f_max', @_); }
sub f_min     { shift->_set_val('f_min', @_); }
sub fixed     { shift->_set_val('fixed', @_); }
sub rot       { shift->_set_val('rot', @_); }
sub quality   { shift->_set_val('quality', @_); }
sub bgcolor   { shift->_set_val('bgcolor', @_); }
sub color     { shift->_set_val('color', @_); }
sub fonts     { shift->_set_val('fonts', @_); }
sub password  { shift->_set_val('password', @_); }
sub cell      { shift->_set_val('cell', @_); }

sub ImagePassword
{
 my $obj = shift;
 my $i;
 my $len = $obj->len();
 my $width = $obj->width();
 my $height = $obj->height();
 my $f_max = $obj->f_max();
 my $f_min = $obj->f_min();
 my $fixed = $obj->fixed();
 my $rot = $obj->rot();
 my $quality = $obj->quality();
 my $f_bgcolor = $obj->bgcolor();
 my $f_color = $obj->color();
 my $cell = $obj->cell();
 my ($cx,$cy);
 my $str = $obj->password() || _generatestring($len);
 $obj->password($str);
 my $i_x = 0;
 my @pnts = ();

 $cy = rand(20) + $height/2;
 $cx = rand(10);
 my $a_cx = $cx * $len;
 for ($i=0;$i<$len;$i++)
  {
   my $sz = $obj->f_min() + rand($obj->f_max() - $obj->f_min());
   push (@pnts,$sz);
   $i_x += $sz;
  }
 my $addl = $cx;
 $cx = ($width/2) - (($i_x+$a_cx)/2);
 my $image = Image::Magick->new(compression=>'LosslessJPEG',quality=>$quality);
 $image->Set(size=>$width.'x'.$height);
 my $bgcolor = $f_bgcolor || _generatebgcolor();
 my $color = $f_color || _choosecolor($bgcolor);
 $image->Set(antialias=>'True');
 $image->ReadImage('xc:'.$bgcolor);
 for ($i=0;$i<$len;$i++)
  {
   my $char = substr($str,$i,1);
   my $sz = $pnts[$i];
   my $rot = int(rand(10)) > 5 ? (-1)*rand($rot) : rand($rot);
   $image->Set(font=>$obj->_choosefont());
   $image->Annotate(pointsize=>$sz, rotate=>$rot, fill=>$color, text=>$char,x=>$cx,y=>$cy);
   if(!$fixed) { $color = $f_color || _choosecolor($bgcolor); }
   $cx += $sz+$addl;
   $cy = rand(20) + $height/2;
  }
 if($cell)
 {
  my $y;
  my $xstep = int(rand(12)+8);
  my $rowcolor = '#'.hex(rand(155)+100).hex(rand(55)+200).hex(rand(255));
  my $colcolor = '#'.hex(rand(100)+155).hex(rand(100)+155).hex(rand(200)+55);
  for ($y = 0; $y<int($height/$xstep)+1; $y++)
   {
    $image->Draw(stroke=>$rowcolor, primitive=>'line', points=>"0,".$y*$xstep.",$width,".$y*$xstep);
   }
  $image->Draw(stroke=>$rowcolor, primitive=>'line', points=>"0,".($height-2).",".$width.",".($height-2));
  my $x;
  my $ystep = int(rand(10)+10);
  for ($x = 0; $x<int($width/$ystep)+1; $x++)
   {
    $image->Draw(stroke=>$colcolor, primitive=>'line', points=>$x*$ystep.",0,".$x*$ystep.",$height");
   }
  $image->Draw(stroke=>$colcolor, primitive=>'line', points=>($width-2).",0,".($width-2).",$height");
 }
 return($image);
}

sub _generatestring
{
 my $size = shift(@_);
 my ($i,$str,$char) = ();
 
 for ($i = 0; $i < $size; $i++)
  {
   $char = chr(rand(ord('Z')-33)+33);
   if($char =~ m/^[A-Za-z0-9\@\#\$\&\(\)\=\+\\\[\]\?\/]$/s)
     {
      $str .= $char;
     }
   else {redo;}
  }
 return($str);
}

sub _generatebgcolor
{
 my @colors = ('black','blue','gray','green');
 my $colr = rand($#colors+1);
 return($colors[$colr]);



( run in 1.213 second using v1.01-cache-2.11-cpan-39bf76dae61 )