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

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.23    ! albertel    5: # $Id: randomlylabel.pm,v 1.22 2005/02/23 15:48:54 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'; }
1.23    ! albertel  240:     $color=~s/^[x\#]//;
1.16      albertel  241:     my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                    242:     $red=hex($red);$green=hex($green);$blue=hex($blue);
                    243:     my $imcolor;
                    244:     if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                    245: 	$imcolor = $image->colorClosestHWB($red,$green,$blue);
                    246:     }
                    247:     return $imcolor;
                    248: }
                    249: 
1.1       albertel  250: sub handler {
                    251:     my $r = shift;
                    252:     $r->content_type('image/png');
1.15      albertel  253:     $r->send_http_header;
1.11      albertel  254:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.17      albertel  255:     my $image;
                    256:     if (defined($ENV{"cgi.$id.BGIMG"})) {
                    257: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
1.18      albertel  258: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
1.17      albertel  259: 	$image=&get_image($bgimg,0);
                    260: 	if (! defined($image)) {
                    261: 	    &Apache::lonnet::logthis('Unable to create image object for -'.
                    262: 				     $id.'-'.$bgimg);
                    263: 	    return OK;
                    264: 	}
1.16      albertel  265:     } elsif (defined($ENV{"cgi.$id.SIZE"})) {
                    266: 	my ($width,$height)=split(':',$ENV{"cgi.$id.SIZE"});
                    267: 	$image = new GD::Image($width,$height,1);
                    268: 	my ($bgcolor)=split(':',$ENV{"cgi.$id.BGCOLOR"});
                    269: 	if ($bgcolor ne 'transparent') {
                    270: 	    $bgcolor=&get_color_from_hexstring($image,$bgcolor);
                    271: #	$image->rectangle(0,0,$width,$height,$bgcolor);
                    272: 	    $image->fill(0,0,$bgcolor);
                    273: 	} else {
                    274: 	    $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    275: 	    $image->fill(0,0,$bgcolor);
                    276: 	    $image->transparent($bgcolor);
                    277: 	}
                    278:     } else {
                    279: 	&Apache::lonnet::logthis('Unable to create image object, no info');
                    280: 	return OK;
1.4       matthew   281:     }
1.1       albertel  282:     #binmode(STDOUT);
1.16      albertel  283:     my @objtypes=split(':',$ENV{"cgi.$id.OBJTYPE"});
                    284:     foreach(my $i=0;$i<$ENV{"cgi.$id.OBJCOUNT"};$i++) {
                    285: 	my $type=shift(@objtypes);
                    286: 	if ($type eq 'LINE') {
                    287: 	    my ($x1,$y1,$x2,$y2,$color,$thickness)=
                    288: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    289: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    290: 	    if (!defined($thickness)) { $thickness=1; }
                    291: 	    $image->setThickness($thickness);
1.21      albertel  292: #	    $image->setAntiAliased($imcolor);
                    293: 	    $image->line($x1,$y1,$x2,$y2,$imcolor);
1.16      albertel  294: 	} elsif ($type eq 'RECTANGLE') {
                    295: 	    my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
                    296: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    297: 	    if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    298: 	    if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    299: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    300: 	    if (!defined($thickness)) { $thickness=1; }
                    301: 	    $image->setThickness($thickness);
                    302: #	    $image->setAntiAliased($imcolor);
                    303: 	    if ($filled) {
                    304: 		$image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
                    305: 	    } else {
                    306: 		$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
                    307: 	    }
                    308: 	} elsif ($type eq 'POLYGON') {
1.21      albertel  309: 	    my ($color,$width,$open,$filled)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  310: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.21      albertel  311: 	    my $polygon = (($open && lc ($open ne 'no')) ?
                    312: 			   (new GD::Polyline) : (new GD::Polygon));
1.18      albertel  313: 	    my $added=0;
1.16      albertel  314: 	    foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
                    315: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    316: 		$polygon->addPt($x,$y);
1.18      albertel  317: 		$added++;
1.16      albertel  318: 	    }
1.18      albertel  319: 	    
1.16      albertel  320: 	    $image->setThickness($width);
1.18      albertel  321: 	    if ($added) {
1.21      albertel  322: 		if ($open && lc($open) ne 'no') {
1.18      albertel  323: 		    $image->polydraw($polygon,$imcolor);
1.21      albertel  324: 		} elsif ($filled && lc($filled) ne 'no') {
                    325: 		    $image->filledPolygon($polygon,$imcolor);
1.18      albertel  326: 		} else {
                    327: 		    $image->polygon($polygon,$imcolor);
                    328: 		}
1.16      albertel  329: 	    }
                    330: 	} elsif ($type eq 'ARC') {
                    331: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
                    332: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    333: 	    if (!$color) { $color='000000'; }
                    334: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    335: 	    if (!defined($thickness)) { $thickness=1; }
                    336: 	    $image->setThickness($thickness);
                    337: #	    $image->setAntiAliased($imcolor);
                    338: 	    if ($filled) {
                    339: 		$image->filledArc($x,$y,$width,$height,$start,$end,
                    340: 				  $imcolor);
                    341: 	    } else {
                    342: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
                    343: 	    }
                    344: 	} elsif ($type eq 'FILL') {
                    345: 	    my ($x,$y,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    346: 	    if (!$color) { $color='000000'; }
                    347: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    348: 	    $image->fill($x,$y,$imcolor);
                    349: 	} elsif ($type eq 'IMAGE') {
1.21      albertel  350: 	    my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
                    351: 		$srcH)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  352: 	    $file=&Apache::lonnet::unescape($file);
                    353: 	    if (!defined($transparent)) { $transparent=1; }
                    354: 	    my $subimage=&get_image($file,$transparent);
                    355: 	    if (!defined($subimage)) {
                    356: 		&Apache::lonnet::logthis('Unable to create image object for '.
                    357: 					 $file);
                    358: 		next;
                    359: 	    }
1.21      albertel  360: 	    if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
                    361: 	    if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
                    362: 	    if (!defined($destW) or !$destW) { $destW=$srcW; }
                    363: 	    if (!defined($destH) or !$destH) { $destH=$srcH; }
                    364: 	    $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
                    365: 				$srcW,$srcH);
1.16      albertel  366: 	} elsif ($type eq 'LABEL') {
1.19      albertel  367: 	    my ($x,$y,$text,$font,$color,$direction)=
                    368: 		split(':',$ENV{"cgi.$id.OBJ$i"});
1.16      albertel  369: 	    $text=&Apache::lonnet::unescape($text);
                    370: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.19      albertel  371: 	    my $type='normal';
                    372: 	    my ($height,$fontref);
                    373: 	    if ($font eq 'tiny') {
                    374: 		$height=GD::Font->Tiny->height;
                    375: 		$fontref=GD::gdTinyFont;
                    376: 	    } elsif ($font eq 'small') {
                    377: 		$height=GD::Font->Small->height;
                    378: 		$fontref=GD::gdSmallFont;
                    379: 	    } elsif ($font eq 'medium') {
                    380: 		$height=GD::Font->MediumBold->height;
                    381: 		$fontref=GD::gdMediumBoldFont;
                    382: 	    } elsif ($font eq 'large') {
                    383: 		$height=GD::Font->Large->height;
                    384: 		$fontref=GD::gdLargeFont;
1.21      albertel  385: 	    } elsif ($font eq 'giant' || !$font) {
1.19      albertel  386: 		$height=GD::Font->Giant->height;
                    387: 		$fontref=GD::gdGiantFont;
                    388: 	    } else {
                    389: 		$type='ttf';
                    390: 	    }
                    391: 	    if ($type eq 'normal' && $direction eq 'vertical') {
                    392: 		$image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
                    393: 	    } elsif ($type eq 'normal') {
                    394: 		$image->string($fontref,$x,$y-$height,$text,$imcolor);
                    395: 	    } elsif ($type eq 'ttf') {
                    396: 		my ($fontname,$ptsize)=split(/\s+/,$font);
                    397: 		$image->stringFT($imcolor,$fontname,$ptsize,90,$x,$y,$text);
                    398: 	    }
1.18      albertel  399: 	} else {
                    400: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
1.13      albertel  401: 	}
1.10      albertel  402:     }
                    403:     $image->setThickness(1);
1.3       albertel  404:     $r->print($image->png);
1.1       albertel  405:     return OK;
                    406: }
                    407: 
                    408: 1;

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