Code: [show] | [select all]
=info
Auth: vicyang/523066680
Date: 2018-04
=cut
use Imager;
use Imager::Matrix2d;
use Data::Dumper;
use Time::Local;
use Encode;
STDOUT->autoflush(1);
my $file = "src.jpg";
my $img = Imager->new();
$img->read(file=>$file) or die "Cannot load $image_source: ", $image->errstr;
my ($width, $height) = ( $img->getwidth(), $img->getheight() );
my $fontsize = int($height/40);
my $skip = $fontsize * 5;
print "$width $height\n";
$blue = Imager::Color->new(0xB0, 0xB0, 0xB0, 0x60);
$font = Imager::Font->new(file => 'C:/windows/fonts/msyh.ttf',
color => $blue,
size => $fontsize );
my $matrix = Imager::Matrix2d->rotate( degrees => 45 );
$font->transform(matrix => $matrix);
my @str;
my $date = time_format( time() );
push @str, "Perl专栏 "x50;
push @str, "$date "x50;
for my $y ( 1 .. 25 )
{
$img->string(
font => $font,
text => decode('utf8', $str[ $y % 2 ] ),
x => 0,
y => $y * $skip,
color => $blue
);
}
$img->write( file => "result.jpg") or die $img->errstr;
sub time_format
{
my ($sec, $min, $hour, $day, $mon, $year) = localtime( shift );
$mon += 1;
$year += 1900;
return sprintf "%d-%02d-%02d %02d:%02d", $year,$mon,$day,$hour,$min;
}