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

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.12    ! albertel    5: # $Id: randomlylabel.pm,v 1.11 2003/11/11 00:25:24 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');
1.11      albertel   63:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
                     64:     my $image=&get_image(&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"}),0);
1.4       matthew    65:     if (! defined($image)) {
1.11      albertel   66:         &Apache::lonnet::logthis('Unable to create image object for -'.$id.'-'.
                     67: 				 $ENV{"cgi.$id.BGIMG"});
1.4       matthew    68:         return OK;
                     69:     }
1.1       albertel   70:     #binmode(STDOUT);
1.8       albertel   71:     my $black;
                     72:     if (!($black=$image->colorResolve(0,0,0))) {
                     73: 	$black = $image->colorClosestHWB(0,0,0);
                     74:     }
1.11      albertel   75:     for(my $i=0;$i<$ENV{"cgi.$id.ICOUNT"};$i++) {
                     76: 	my $subimage=&get_image(&Apache::lonnet::unescape($ENV{"cgi.$id.IMG$i"}),1);
1.9       albertel   77: 	if (!defined($subimage)) {
                     78:             &Apache::lonnet::logthis('Unable to create image object for '.
1.11      albertel   79:                                  $ENV{"cgi.$id.BGIMG"});
1.9       albertel   80:             next;
                     81:         }
1.11      albertel   82: 	$image->copy($subimage,$ENV{"cgi.$id.IX$i"},$ENV{"cgi.$id.IY$i"},
1.3       albertel   83: 		     0,0,$subimage->getBounds());
1.1       albertel   84:     }
1.3       albertel   85:     my $height=GD::Font->Giant->height;
1.11      albertel   86:     for(my $i=0;$i<$ENV{"cgi.$id.COUNT"};$i++) {
                     87: 	$image->string(gdGiantFont,$ENV{"cgi.$id.X$i"},
                     88: 		       $ENV{"cgi.$id.Y$i"}-$height,
                     89: 		       &Apache::lonnet::unescape($ENV{"cgi.$id.LB$i"}),$black);
                     90:     }
                     91:     for(my $i=0;$i<$ENV{"cgi.$id.LINECOUNT"};$i++) {
                     92: 	my ($x1,$y1,$x2,$y2,$color,$width)=split(':',$ENV{"cgi.$id.LINE$i"});
1.10      albertel   93: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                     94: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
                     95: 	my $imcolor;
                     96: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                     97: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
                     98: 	}
                     99: 	$image->setThickness($width);
                    100:        	$image->line($x1,$y1,$x2,$y2,$imcolor);
1.12    ! albertel  101:     }
        !           102:     for(my $i=0;$i<$ENV{"cgi.$id.BOXCOUNT"};$i++) {
        !           103: 	my ($x1,$y1,$x2,$y2,$color,$width)=split(':',$ENV{"cgi.$id.BOX$i"});
        !           104: 	if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
        !           105: 	if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
        !           106: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
        !           107: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
        !           108: 	my $imcolor;
        !           109: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
        !           110: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
        !           111: 	}
        !           112: 	$image->setThickness($width);
        !           113:        	$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
1.10      albertel  114:     }
                    115:     $image->setThickness(1);
1.3       albertel  116:     $r->print($image->png);
1.1       albertel  117:     return OK;
                    118: }
                    119: 
                    120: 1;

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