|
You can use this code and save it as watermark.cgi or whatever. Place the watermark.jpg in the same folder as the cgi.
#!/usr/bin/perl
$position="SouthWest"; # can be NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
$watermark_pic = "watermark.jpg";
$composite = "/usr/bin/composite";
BEGIN {
$debug = 0;
if ($debug) {
print "Content-type: text/plain\n\n";
open (STDERR, ">&STDOUT");
select(STDERR); $| = 1;
select(STDOUT); $| = 1;
}
}
$startdir = "$ENV{DOCUMENT_ROOT}";
"/usr/bin:/usr/local/bin" =~ /(.*)/; #untaint path
$ENV{'PATH'} = $1;
&getinput;
print "Content-type: text/html\n\n<html><body>\n\n";
open (STDERR, ">&STDOUT");
$cgi{'dir'} = &canonical_path($cgi{'dir'});
if ($cgi{'submit'} eq "Watermark") {
&do_dir("$cgi{'dir'}");
} else {
if ($cgi{'dir'} eq "") {
&list_dir($startdir);
} else {
&list_dir("$cgi{'dir'}");
}
}
print "\n<body>\n</html>\n";
sub getinput() {
@pairs= split(/&/,$ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$cgi{$name} = $value;
}
}
sub do_dir() {
$dir = $_[0];
$dir =~ /(.*)/;
$dir = $1;
print "<pre>reading $dir\n\n";
opendir(DIR, $dir) or die "Could not open dir $dir: $!";
my @files = readdir(DIR);
closedir DIR;
for $file (@files) {
next if ($file =~ m/^\./);
$file =~ /(.*)/; #untaint file
$file = $1;
# print "processing $dir/$file\n";
if (-d "$dir/$file") {
&do_dir("$dir/$file");
} else {
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat("$dir/$file");
next if ($size < 20000); # don't watermark thumbnails
if ("$dir/$file" =~ m/jpe?g/i) {
print "marking $dir/$file\n";
open(TEST, ">$dir/test.txt") or die "Can't open $dir/test.txt: $!";
print TEST "1";
close TEST;
system("$composite -compose Bumpmap -watermark 30x30 -gravity $position $watermark_pic $dir/$file $dir/$file");
}
}
}
print "\n\n</pre>\n";
}
sub list_dir() {
$dir = $_[0];
print qq|Select a directory to watermark:<br>
<form action="$ENV{'SCRIPT_NAME'}" method="GET">
<ul>
|;
opendir(DIR, $dir) or die "Could not open dir $dir: $!";
my @files = readdir(DIR);
closedir DIR;
for $file (@files) {
next if ($file =~ m/^\.$/);
if (-d "$dir/$file") {
my $path = &canonical_path("$dir/$file");
my $path_enc = &urlencode($path);
opendir(DIR, $path);
my @jpegs = sort( grep( /jpe?g/i, readdir(DIR) ) );
closedir DIR;
if ($#jpegs > 0) {
$jpeg_list = "<ul><li>$jpegs[0]</li><li>$jpegs[1]...</li></ul>\n";
} else {
$jpeg_list = "";
}
print qq|<li>
<input type="radio" name="dir" value="$path_enc">
<a href="$ENV{'SCRIPT_NAME'}?dir=$path_enc">
$path
</a>
$jpeg_list
</li>|;
}
}
print qq|
</ul>
<input type="submit" name="submit" value="Watermark">
</form>
|;
}
sub urlencode{
my($text)=$_[0];
for (0..255) {
$escapes{chr($_)} = sprintf("%%haha2X", $_);
}
## Regular expression for escaping special chars.
$text =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
return $text;
}
sub canonical_path {
my $in = $_[0];
my @inparts = split("/", $in);
my @outparts;
foreach $part (@inparts) {
if ($part eq "..") {
pop(@outparts);
} else {
push(@outparts, $part);
}
}
return join("/", @outparts);
}
|