Image Magick can to this, and on your server as you upload images. Working sample below. You will need: - CGI-executable abilities with perl - ImageMagick/PerlMagick installed on server - directory with two images and one to composite over these - the "composite" image must be a .png with a transparent background. The other images can be in any web format - Set the permissions on the images directory to writable, the script to executable Load it up in a browser and select the image, ImageMagick will get the image sizes and place the composite image in the center of the image you select. Adjust programming to locate your logo as required. #!/usr/bin/perl $imagesdir = '/virtual/path/to/images'; $imageurl = '/server/path/to/images'; $img1 = 'image1.jpg'; $img2 = 'image2.jpg'; $comp_img = 'overlay.png'; $thisScript = 'composite_sample.cgi'; use Image::Magick; %qs = &query; if ($qs{'base_img'} && $qs{'comp_image'}) { &comp_image; } else { &html('Select Images'); } sub html { $html = shift(@_); if ($html eq 'Select Images') { $content = qq¦ <div style="width: 50%; margin: auto"> <p>Below you will see three images. They are named $img1, $img2, and $comp_img. Select one of the first two and $comp_img will be composited over the top of it.</p> <form method="post" action="$thisScript"> <input type="hidden" name="comp_image" value="$comp_img"> <select name="base_img" id="base_img"> <option value="">SELECT IMAGE</option> <option value="$img1">$img1</option> <option value="$img2">$img2</option> </select> <input type="submit" value="Composite It"> </form> <div style="clear:both;"></div> </div> <h3 class="center">For your reference . . . </h3> <div style="float:left; text-align: center;"><img src="$imageurl/$img1" alt="$img1"><br><strong>$img1</strong></div> <div style="float:left; text-align: center;"><img src="$imageurl/$img2" alt="$img2"><br><strong>$img2</strong></div> <div style="float:left; text-align: center;"><img src="$imageurl/$comp_img" alt="$comp_img"><br><strong>$comp_img</strong></div> ¦; } elsif ($html eq 'Image Composited') { $content = qq¦ <p style="text-align:center">Below should be the composited image.</p> <div style="margin:auto"><img src="$imageurl/$qs{'composite'}" alt="$qs{'composite'}"></div> <p style="text-align:center"><a href="$thisScript">Test Another</a></p> ¦; } else { $error=$html; $html="No State:$error"; $content = "Oops. No HTML state."; } print "content-type: text/html\n\n"; print qq¦ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>$html</title> </head> <body> <h1 style="text-align:center;">$html</h1> $content </body> </html> ¦; exit 0; } ######################################################################### sub query{ my $method = $ENV{'REQUEST_METHOD'}; my ($query_string, $pair); my %query_hash; $query_string = $ENV{'QUERY_STRING'} if $method eq 'GET'; $query_string = <STDIN> if $method eq 'POST'; return undef unless $query_string; foreach $pair (split(/&/, $query_string)) { ($_qsname, $_qsvalue) = split(/=/, $pair); $_qsname =~ s/\+/ /g; $_qsname =~ s/%([\da-f]{2})/pack('c',hex($1))/ieg; $_qsvalue =~ s/\+/ /g; $_qsvalue =~ s/%([\da-f]{2})/pack('c',hex($1))/ieg; $query_hash{$_qsname} = $_qsvalue; } return %query_hash; } ##################################################### sub comp_image { my ($pic,$x,$height,$width,$mid_x,$mid_y,$nm,$ext,$file,$comp,$composite,$compw,$comph); unless ($qs{'base_img'} && $qs{'comp_image'}) { &error("Image missing: base $qs{'base_img'} comp $qs{'comp_image'}"); } $pic = Image::Magick->new; $x =$pic->Read("$imagesdir/$qs{'base_img'}"); if ($x) { &error("Error on reading image: $x"); } $pic->Set(background=>'white'); $height=$pic->GetAttribute('height'); $width= $pic->GetAttribute('width'); $comp=Image::Magick->new; $x=$comp->ReadImage("$imagesdir/$qs{'comp_image'}"); if ($x) { &error("Error on reading composite: $x"); } $comp->Set(background=>'white'); $comph=$comp->GetAttribute('height'); $compw= $comp->GetAttribute('width'); $mid_x = int(($width/2)-($compw/2)); $mid_y = int(($height/2)-($comph/2)); ($nm,$ext) = split(/\./,$qs{'base_image'}); $file = $nm . '_composite.' . $ext; $qs{'composite'} = $file; $composite=$pic->Clone(); $composite->Composite(image=>$comp,compose=>'over',geometry=>"+$mid_x+$mid_y"); $composite->Write("$imagesdir/$file"); &html('Image Composited'); } ######################################### sub error { $err = shift(@_); print "content-type: text/html\n\n"; print "ERROR: $err"; exit 0; }
|