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

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.22    ! albertel    5: # $Id: randomlylabel.pm,v 1.21 2004/09/09 07:24:56 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: #
1.22    ! albertel   29: 
        !            30: =pod
        !            31: 
        !            32: =head1 Syntax of randomlylabel commands
        !            33: 
        !            34: =over 4
        !            35: 
        !            36: =item BGIMG
        !            37: 
        !            38: /home/... file or href (href must contain http://...)
        !            39: Expected to be HTTP escaped
        !            40: 
        !            41: =item OBJCOUNT
        !            42: 
        !            43: a number
        !            44: 
        !            45: =item OBJTYPE
        !            46: 
        !            47: a colon seperated list of types, supported types are
        !            48: 
        !            49:          B<LINE> B<RECTANGLE> B<POLYGON> B<ARC> B<FILL> B<IMAGE> B<LABEL>
        !            50: 
        !            51: =item OBJI<num>
        !            52: 
        !            53: arguments for this OBJ
        !            54: 
        !            55: some common arguments are
        !            56: 
        !            57: =over 4
        !            58: 
        !            59: =item x y thickness
        !            60: 
        !            61: are pixel values
        !            62: 
        !            63: =item color
        !            64: 
        !            65: a hexstring, without with out a leading # or x)
        !            66: 
        !            67: =item filled
        !            68: 
        !            69: boolean, (1 or 0)
        !            70: 
        !            71: =back
        !            72: 
        !            73: The argumants for the possible object types are
        !            74: 
        !            75: =over 4
        !            76: 
        !            77: =item LINE
        !            78: 
        !            79: x1:y1:x2:y2:color:thickness
        !            80: 
        !            81: =item RECTANGLE 
        !            82: 
        !            83: x1:y1:x2:y2:color:thickness:filled
        !            84: 
        !            85: =item ARC
        !            86: 
        !            87: x:y:width:height:start:end:color:thickness:filled
        !            88: 
        !            89: =over 4
        !            90: 
        !            91: =item start, end
        !            92: 
        !            93: start and ends of the arc (in degrees)
        !            94: 
        !            95: =back
        !            96: 
        !            97: =item FILL
        !            98: 
        !            99: x:y:color
        !           100: 
        !           101: =item IMAGE
        !           102: 
        !           103: x:y:file:transparent:srcX:srcY:destW:destH:srcW:srcH
        !           104: 
        !           105: =over 4
        !           106: 
        !           107: =item srcX,srcY,srcW,srcH 
        !           108: 
        !           109: the start and extant of the region in file to copy to x,y with width/height
        !           110:            destW destH
        !           111: 
        !           112: =back
        !           113: 
        !           114: =item LABEL
        !           115: 
        !           116: x:y:text:font:color:direction
        !           117: 
        !           118: =over 4
        !           119: 
        !           120: =item text
        !           121: 
        !           122: HTTP escaped string of the text to place on the image
        !           123: 
        !           124: =item font
        !           125: 
        !           126: one of B<tiny>, B<small>, B<medium>, B<large>, B<giant>, or an
        !           127: installed TTF font and point size
        !           128: 
        !           129: =item direction
        !           130: 
        !           131: either B<horizontal> or B<vertical>
        !           132: 
        !           133: =back
        !           134: 
        !           135: =item  POLYGON
        !           136: 
        !           137: color:width:open:filled
        !           138: 
        !           139: =over 4 
        !           140: 
        !           141: =item open
        !           142: 
        !           143: boolean, (1 or 0)
        !           144: 
        !           145: =back
        !           146: 
        !           147: =back
        !           148: 
        !           149: 
        !           150: =item OBJEXTRAI<num>
        !           151: 
        !           152: extra arguments for object I<num>
        !           153: 
        !           154: The possible values for this for the different object types are
        !           155: 
        !           156: =over 4
        !           157: 
        !           158: =item POLYGON 
        !           159: 
        !           160: a list of coords in the form
        !           161: 
        !           162:      (x,y)-(x,y)-(x,y)
        !           163: 
        !           164: (there can be arbitrarily many of these)
        !           165: 
        !           166: =back
        !           167: 
        !           168: =back
        !           169: 
        !           170: =head1 Example
        !           171: 
        !           172:  BGIMG=file
        !           173:  OBJTYPE=LINE:LINE:LINE:LINE
        !           174:  OBJCOUNT=4
        !           175:  OBJ0=xmin:ymin:xmax:ymax:FFFFFF:3
        !           176:  OBJ1=xmin:ymax:xmax:ymin:FFFFFF:3
        !           177:  OBJ2=xmin:ymin:xmax:ymax:FF0000:1
        !           178:  OBJ3=xmin:ymax:xmax:ymin:FF0000:1
        !           179: 
        !           180: =cut
1.1       albertel  181: 
                    182: package Apache::randomlylabel;
                    183: 
                    184: use strict;
                    185: use Image::Magick;
                    186: use Apache::Constants qw(:common);
                    187: use Apache::loncommon();
1.17      albertel  188: use GD;
1.13      albertel  189: use GD::Polyline();
1.15      albertel  190: use LWP::UserAgent();
1.3       albertel  191: 
                    192: sub get_image {
                    193:     my ($imgsrc,$set_trans)=@_;
                    194:     my $image;
1.15      albertel  195:     if ($imgsrc !~ m|^(/home/)|) {
                    196: 	if ($imgsrc !~ /^http:/) {
                    197: 	    $imgsrc="http://".$ENV{'HTTP_HOST'}.$imgsrc;
                    198: 	}
                    199: 	my $ua=new LWP::UserAgent;
                    200: 	my $request=new HTTP::Request('GET',"$imgsrc");
                    201: 	$request->header(Cookie => $ENV{'HTTP_COOKIE'});
                    202: 	my $file="/tmp/imagetmp".$$;
                    203: 	my $response=$ua->request($request,$file);
                    204: 	if ($response->is_success) {
                    205: 	    if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
                    206: 		my $conv_image = Image::Magick->new;
                    207: 		my $current_figure = $conv_image->Read('filename'=>$file);
1.20      albertel  208: 		$conv_image->Set('type'=>'TrueColor');
1.15      albertel  209: 		$conv_image->Set('magick'=>'png');
                    210: 		my @blobs=$conv_image->ImageToBlob();
                    211: 		undef $conv_image;
                    212: 		$image = GD::Image->new($blobs[0]);
                    213: 	    } else {
                    214: 		GD::Image->trueColor(1);
                    215: 		$image = GD::Image->new($file);
                    216: 	    }
                    217: 	}
                    218:     } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3       albertel  219: 	my $conv_image = Image::Magick->new;
                    220: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
1.20      albertel  221: 	$conv_image->Set('type'=>'TrueColor');
1.3       albertel  222: 	$conv_image->Set('magick'=>'png');
                    223: 	my @blobs=$conv_image->ImageToBlob();
                    224: 	undef $conv_image;
                    225: 	$image = GD::Image->new($blobs[0]);
                    226:     } else {
1.17      albertel  227: 	$image = GD::Image->trueColor(1);
1.3       albertel  228: 	$image = GD::Image->new($imgsrc);
                    229:     }
