Annotation of loncom/homework/randomlylabel.pm, revision 1.10

1.1       albertel    1: #!/usr/bin/perl
                      2: # The LearningOnline Network with CAPA
                      3: # randomlabel.png: composite together text and images into 1 image
                      4: #
1.10    ! albertel    5: # $Id: randomlylabel.pm,v 1.9 2003/05/13 19:08:31 albertel Exp $
1.1       albertel    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
                     28: #
                     29: ###
                     30: 
                     31: package Apache::randomlylabel;
                     32: 
                     33: use strict;
                     34: use Image::Magick;
                     35: use Apache::Constants qw(:common);
                     36: use Apache::loncommon();
1.3       albertel   37: use GD;
                     38: 
                     39: sub get_image {
                     40:     my ($imgsrc,$set_trans)=@_;
                     41:     my $image;
1.5       www        42:     if ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3       albertel   43: 	my $conv_image = Image::Magick->new;
                     44: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
                     45: 	$conv_image->Set('magick'=>'png');
                     46: 	my @blobs=$conv_image->ImageToBlob();
                     47: 	undef $conv_image;
                     48: 	$image = GD::Image->new($blobs[0]);
                     49:     } else {
1.6       albertel   50: 	GD::Image->trueColor(1);
1.3       albertel   51: 	$image = GD::Image->new($imgsrc);
                     52:     }
1.9       albertel   53:     if ($set_trans && defined($image)) {
1.3       albertel   54: 	my $white=$image->colorExact(255,255,255);
                     55: 	if ($white != -1) { $image->transparent($white); }
                     56:     }
                     57:     return $image;
                     58: }
1.1       albertel   59: 
                     60: sub handler {
                     61:     my $r = shift;
                     62:     $r->content_type('image/png');
                     63:     my (undef,$token) = split(/=/,$ENV{'QUERY_STRING'});
                     64:     &Apache::loncommon::get_unprocessed_cgi(
                     65:                &Apache::lonnet::unescape($ENV{'imagerequest.'.$token}));
1.3       albertel   66:     my $image=&get_image($ENV{"form.BGIMG"},0);
1.4       matthew    67:     if (! defined($image)) {
1.6       albertel   68:         &Apache::lonnet::logthis('Unable to create image object for '.
                     69: 				 $ENV{"form.BGIMG"});
1.4       matthew    70:         return OK;
                     71:     }
1.1       albertel   72:     #binmode(STDOUT);
1.8       albertel   73:     my $black;
                     74:     if (!($black=$image->colorResolve(0,0,0))) {
                     75: 	$black = $image->colorClosestHWB(0,0,0);
                     76:     }
1.1       albertel   77:     for(my $i=0;$i<$ENV{"form.ICOUNT"};$i++) {
1.3       albertel   78: 	my $subimage=&get_image($ENV{"form.IMG$i"},1);
1.9       albertel   79: 	if (!defined($subimage)) {
                     80:             &Apache::lonnet::logthis('Unable to create image object for '.
                     81:                                  $ENV{"form.BGIMG"});
                     82:             next;
                     83:         }
                     84: 	$image->copy($subimage,$ENV{"form.IX$i"},$ENV{"form.IY$i"},
1.3       albertel   85: 		     0,0,$subimage->getBounds());
1.1       albertel   86:     }
1.3       albertel   87:     my $height=GD::Font->Giant->height;
1.1       albertel   88:     for(my $i=0;$i<$ENV{"form.COUNT"};$i++) {
1.3       albertel   89: 	$image->string(gdGiantFont,$ENV{"form.X$i"},$ENV{"form.Y$i"}-$height,
                     90: 		       $ENV{"form.LB$i"},$black);
1.1       albertel   91:     }
1.10    ! albertel   92:     for(my $i=0;$i<$ENV{"form.LINECOUNT"};$i++) {
        !            93: 	my $x1=$ENV{"form.LINEX1$i"};
        !            94: 	my $y1=$ENV{"form.LINEY1$i"};
        !            95: 	my $x2=$ENV{"form.LINEX2$i"};
        !            96: 	my $y2=$ENV{"form.LINEY2$i"};
        !            97: 	my $width=$ENV{"form.LINEW$i"};
        !            98: 	my $color=$ENV{"form.LINEC$i"};
        !            99: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
        !           100: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
        !           101: 	my $imcolor;
        !           102: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
        !           103: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
        !           104: 	}
        !           105: 	$image->setThickness($width);
        !           106:        	$image->line($x1,$y1,$x2,$y2,$imcolor);
        !           107:     }
        !           108:     $image->setThickness(1);
1.3       albertel  109:     $r->print($image->png);
1.1       albertel  110:     return OK;
                    111: }
                    112: 
                    113: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>