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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # randomlabel.png: composite together text and images into 1 image
                      3: #
1.32    ! raeburn     4: # $Id: randomlylabel.pm,v 1.31 2008/12/19 14:28:14 raeburn Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.22      albertel   28: 
                     29: =pod
                     30: 
                     31: =head1 Syntax of randomlylabel commands
                     32: 
1.25      www        33: Required items are: (one of BGIMG or SIZE) and OBJCOUNT
1.24      albertel   34: 
1.22      albertel   35: =over 4
                     36: 
                     37: =item BGIMG
                     38: 
1.25      www        39: /home/... file
                     40: /res/ ... URL
                     41: or href (href must contain http://...)
1.22      albertel   42: Expected to be HTTP escaped
                     43: 
1.24      albertel   44: =item SIZE
                     45: 
                     46: width:height
                     47: 
                     48: Creates a blank canvas of size width,height.
                     49: 
                     50: =item BGCOLOR
                     51: 
                     52: either I<transparent> or a color hexstring
                     53: 
                     54: Sets the background color, if SIZE is used to create a new canvas,
                     55: I<trasparent> makes the background transparent.
                     56: 
1.22      albertel   57: =item OBJCOUNT
                     58: 
                     59: a number
                     60: 
                     61: =item OBJTYPE
                     62: 
                     63: a colon seperated list of types, supported types are
                     64: 
                     65:          B<LINE> B<RECTANGLE> B<POLYGON> B<ARC> B<FILL> B<IMAGE> B<LABEL>
                     66: 
                     67: =item OBJI<num>
                     68: 
                     69: arguments for this OBJ
                     70: 
                     71: some common arguments are
                     72: 
                     73: =over 4
                     74: 
                     75: =item x y thickness
                     76: 
                     77: are pixel values
                     78: 
                     79: =item color
                     80: 
                     81: a hexstring, without with out a leading # or x)
                     82: 
                     83: =item filled
                     84: 
                     85: boolean, (1 or 0)
                     86: 
                     87: =back
                     88: 
                     89: The argumants for the possible object types are
                     90: 
                     91: =over 4
                     92: 
                     93: =item LINE
                     94: 
                     95: x1:y1:x2:y2:color:thickness
                     96: 
                     97: =item RECTANGLE 
                     98: 
                     99: x1:y1:x2:y2:color:thickness:filled
                    100: 
                    101: =item ARC
                    102: 
                    103: x:y:width:height:start:end:color:thickness:filled
                    104: 
                    105: =over 4
                    106: 
                    107: =item start, end
                    108: 
                    109: start and ends of the arc (in degrees)
                    110: 
                    111: =back
                    112: 
                    113: =item FILL
                    114: 
                    115: x:y:color
                    116: 
                    117: =item IMAGE
                    118: 
                    119: x:y:file:transparent:srcX:srcY:destW:destH:srcW:srcH
                    120: 
                    121: =over 4
                    122: 
                    123: =item srcX,srcY,srcW,srcH 
                    124: 
                    125: the start and extant of the region in file to copy to x,y with width/height
                    126:            destW destH
                    127: 
                    128: =back
                    129: 
                    130: =item LABEL
                    131: 
                    132: x:y:text:font:color:direction
                    133: 
                    134: =over 4
                    135: 
                    136: =item text
                    137: 
                    138: HTTP escaped string of the text to place on the image
                    139: 
                    140: =item font
                    141: 
                    142: one of B<tiny>, B<small>, B<medium>, B<large>, B<giant>, or an
                    143: installed TTF font and point size
                    144: 
                    145: =item direction
                    146: 
                    147: either B<horizontal> or B<vertical>
                    148: 
                    149: =back
                    150: 
                    151: =item  POLYGON
                    152: 
                    153: color:width:open:filled
                    154: 
                    155: =over 4 
                    156: 
                    157: =item open
                    158: 
                    159: boolean, (1 or 0)
                    160: 
                    161: =back
                    162: 
                    163: =back
                    164: 
                    165: 
                    166: =item OBJEXTRAI<num>
                    167: 
                    168: extra arguments for object I<num>
                    169: 
                    170: The possible values for this for the different object types are
                    171: 
                    172: =over 4
                    173: 
                    174: =item POLYGON 
                    175: 
                    176: a list of coords in the form
                    177: 
                    178:      (x,y)-(x,y)-(x,y)
                    179: 
                    180: (there can be arbitrarily many of these)
                    181: 
                    182: =back
                    183: 
                    184: =back
                    185: 
                    186: =head1 Example
                    187: 
                    188:  BGIMG=file
                    189:  OBJTYPE=LINE:LINE:LINE:LINE
                    190:  OBJCOUNT=4
                    191:  OBJ0=xmin:ymin:xmax:ymax:FFFFFF:3
                    192:  OBJ1=xmin:ymax:xmax:ymin:FFFFFF:3
                    193:  OBJ2=xmin:ymin:xmax:ymax:FF0000:1
                    194:  OBJ3=xmin:ymax:xmax:ymin:FF0000:1
                    195: 
                    196: =cut
1.1       albertel  197: 
                    198: package Apache::randomlylabel;
                    199: 
                    200: use strict;
                    201: use Image::Magick;
                    202: use Apache::Constants qw(:common);
                    203: use Apache::loncommon();
1.17      albertel  204: use GD;
1.13      albertel  205: use GD::Polyline();
1.26      albertel  206: use Apache::lonnet;
1.28      www       207: use lib '/home/httpd/lib/perl/';
                    208: use LONCAPA;
1.32    ! raeburn   209: use LONCAPA::LWPReq; 
1.3       albertel  210: 
                    211: sub get_image {
                    212:     my ($imgsrc,$set_trans)=@_;
                    213:     my $image;
1.15      albertel  214:     if ($imgsrc !~ m|^(/home/)|) {
1.31      raeburn   215: 	if ($imgsrc !~ /^https?\:/) {
                    216: 	    $imgsrc=&Apache::lonnet::absolute_url($ENV{'HTTP_HOST'}).$imgsrc;
1.15      albertel  217: 	}
                    218: 	my $request=new HTTP::Request('GET',"$imgsrc");
                    219: 	$request->header(Cookie => $ENV{'HTTP_COOKIE'});
                    220: 	my $file="/tmp/imagetmp".$$;
1.32    ! raeburn   221:         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
        !           222:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,$file,'','','',1);
1.15      albertel  223: 	if ($response->is_success) {
                    224: 	    if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
                    225: 		my $conv_image = Image::Magick->new;
                    226: 		my $current_figure = $conv_image->Read('filename'=>$file);
1.20      albertel  227: 		$conv_image->Set('type'=>'TrueColor');
1.15      albertel  228: 		$conv_image->Set('magick'=>'png');
                    229: 		my @blobs=$conv_image->ImageToBlob();
                    230: 		undef $conv_image;
                    231: 		$image = GD::Image->new($blobs[0]);
                    232: 	    } else {
                    233: 		GD::Image->trueColor(1);
                    234: 		$image = GD::Image->new($file);
                    235: 	    }
                    236: 	}
                    237:     } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3       albertel  238: 	my $conv_image = Image::Magick->new;
                    239: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
