convert some functions to use Imager
This commit is contained in:
parent
73152fa399
commit
c4bb713d94
1 changed files with 64 additions and 81 deletions
|
|
@ -254,50 +254,58 @@ Note: captcha images will NOT be synchronized to a CDN, even if other files are.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
use Imager;
|
||||||
sub addFileFromCaptcha {
|
sub addFileFromCaptcha {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $error = "";
|
my $error = "";
|
||||||
my $challenge;
|
my $challenge;
|
||||||
$challenge .= ('A'..'Z')[rand(26)] foreach (1..6);
|
$challenge .= ('A'..'Z')[rand(26)] foreach (1..6);
|
||||||
my $filename = "captcha.".$self->session->id->generate().".gif";
|
my $filename = "captcha.".$self->session->id->generate().".png";
|
||||||
my $image = Image::Magick->new();
|
my $image = Imager->new(xsize => 200, ysize => 50
|
||||||
$error = $image->Set(size=>'200x50');
|
) || warn Imager->errstr;
|
||||||
if($error) {
|
$image->box(
|
||||||
$self->session->log->warn("Error setting captcha image size: $error");
|
filled => 1,
|
||||||
}
|
color => 'grey',
|
||||||
$error = $image->ReadImage('xc:white');
|
) || warn $image->errstr;
|
||||||
if($error) {
|
$image->filter(
|
||||||
$self->session->log->warn("Error initializing image: $error");
|
type => 'noise',
|
||||||
}
|
amount => 100,
|
||||||
$error = $image->AddNoise(noise=>"Multiplicative");
|
subtype => 1,
|
||||||
if($error) {
|
) || warn $image->errstr;
|
||||||
$self->session->log->warn("Error adding noise: $error");
|
$image->filter(
|
||||||
}
|
type => 'autolevels',
|
||||||
# AddNoise generates a different average color depending on library. This is ugly, but the best I can see for now
|
usat => 0.5,
|
||||||
$error = $image->Annotate(font=>WebGUI::Paths->share.'/default.ttf', pointsize=>40, skewY=>0, skewX=>0, gravity=>'center', fill=>'#ffffff', antialias=>'true', text=>$challenge);
|
);
|
||||||
if($error) {
|
|
||||||
$self->session->log->warn("Error Annotating image: $error");
|
my $font = Imager::Font->new(file => WebGUI::Paths->share . '/default.ttf');
|
||||||
}
|
$image->align_string(
|
||||||
$error = $image->Draw(primitive=>"line", points=>"5,5 195,45", stroke=>'#ffffff', antialias=>'true', strokewidth=>2);
|
string => $challenge,
|
||||||
if($error) {
|
font => $font,
|
||||||
$self->session->log->warn("Error drawing line: $error");
|
color => 'white',
|
||||||
}
|
x => $image->getwidth / 2,
|
||||||
$error = $image->Blur(geometry=>"9");
|
y => $image->getheight / 2,
|
||||||
if($error) {
|
halign => 'center',
|
||||||
$self->session->log->warn("Error blurring image: $error");
|
valign => 'center',
|
||||||
}
|
size => 40,
|
||||||
$error = $image->Set(type=>"Grayscale");
|
) || warn $image->errstr;
|
||||||
if($error) {
|
$image->line(
|
||||||
$self->session->log->warn("Error setting grayscale: $error");
|
color => 'white',
|
||||||
}
|
x1 => 5, y1 => 5,
|
||||||
$error = $image->Border(fill=>'black', width=>1, height=>1);
|
x2 => 195, y2 => 45,
|
||||||
if($error) {
|
endp => 1,
|
||||||
$self->session->log->warn("Error setting border: $error");
|
aa => 1,
|
||||||
}
|
) || warn $image->errstr;
|
||||||
$error = $image->Write($self->getPath($filename));
|
$image->filter(
|
||||||
if($error) {
|
type => 'gaussian',
|
||||||
$self->session->log->warn("Error writing image: $error");
|
stddev => 1,
|
||||||
}
|
) || warn $image->errstr;
|
||||||
|
$image = $image->convert(
|
||||||
|
preset => 'gray',
|
||||||
|
) || warn $image->errstr;
|
||||||
|
$image->box(color => 'black'
|
||||||
|
) || warn $image->errstr;
|
||||||
|
$image->write(file => $self->getPath($filename)
|
||||||
|
) || warn $image->errstr;
|
||||||
return ($filename, $challenge);
|
return ($filename, $challenge);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -851,28 +859,14 @@ sub generateThumbnail {
|
||||||
$self->session->log->warn("Can't generate a thumbnail for something that's not an image.");
|
$self->session->log->warn("Can't generate a thumbnail for something that's not an image.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
my $image = Image::Magick->new;
|
my $image = Imager->new;
|
||||||
my $error = $image->Read($self->getPath($filename));
|
$image->read( file => $self->getPath($filename) );
|
||||||
if ($error) {
|
my ($x, $y) = ($image->getwidth, $image->getheight);
|
||||||
$self->session->log->error("Couldn't read image for thumbnail creation: ".$error);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
my ($x, $y) = $image->Get('width','height');
|
|
||||||
my $n = $thumbnailSize;
|
my $n = $thumbnailSize;
|
||||||
if ($x > $n || $y > $n) {
|
if ($x > $n || $y > $n) {
|
||||||
my $r = $x>$y ? $x / $n : $y / $n;
|
$image->scale(xpixels => $n, ypixels => $n, type => 'min');
|
||||||
$x /= $r;
|
|
||||||
$y /= $r;
|
|
||||||
if($x < 1) { $x = 1 } # Dimentions < 1 cause Scale to fail
|
|
||||||
if($y < 1) { $y = 1 }
|
|
||||||
$image->Scale(width=>$x,height=>$y);
|
|
||||||
$image->Sharpen('0.0x1.0');
|
|
||||||
}
|
|
||||||
$error = $image->Write($self->getPath.'/'.'thumb-'.$filename);
|
|
||||||
if ($error) {
|
|
||||||
$self->session->log->error("Couldn't create thumbnail: ".$error);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
$image->write(file => $self->getPath . '/thumb-' . $filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -938,15 +932,9 @@ The file to generate a thumbnail for.
|
||||||
sub getSize {
|
sub getSize {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $filename = shift;
|
my $filename = shift;
|
||||||
my $image = Image::Magick->new;
|
my $image = Imager->new;
|
||||||
my $error = $image->Read($self->getPath($filename));
|
$image->read(file => $self->getPath($filename));
|
||||||
if ($error) {
|
return ($image->getwidth, $image->getheight);
|
||||||
$self->session->log->error("Couldn't read image for size reading: ".$error);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
my ($x, $y) = $image->Get('width','height');
|
|
||||||
|
|
||||||
return($x, $y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1266,13 +1254,8 @@ sub getSizeInPixels {
|
||||||
$self->session->log->error("Can't check the size of something that's not an image.");
|
$self->session->log->error("Can't check the size of something that's not an image.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
my $image = Image::Magick->new;
|
$image->read(file => $self->getPath($filename));
|
||||||
my $error = $image->Read($self->getPath($filename));
|
return ($image->getwidth, $image->getheight);
|
||||||
if ($error) {
|
|
||||||
$self->session->log->error("Couldn't read image to check the size of it: ".$error);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return $image->Get('width','height');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue