Original content here is published under these license terms:
X
License Type:
Read Only
License Summary:
You may read the original content in the context in which it is published (at this web address). No other copying or use is permitted without written agreement from the author.
I was writing a little script to watermark some images on my site, and thought I would share it for anyone who can make use of it
All the main options are found at the top – so should be pretty simple to customize. The idea behind this system is that you upload it to your /cgi-bin (either password protected, or just somewhere safe), and then access it from your browser. You then upload your image, and it will scale the image and add your watermark to it. Simple as that!
#!/usr/local/bin/perl
use warnings;
use strict;
use CGI;
my $write_folder = "/var/home/user/www/watermarked";
my $write_url = "/watermarked"; # you can use the URL, or relative is just fine...
my $watermark_img = "/var/home/user/cgi-bin/cgi-bin/watermark/watermark.png";
my $transparency = "15"; # percentage of watermark image opacity
my $resize = "400x400";
my $IN = new CGI;
print "Content-Type: text/html \n\n";
if (!$IN->param('doit')) {
print qq|
<form action="watermark.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<br /><br />
<input type="submit" value="Watermark" >
<input type="hidden" name="doit" value="1" />
</form>
|;
} else {
my $file = $IN->param('image');
my $ext = lc((reverse split /\./, $file)[0]);
my $upload = $IN->upload('image');
#print "GOT: $file , and $ext";
my $write_file = CORE::time() . ".$ext";
#print "Writing to $write_folder/$write_file <br />";
open (UPLOADFILE, ">$write_folder/$write_file") || die $!;
binmode UPLOADFILE;
while ( <$upload> ) {
print UPLOADFILE;
}
close(UPLOADFILE);
my $command = qq|convert $write_folder/$write_file -resize $resize $write_folder/$write_file|;
my $command2 = qq|composite -dissolve $transparency -tile $watermark_img $write_folder/$write_file $write_folder/$write_file|;
`$command`;
`$command2`;
if ($ext !~ /jpg/i) {
my $new_filename = $write_file;
$new_filename =~ s/\.$ext/jpg/g;
`convert $write_folder/$write_file $write_folder/$new_filename`;
$write_file = $new_filename;
}
print qq|<img src="$write_url/$write_file" />|
}
Original content here is published under these license terms:
X
License Type:
Read Only
License Summary:
You may read the original content in the context in which it is published (at this web address). No other copying or use is permitted without written agreement from the author.
If you want to convert a US phone number into the format xxx-xxx-xxxx, then you can use this nice simple function.
sub convert_tel {
my $tel = $_[0];
if ($tel =~ /^\d+$/) {
$tel =~ s/([\d]{3,3})/$1-/g;
$tel =~ s/\-$//g;
return $tel;
} else {
return $tel;
}
}
That would convert a 10 digit number into 123-123-123-1. However, it would be nicer to have it formatted as xxx-xxx-xxxx, so to get that lets do this regex instead:
Thanks for taking the time to visit my blog. I've been working on it since November 2010, and aim to try and keep adding as many article as time permits.
I've been working as a Web-Design / Coder for many years now, and thought it was finally time for me to give back some of the stuff I've learned over the years. I'm always looking for new suggestions, so please visit the forum, or shoot me an email with suggestions!