1.20      albertel  240: 	$conv_image->Set('type'=>'TrueColor');
1.3       albertel  241: 	$conv_image->Set('magick'=>'png');
                    242: 	my @blobs=$conv_image->ImageToBlob();
                    243: 	undef $conv_image;
                    244: 	$image = GD::Image->new($blobs[0]);
                    245:     } else {
1.17      albertel  246: 	$image = GD::Image->trueColor(1);
1.3       albertel  247: 	$image = GD::Image->new($imgsrc);
                    248:     }
1.9       albertel  249:     if ($set_trans && defined($image)) {
1.3       albertel  250: 	my $white=$image->colorExact(255,255,255);
                    251: 	if ($white != -1) { $image->transparent($white); }
                    252:     }
                    253:     return $image;
                    254: }
1.1       albertel  255: 
1.16      albertel  256: sub get_color_from_hexstring {
                    257:     my ($image,$color)=@_;
                    258:     if (!$color) { $color='000000'; }
1.23      albertel  259:     $color=~s/^[x\#]//;
1.16      albertel  260:     my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                    261:     $red=hex($red);$green=hex($green);$blue=hex($blue);
                    262:     my $imcolor;
                    263:     if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                    264: 	$imcolor = $image->colorClosestHWB($red,$green,$blue);
                    265:     }
                    266:     return $imcolor;
                    267: }
                    268: 
1.30      albertel  269: sub add_click {
                    270:     my ($image) = @_;
                    271: 
                    272:     my $length=6;
                    273:     my $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    274:     my $fgcolor=&get_color_from_hexstring($image,'009999');
                    275: 
                    276:     my ($x,$y) = split(':',$env{'form.clickdata'});
                    277: 
                    278:     $image->setThickness(3);
                    279:     $image->line($x-$length,$y,        $x+$length,$y,        $bgcolor);
                    280:     $image->line($x,        $y-$length,$x,        $y+$length,$bgcolor);
                    281:     $image->setThickness(1);
                    282:     $image->line($x-$length,$y,        $x+$length,$y,        $fgcolor);
                    283:     $image->line($x,        $y-$length,$x,        $y+$length,$fgcolor);
                    284: }
                    285: 
