Created
June 22, 2011 14:33
-
-
Save kkosuge/1040212 to your computer and use it in GitHub Desktop.
画像ファイルのヘッダ見て拡張子つけてくれるやつ。カレントディレクトリにある全てのそれっぽい画像ファイルに操作する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
my @file = glob "*"; | |
foreach my $f (@file){ | |
if($f=~/(.*)\.(.*)/){ | |
unless($2=~/jpg|png|gif|bmp|svg/){ | |
&rename_img($f); | |
} | |
} | |
} | |
sub rename_img{ | |
my $file_name = shift; | |
open(FH, $file_name); | |
my $header = <FH>; | |
if ($header =~ /^\x89PNG/m){ | |
rename $file_name, "$file_name.png"; | |
}elsif($header =~ /^GIF8[79]a/){ | |
rename $file_name, "$file_name.gif"; | |
}elsif($header =~ /^\xff\xd8/){ | |
rename $file_name, "$file_name.gif"; | |
}elsif($header =~ /^BM/){ | |
rename $file_name, "$file_name.bmp"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment