EBook-MOBI
view release on metacpan or search on metacpan
lib/EBook/MOBI/MobiPerl/Util.pm view on Meta::CPAN
}
sub get_thumb_cover_image_data {
my $filename = shift;
die 'ERROR: GD not available ' unless is_loaded('GD');
## print STDERR "COVERIMAGE: $filename\n";
my $data = "";
if (not -e $filename) {
print STDERR "Image file does not exist: $filename\n";
return $data;
}
my $p = new GD::Image ("$filename");
my ($x, $y) = $p->getBounds();
# my $x = $p->width;
# my $y = $p->height;
## add_text_to_image ($p, $opt_covertext);
# pdurrant
# Make thumb 320 high and proportional width
# latest Mobipocket Creator makes Thumbnails 320 high
my $scaled = scale_gd_image ($p, 320/$y);
print STDERR "Resizing image $x x $y -> $x*320/$y x 320 -> scaled.jpg\n";
# my $scaled = scale_gd_image ($p, 180, 240);
# print STDERR "Resizing image $x x $y -> 180 x 240 -> scaled.jpg\n";
return $scaled->jpeg ();
}
sub scale_gd_image {
my $im = shift;
my $x = shift;
my $y = shift;
die 'ERROR: GD not available ' unless is_loaded('GD');
my ($w0, $h0) = $im->getBounds();
# my $w0 = $im->width;
# my $h0 = $im->height;
my $w1 = $w0*$x;
my $h1 = $h0*$x;
print STDERR "SCALE GD: $w0 $h0 -> $w1 $h1\n";
if (defined $y) {
$w1 = $x;
$h1 = $y;
}
my $res = new GD::Image ($w1, $h1);
$res->copyResized ($im, 0, 0, 0, 0, $w1, $h1, $w0, $h0);
return $res;
}
sub get_text_image {
my $width = shift;
my $height = shift;
my $text = shift;
# my $image = Image::Magick->new;
# $image->Set(size=>"$width x $height");
# $image->ReadImage('xc:white');
# $image->Draw (pen => "red",
# primitive => "text",
# x => 200,
# y => 200,
# font => "Bookman-DemiItalic",
# text => "QQQQ$text, 200, 200",
# fill => "black",
# pointsize => 40);
# $image->Draw(pen => 'red', fill => 'red', primitive => 'rectangle',
# points => '20,20 100,100');
# $image->Write (filename => "draw2.jpg");
}
sub get_gd_image_data {
my $im = shift;
my $filename = shift;
my $quality = shift;
$quality = -1 if not defined $quality;
#
# For some strange reason it does not work if using
# the gif file with size 600x800
#
## if ($filename =~ /\.gif/ or $filename =~ /\.GIF/) {
## return $im->gif ();
## }
if ($quality <= 0) {
return $im->jpeg ();
} else {
return $im->jpeg ($quality);
}
}
sub add_text_to_image {
my $im = shift;
my $text = shift;
my $x = $im->Get ("width");
my $y = $im->Get ("height");
if (defined $text and $text) {
print STDERR "DRAW TEXT: $text\n";
my $textim = get_text_image ($x, $y, $text);
$im->Draw (primitive => "text",
text => $text,
points => "50,50",
fill => "red",
pointsize => 72);
}
$im->Write (filename => "draw.jpg");
}
sub get_image_data {
my $filename = shift;
my $rescale = shift;
( run in 1.841 second using v1.01-cache-2.11-cpan-97f6503c9c8 )