1.1       albertel  286: sub handler {
                    287:     my $r = shift;
                    288:     $r->content_type('image/png');
1.15      albertel  289:     $r->send_http_header;
1.29      albertel  290: 
                    291:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    292: 
                    293:     my $prefix;
1.24      albertel  294:     if ($ENV{'QUERY_STRING'}=~/OBJCOUNT\=/) {
                    295: 	$prefix='form.';
1.29      albertel  296:     } else {
                    297: 	$prefix="cgi.$env{'form.token'}.";
1.24      albertel  298:     }
1.29      albertel  299: 
                    300:     my $image;
1.26      albertel  301:     if (defined($env{$prefix."BGIMG"})) {
1.28      www       302: 	my $bgimg=&unescape($env{$prefix."BGIMG"});
1.18      albertel  303: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
1.17      albertel  304: 	$image=&get_image($bgimg,0);
                    305: 	if (! defined($image)) {
                    306: 	    &Apache::lonnet::logthis('Unable to create image object for -'.
1.29      albertel  307: 				     $env{'form.token'}.'-'.$bgimg);
1.17      albertel  308: 	    return OK;
                    309: 	}
1.26      albertel  310:     } elsif (defined($env{$prefix."SIZE"})) {
                    311: 	my ($width,$height)=split(':',$env{$prefix."SIZE"});
1.16      albertel  312: 	$image = new GD::Image($width,$height,1);
1.26      albertel  313: 	my ($bgcolor)=split(':',$env{$prefix."BGCOLOR"});
1.16      albertel  314: 	if ($bgcolor ne 'transparent') {
                    315: 	    $bgcolor=&get_color_from_hexstring($image,$bgcolor);
                    316: #	$image->rectangle(0,0,$width,$height,$bgcolor);
                    317: 	    $image->fill(0,0,$bgcolor);
                    318: 	} else {
                    319: 	    $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    320: 	    $image->fill(0,0,$bgcolor);
                    321: 	    $image->transparent($bgcolor);
                    322: 	}
                    323:     } else {
1.29      albertel  324: 	&Apache::lonnet::logthis('Unable to create image object, no info '.$prefix);
1.16      albertel  325: 	return OK;
1.4       matthew   326:     }
1.1       albertel  327:     #binmode(STDOUT);
1.26      albertel  328:     my @objtypes=split(':',$env{$prefix."OBJTYPE"});
                    329:     foreach(my $i=0;$i<$env{$prefix."OBJCOUNT"};$i++) {
1.16      albertel  330: 	my $type=shift(@objtypes);
                    331: 	if ($type eq 'LINE') {
                    332: 	    my ($x1,$y1,$x2,$y2,$color,$thickness)=
1.26      albertel  333: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  334: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    335: 	    if (!defined($thickness)) { $thickness=1; }
                    336: 	    $image->setThickness($thickness);
1.21      albertel  337: #	    $image->setAntiAliased($imcolor);
                    338: 	    $image->line($x1,$y1,$x2,$y2,$imcolor);
1.16      albertel  339: 	} elsif ($type eq 'RECTANGLE') {
                    340: 	    my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
1.26      albertel  341: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  342: 	    if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    343: 	    if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    344: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    345: 	    if (!defined($thickness)) { $thickness=1; }
                    346: 	    $image->setThickness($thickness);
                    347: #	    $image->setAntiAliased($imcolor);
                    348: 	    if ($filled) {
                    349: 		$image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
                    350: 	    } else {
                    351: 		$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
                    352: 	    }
                    353: 	} elsif ($type eq 'POLYGON') {
1.26      albertel  354: 	    my ($color,$width,$open,$filled)=split(':',$env{$prefix."OBJ$i"});
1.16      albertel  355: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.21      albertel  356: 	    my $polygon = (($open && lc ($open ne 'no')) ?
                    357: 			   (new GD::Polyline) : (new GD::Polygon));
1.18      albertel  358: 	    my $added=0;
1.26      albertel  359: 	    foreach my $coord (split('-',$env{$prefix."OBJEXTRA$i"})) {
1.16      albertel  360: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    361: 		$polygon->addPt($x,$y);
1.18      albertel  362: 		$added++;
1.16      albertel  363: 	    }
1.18      albertel  364: 	    
1.16      albertel  365: 	    $image->setThickness($width);
1.18      albertel  366: 	    if ($added) {
1.21      albertel  367: 		if ($open && lc($open) ne 'no') {
1.18      albertel  368: 		    $image->polydraw($polygon,$imcolor);
1.21      albertel  369: 		} elsif ($filled && lc($filled) ne 'no') {
                    370: 		    $image->filledPolygon($polygon,$imcolor);
1.18      albertel  371: 		} else {
                    372: 		    $image->polygon($polygon,$imcolor);
                    373: 		}
1.16      albertel  374: 	    }
                    375: 	} elsif ($type eq 'ARC') {
                    376: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
1.26      albertel  377: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  378: 	    if (!$color) { $color='000000'; }
                    379: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    380: 	    if (!defined($thickness)) { $thickness=1; }
                    381: 	    $image->setThickness($thickness);
                    382: #	    $image->setAntiAliased($imcolor);
                    383: 	    if ($filled) {
                    384: 		$image->filledArc($x,$y,$width,$height,$start,$end,
                    385: 				  $imcolor);
                    386: 	    } else {
                    387: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
                    388: 	    }
                    389: 	} elsif ($type eq 'FILL') {
1.26      albertel  390: 	    my ($x,$y,$color)=split(':',$env{$prefix."OBJ$i"});
1.16      albertel  391: 	    if (!$color) { $color='000000'; }
                    392: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    393: 	    $image->fill($x,$y,$imcolor);
                    394: 	} elsif ($type eq 'IMAGE') {
1.21      albertel  395: 	    my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
1.26      albertel  396: 		$srcH)=split(':',$env{$prefix."OBJ$i"});
1.28      www       397: 	    $file=&unescape($file);
1.16      albertel  398: 	    if (!defined($transparent)) { $transparent=1; }
                    399: 	    my $subimage=&get_image($file,$transparent);
                    400: 	    if (!defined($subimage)) {
                    401: 		&Apache::lonnet::logthis('Unable to create image object for '.
                    402: 					 $file);
                    403: 		next;
                    404: 	    }
1.21      albertel  405: 	    if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
                    406: 	    if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
                    407: 	    if (!defined($destW) or !$destW) { $destW=$srcW; }
                    408: 	    if (!defined($destH) or !$destH) { $destH=$srcH; }
                    409: 	    $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
                    410: 				$srcW,$srcH);
1.16      albertel  411: 	} elsif ($type eq 'LABEL') {
1.19      albertel  412: 	    my ($x,$y,$text,$font,$color,$direction)=
1.26      albertel  413: 		split(':',$env{$prefix."OBJ$i"});
1.28      www       414: 	    $text=&unescape($text);
1.16      albertel  415: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.19      albertel  416: 	    my $type='normal';
                    417: 	    my ($height,$fontref);
                    418: 	    if ($font eq 'tiny') {
                    419: 		$height=GD::Font->Tiny->height;
                    420: 		$fontref=GD::gdTinyFont;
                    421: 	    } elsif ($font eq 'small') {
                    422: 		$height=GD::Font->Small->height;
                    423: 		$fontref=GD::gdSmallFont;
                    424: 	    } elsif ($font eq 'medium') {
                    425: 		$height=GD::Font->MediumBold->height;
                    426: 		$fontref=GD::gdMediumBoldFont;
                    427: 	    } elsif ($font eq 'large') {
                    428: 		$height=GD::Font->Large->height;
                    429: 		$fontref=GD::gdLargeFont;
1.21      albertel  430: 	    } elsif ($font eq 'giant' || !$font) {
1.19      albertel  431: 		$height=GD::Font->Giant->height;
                    432: 		$fontref=GD::gdGiantFont;
                    433: 	    } else {
                    434: 		$type='ttf';
                    435: 	    }
                    436: 	    if ($type eq 'normal' && $direction eq 'vertical') {
                    437: 		$image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
                    438: 	    } elsif ($type eq 'normal') {
                    439: 		$image->string($fontref,$x,$y-$height,$text,$imcolor);
                    440: 	    } elsif ($type eq 'ttf') {
                    441: 		my ($fontname,$ptsize)=split(/\s+/,$font);
                    442: 		$image->stringFT($imcolor,$fontname,$ptsize,90,$x,$y,$text);
                    443: 	    }
1.18      albertel  444: 	} else {
                    445: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
1.13      albertel  446: 	}
1.10      albertel  447:     }
1.30      albertel  448:     if (exists($env{'form.clickdata'})) { &add_click($image); }
1.10      albertel  449:     $image->setThickness(1);
1.3       albertel  450:     $r->print($image->png);
1.1       albertel  451:     return OK;
                    452: }
                    453: 
                    454: 1;

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