1.9       albertel  230:     if ($set_trans && defined($image)) {
1.3       albertel  231: 	my $white=$image->colorExact(255,255,255);
                    232: 	if ($white != -1) { $image->transparent($white); }
                    233:     }
                    234:     return $image;
                    235: }
1.1       albertel  236: 
1.16      albertel  237: sub get_color_from_hexstring {
                    238:     my ($image,$color)=@_;
                    239:     if (!$color) { $color='000000'; }
                    240:     my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                    241:     $red=hex($red);$green=hex($green);$blue=hex($blue);
                    242:     my $imcolor;
                    243:     if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                    244: 	$imcolor = $image->colorClosestHWB($red,$green,$blue);
                    245:     }
                    246:     return $imcolor;
                    247: }
                    248: 
1.1       albertel  249: sub handler {
                    250:     my $r = shift;
                    251:     $r->content_type('image/png');
1.15      albertel  252:     $r->send_http_header;
1.11      albertel  253:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.17      albertel  254:     my $image;
                    255:     if (defined($ENV{"cgi.$id.BGIMG"})) {
                    256: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
1.18      albertel  257: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
1.17      albertel  258: 	$image=&get_image($bgimg,0);
                    259: 	if (! defined($image)) {
                    260: 	    &Apache::lonnet::logthis('Unable to create image object for -'.
                    261: 				     $id.'-'.$bgimg);
                    262: 	    return OK;
                    263: 	}
1.16      albertel  264:     } elsif (defined($ENV{"cgi.$id.SIZE"})) {
                    265: 	my ($width,$height)=split(':',$ENV{"cgi.$id.SIZE"});
                    266: 	$image = new GD::Image($width,$height,1);
                    267: 	my ($bgcolor)=split(':',$ENV{"cgi.$id.BGCOLOR"});
                    268: 	if ($bgcolor ne 'transparent') {
                    269: 	    $bgcolor=&get_color_from_hexstring($image,$bgcolor);
                    270: #	$image->rectangle(0,0,$width,$height,$bgcolor);
                    271: 	    $image->fill(0,0,$bgcolor);
                    272: 	} else {
                    273: 	    $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    274: 	    $image->fill(0,0,$bgcolor);
                    275: 	    $image->transparent($bgcolor);
                    276: 	}
                    277:     } else {
                    278: 	&Apache::lonnet::logthis('Unable to create image object, no info');
                    279: 	return OK;
1.4       matthew   280:     }
1.1       albertel  281:     #binmode(STDOUT);
1.16      albertel  282:     my @objtypes=split(':',$ENV{"cgi.$id.OBJTYPE"});
                    283:     foreach(my $i=0;$i<$ENV{"cgi.$id.OBJCOUNT"};$i++) {
                    284: 	my $type=shift(@objtypes);
                    285: 	if ($type eq 'LINE') {
                    286: 	    my ($x1,$y1,$x2,$y2,$color,$thickness)=
                    287: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    288: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    289: 	    if (!defined($thickness)) { $thickness=1; }
                    290: 	    $image->setThickness($thickness);
1.21      albertel  291: #	    $image->setAntiAliased($imcolor);
                    292: 	    $image->line($x1,$y1,$x2,$y2,$imcolor);
1.16      albertel  293: 	} elsif ($type eq 'RECTANGLE') {
                    294: 	    my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
                    295: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    296: 	    if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    297: 	    if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    298: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    299: 	    if (!defined($thickness)) { $thickness=1; }
                    300: 	    $image->setThickness($thickness);
                    301: #	    $image->setAntiAliased($imcolor);
                    302: 	    if ($filled) {
                    303: 		$image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
                    304: 	    } else {
                    305: 		$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
                    306: 	    }
                    307: 	} elsif ($type eq 'POLYGON') {
1.21      albertel  308: 	    my ($color,$width,$open,$filled)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  309: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.21      albertel  310: 	    my $polygon = (($open && lc ($open ne 'no')) ?
                    311: 			   (new GD::Polyline) : (new GD::Polygon));
1.18      albertel  312: 	    my $added=0;
1.16      albertel  313: 	    foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
                    314: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    315: 		$polygon->addPt($x,$y);
1.18      albertel  316: 		$added++;
1.16      albertel  317: 	    }
1.18      albertel  318: 	    
1.16      albertel  319: 	    $image->setThickness($width);
1.18      albertel  320: 	    if ($added) {
1.21      albertel  321: 		if ($open && lc($open) ne 'no') {
1.18      albertel  322: 		    $image->polydraw($polygon,$imcolor);
1.21      albertel  323: 		} elsif ($filled && lc($filled) ne 'no') {
                    324: 		    $image->filledPolygon($polygon,$imcolor);
1.18      albertel  325: 		} else {
                    326: 		    $image->polygon($polygon,$imcolor);
                    327: 		}
1.16      albertel  328: 	    }
                    329: 	} elsif ($type eq 'ARC') {
                    330: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
                    331: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    332: 	    if (!$color) { $color='000000'; }
                    333: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    334: 	    if (!defined($thickness)) { $thickness=1; }
                    335: 	    $image->setThickness($thickness);
                    336: #	    $image->setAntiAliased($imcolor);
                    337: 	    if ($filled) {
                    338: 		$image->filledArc($x,$y,$width,$height,$start,$end,
                    339: 				  $imcolor);
                    340: 	    } else {
                    341: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
                    342: 	    }
                    343: 	} elsif ($type eq 'FILL') {
                    344: 	    my ($x,$y,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    345: 	    if (!$color) { $color='000000'; }
                    346: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    347: 	    $image->fill($x,$y,$imcolor);
                    348: 	} elsif ($type eq 'IMAGE') {
1.21      albertel  349: 	    my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
                    350: 		$srcH)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  351: 	    $file=&Apache::lonnet::unescape($file);
                    352: 	    if (!defined($transparent)) { $transparent=1; }
                    353: 	    my $subimage=&get_image($file,$transparent);
                    354: 	    if (!defined($subimage)) {
                    355: 		&Apache::lonnet::logthis('Unable to create image object for '.
                    356: 					 $file);
                    357: 		next;
                    358: 	    }
1.21      albertel  359: 	    if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
                    360: 	    if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
                    361: 	    if (!defined($destW) or !$destW) { $destW=$srcW; }
                    362: 	    if (!defined($destH) or !$destH) { $destH=$srcH; }
                    363: 	    $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
                    364: 				$srcW,$srcH);
1.16      albertel  365: 	} elsif ($type eq 'LABEL') {
1.19      albertel  366: 	    my ($x,$y,$text,$font,$color,$direction)=
                    367: 		split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  368: 	    $text=&Apache::lonnet::unescape($text);
                    369: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.19      albertel  370: 	    my $type='normal';
                    371: 	    my ($height,$fontref);
                    372: 	    if ($font eq 'tiny') {
                    373: 		$height=GD::Font->Tiny->height;
                    374: 		$fontref=GD::gdTinyFont;
                    375: 	    } elsif ($font eq 'small') {
                    376: 		$height=GD::Font->Small->height;
                    377: 		$fontref=GD::gdSmallFont;
                    378: 	    } elsif ($font eq 'medium') {
                    379: 		$height=GD::Font->MediumBold->height;
                    380: 		$fontref=GD::gdMediumBoldFont;
                    381: 	    } elsif ($font eq 'large') {
                    382: 		$height=GD::Font->Large->height;
                    383: 		$fontref=GD::gdLargeFont;
1.21      albertel  384: 	    } elsif ($font eq 'giant' || !$font) {
1.19      albertel  385: 		$height=GD::Font->Giant->height;
                    386: 		$fontref=GD::gdGiantFont;
                    387: 	    } else {
                    388: 		$type='ttf';
                    389: 	    }
                    390: 	    if ($type eq 'normal' && $direction eq 'vertical') {
                    391: 		$image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
                    392: 	    } elsif ($type eq 'normal') {
                    393: 		$image->string($fontref,$x,$y-$height,$text,$imcolor);
                    394: 	    } elsif ($type eq 'ttf') {
                    395: 		my ($fontname,$ptsize)=split(/\s+/,$font);
                    396: 		$image->stringFT($imcolor,$fontname,$ptsize,90,$x,$y,$text);
                    397: 	    }
1.18      albertel  398: 	} else {
                    399: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
1.13      albertel  400: 	}
1.10      albertel  401:     }
                    402:     $image->setThickness(1);
1.3       albertel  403:     $r->print($image->png);
1.1       albertel  404:     return OK;
                    405: }
                    406: 
                    407: 1;

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