#!/usr/bin/perl -w use Image::Magick; use LWP::Simple; use Mysql; # These two lines are needed to run on Windows -- ImageMagick will produce some funny results. # use FileHandle; # autoflush STDOUT 1; my $qin = &get_qstring; my $fmt = "png"; print( "Content-type:image/$fmt\n\n" ); # Get filename from URL my $url=$qin->{url}; $url=~m/.*?\/([^\/]*)$/; my $filename = $1; # LWP Fetch Image my $image_src = get $url; # Save image open(HANDLE,">","images/P$filename"); binmode HANDLE; print HANDLE $image_src; close HANDLE; # ... open it back up # If you can find a way to pipe the binary results from LWP into here, you save a lot of work. $image = new Image::Magick; $image->Read("images/P$filename"); # Size to bounds my $h = $image->Get( 'rows' ); my $w = $image->Get( 'columns' ); my $nh; my $nw; if ( $h > 50 && $w > 50 ) { if ( $h > $w ) { my $f=$w/51; $nw=51; $nh=$h/$f; } else { my $f=$h/51; $nh=51; $nw=$w/$f; } $image->Resize( geometry=>$nw."x".$nh ); } $image->Crop( geometry=>"50x50", x=>$qin->{x}, y=>$qin->{y} ); # Adjust B&C $image->Modulate( brightness=>105, saturation=>95 ); # Open mask $image_mask = new Image::Magick; $image_mask->Read("png:circ_mask50.png"); # Perform Mask $image_mask->Composite( image=>$image, compose=>'In', gravity=>'center', background=>"#FF00FF" ); # Open Border $image_mask2 = new Image::Magick; $image_mask2->Read("png:circ_border50.png"); # Place Border $image_mask2->Composite( image=>$image_mask, compose=>'Over', gravity=>'center', opacity=>0 ); # Output to client binmode STDOUT; $image_mask2->Write("$fmt:-"); # Write image to disk and update cache $image_mask2->Write("png:../../../htdocs/projects/speaking/tae2006/music/imagecache/$filename\.png"); my $conn=Mysql->connect( "", "", "", "" ); my $expression = "insert into ImageCache (URL,ContentType,LocalName) values (" . $conn->quote($url) . ", " . $conn->quote('image/png') . ", " . $conn->quote("/projects/speaking/tae2006/music/imagecache/$filename\.png") . ")"; $conn->query( $expression ); sub get_qstring { my ($i, $op, $key, $val, $arglist ); my $qin= $ENV{'QUERY_STRING'}; my @qin = split(/[&;]/,$qin); my %qin; foreach $i (0 .. $#qin) { $qin[$i] =~ s/\+/ /g; ($key, $op, $val) = split(/((?:\%3C|\%3E|=){1,2})/,$qin[$i],3); # splits on the first =. $op =~ s/%(..)/pack("c",hex($1))/ge; $key =~ s/%(..)/pack("c",hex($1))/ge; $val =~ s/%(..)/pack("c",hex($1))/ge; if ( !( exists $qin{$key} ) ) { $qin{$key} = {}; } $qin{$key} = $val; } return \%qin; }