Skip to content

Instantly share code, notes, and snippets.

@DQNEO
Created November 28, 2011 14:48
Show Gist options
  • Save DQNEO/1400659 to your computer and use it in GitHub Desktop.
Save DQNEO/1400659 to your computer and use it in GitHub Desktop.
GDモジュールを使ったサンプルコード
use strict;
use warnings;
use GD;
my @files= glob("*.JPG");
my $n = @files;
print "handling $n files...\n";
Stopwatch::start();
&resize_all(@files);
my $time = Stopwatch::stop();
print "total ".$time." sec for ".$n." pics\n";
print "for each " .($time/$n) ." sec.\n" unless $n;
sub resize_all {
my $counter;
for (@_) {
&resize("small\\$_", $_ , 0.2 );
print "$_ ";
print ++$counter."/".$n." \n";
}
}
sub resize {
my ($file_out, $file_in , $rate ) = @_;
my $in_img = GD::Image->newFromJpeg( $file_in ,1); # 引数の1は、TrueColorを表す。
my ( $old_width, $old_height ) = $in_img->getBounds();
my ( $new_width, $new_height ) = ($old_width * $rate, $old_height * $rate);
my $out_img = GD::Image->new( $new_width, $new_height, 1 ); # 1はTrueColorを表す。
$out_img->copyResampled($in_img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height );
open( IMG, ">$file_out" );
binmode IMG;
print IMG $out_img->jpeg( 100 );
close( IMG );
}
exit;
#========= 処理時間を計測する ==================
package Stopwatch;
my $start;
my $end;
my $total_time;
sub start {
$start = time;
}
sub stop {
$end = time;
$total_time = $end - $start;
}
sub print {
print "total ".$total_time." sec for ".$n." pics\n";
print "for each " .($total_time/$n) ." sec.\n";
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment