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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # randomlabel.png: composite together text and images into 1 image
                      3: #
1.37    ! raeburn     4: # $Id: randomlylabel.pm,v 1.36 2024/04/05 04:05:08 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: 
1.37    ! raeburn    97: =item RECTANGLE
1.22      albertel   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: 
1.37    ! raeburn   123: =item srcX,srcY,srcW,srcH
1.22      albertel  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: 
1.34      raeburn   132: x:y:text:font:color:direction:rotation
1.22      albertel  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: 
1.35      raeburn   149: =item rotation
                    150: 
                    151: number of degrees to rotate the text, relative to the horizontal.
                    152: only used if font attribute is set to a freetype font (e.g., helvetica 12),
                    153: and in that case, if set to a valid value, overrides value set for direction.
                    154: 
1.22      albertel  155: =back
                    156: 
                    157: =item  POLYGON
                    158: 
                    159: color:width:open:filled
                    160: 
1.37    ! raeburn   161: =over 4
1.22      albertel  162: 
                    163: =item open
                    164: 
                    165: boolean, (1 or 0)
                    166: 
                    167: =back
                    168: 
                    169: =back
                    170: 
                    171: 
                    172: =item OBJEXTRAI<num>
                    173: 
                    174: extra arguments for object I<num>
                    175: 
                    176: The possible values for this for the different object types are
                    177: 
                    178: =over 4
                    179: 
1.37    ! raeburn   180: =item POLYGON
1.22      albertel  181: 
                    182: a list of coords in the form
                    183: 
                    184:      (x,y)-(x,y)-(x,y)
                    185: 
                    186: (there can be arbitrarily many of these)
                    187: 
                    188: =back
                    189: 
                    190: =back
                    191: 
                    192: =head1 Example
                    193: 
                    194:  BGIMG=file
                    195:  OBJTYPE=LINE:LINE:LINE:LINE
                    196:  OBJCOUNT=4
                    197:  OBJ0=xmin:ymin:xmax:ymax:FFFFFF:3
                    198:  OBJ1=xmin:ymax:xmax:ymin:FFFFFF:3
                    199:  OBJ2=xmin:ymin:xmax:ymax:FF0000:1
                    200:  OBJ3=xmin:ymax:xmax:ymin:FF0000:1
                    201: 
                    202: =cut
1.1       albertel  203: 
                    204: package Apache::randomlylabel;
                    205: 
                    206: use strict;
                    207: use Image::Magick;
                    208: use Apache::Constants qw(:common);
                    209: use Apache::loncommon();
1.33      raeburn   210: use Math::Trig();
1.17      albertel  211: use GD;
1.13      albertel  212: use GD::Polyline();
1.26      albertel  213: use Apache::lonnet;
1.28      www       214: use lib '/home/httpd/lib/perl/';
                    215: use LONCAPA;
1.37    ! raeburn   216: use LONCAPA::LWPReq;
1.3       albertel  217: 
1.33      raeburn   218: #
                    219: # Note: Math::Trig is included in the standard perl package for many distros.
                    220: #
                    221: # For distros which use rpm the following command will show whether Trig.pm is
                    222: # included in the system perl: rpm -q --provides perl |grep Math::Trig
                    223: #
                    224: # For distros which use deb the following command will show whether Trig.pm is
                    225: # included in the system perl: dpkg -S perl |grep Math\/Trig\.pm
                    226: #
                    227: 
1.3       albertel  228: sub get_image {
                    229:     my ($imgsrc,$set_trans)=@_;
                    230:     my $image;
1.15      albertel  231:     if ($imgsrc !~ m|^(/home/)|) {
1.31      raeburn   232: 	if ($imgsrc !~ /^https?\:/) {
                    233: 	    $imgsrc=&Apache::lonnet::absolute_url($ENV{'HTTP_HOST'}).$imgsrc;
1.15      albertel  234: 	}
                    235: 	my $request=new HTTP::Request('GET',"$imgsrc");
                    236: 	$request->header(Cookie => $ENV{'HTTP_COOKIE'});
                    237: 	my $file="/tmp/imagetmp".$$;
1.32      raeburn   238:         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
                    239:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,$file,'','','',1);
1.15      albertel  240: 	if ($response->is_success) {
                    241: 	    if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
                    242: 		my $conv_image = Image::Magick->new;
                    243: 		my $current_figure = $conv_image->Read('filename'=>$file);
1.20      albertel  244: 		$conv_image->Set('type'=>'TrueColor');
1.15      albertel  245: 		$conv_image->Set('magick'=>'png');
                    246: 		my @blobs=$conv_image->ImageToBlob();
                    247: 		undef $conv_image;
                    248: 		$image = GD::Image->new($blobs[0]);
                    249: 	    } else {
                    250: 		GD::Image->trueColor(1);
                    251: 		$image = GD::Image->new($file);
                    252: 	    }
                    253: 	}
                    254:     } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3       albertel  255: 	my $conv_image = Image::Magick->new;
                    256: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
1.20      albertel  257: 	$conv_image->Set('type'=>'TrueColor');
1.3       albertel  258: 	$conv_image->Set('magick'=>'png');
                    259: 	my @blobs=$conv_image->ImageToBlob();
                    260: 	undef $conv_image;
                    261: 	$image = GD::Image->new($blobs[0]);
                    262:     } else {
1.17      albertel  263: 	$image = GD::Image->trueColor(1);
1.3       albertel  264: 	$image = GD::Image->new($imgsrc);
                    265:     }
1.9       albertel  266:     if ($set_trans && defined($image)) {
1.3       albertel  267: 	my $white=$image->colorExact(255,255,255);
                    268: 	if ($white != -1) { $image->transparent($white); }
                    269:     }
                    270:     return $image;
                    271: }
1.1       albertel  272: 
1.16      albertel  273: sub get_color_from_hexstring {
                    274:     my ($image,$color)=@_;
                    275:     if (!$color) { $color='000000'; }
1.23      albertel  276:     $color=~s/^[x\#]//;
1.16      albertel  277:     my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                    278:     $red=hex($red);$green=hex($green);$blue=hex($blue);
                    279:     my $imcolor;
                    280:     if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                    281: 	$imcolor = $image->colorClosestHWB($red,$green,$blue);
                    282:     }
                    283:     return $imcolor;
                    284: }
                    285: 
1.30      albertel  286: sub add_click {
                    287:     my ($image) = @_;
                    288: 
                    289:     my $length=6;
                    290:     my $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    291:     my $fgcolor=&get_color_from_hexstring($image,'009999');
                    292: 
                    293:     my ($x,$y) = split(':',$env{'form.clickdata'});
                    294: 
                    295:     $image->setThickness(3);
                    296:     $image->line($x-$length,$y,        $x+$length,$y,        $bgcolor);
                    297:     $image->line($x,        $y-$length,$x,        $y+$length,$bgcolor);
                    298:     $image->setThickness(1);
                    299:     $image->line($x-$length,$y,        $x+$length,$y,        $fgcolor);
                    300:     $image->line($x,        $y-$length,$x,        $y+$length,$fgcolor);
                    301: }
                    302: 
1.1       albertel  303: sub handler {
                    304:     my $r = shift;
1.29      albertel  305: 
                    306:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    307: 
                    308:     my $prefix;
1.24      albertel  309:     if ($ENV{'QUERY_STRING'}=~/OBJCOUNT\=/) {
                    310: 	$prefix='form.';
1.29      albertel  311:     } else {
                    312: 	$prefix="cgi.$env{'form.token'}.";
1.24      albertel  313:     }
1.36      raeburn   314:     my $epsfile;
                    315:     if (defined($env{$prefix."EPSFILE"})) {
                    316:         my $user = $env{'user.name'}.'_'.$env{'user.domain'};
                    317:         if ($env{$prefix."EPSFILE"} =~ /^\Q$user\E_\d+_\d+_\d+_drawimage\.eps$/) {
                    318:             $epsfile = $Apache::lonnet::perlvar{'lonPrtDir'}.'/'.$env{$prefix."EPSFILE"};
                    319:         } else {
                    320:             &Apache::lonnet::logthis('Unable to create eps file for image object for -'.
                    321:                                      $env{'form.token'}.'- for '.$user.' as EPSFILE has '.
                    322:                                      'unexpected value');
                    323:             return OK;
                    324:         }
                    325:     }
                    326:     unless ($epsfile) {
                    327:         $r->content_type('image/png');
                    328:         $r->send_http_header;
                    329:     }
1.29      albertel  330: 
                    331:     my $image;
1.26      albertel  332:     if (defined($env{$prefix."BGIMG"})) {
1.28      www       333: 	my $bgimg=&unescape($env{$prefix."BGIMG"});
1.18      albertel  334: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
1.17      albertel  335: 	$image=&get_image($bgimg,0);
                    336: 	if (! defined($image)) {
                    337: 	    &Apache::lonnet::logthis('Unable to create image object for -'.
1.29      albertel  338: 				     $env{'form.token'}.'-'.$bgimg);
1.17      albertel  339: 	    return OK;
                    340: 	}
1.26      albertel  341:     } elsif (defined($env{$prefix."SIZE"})) {
                    342: 	my ($width,$height)=split(':',$env{$prefix."SIZE"});
1.16      albertel  343: 	$image = new GD::Image($width,$height,1);
1.26      albertel  344: 	my ($bgcolor)=split(':',$env{$prefix."BGCOLOR"});
1.16      albertel  345: 	if ($bgcolor ne 'transparent') {
                    346: 	    $bgcolor=&get_color_from_hexstring($image,$bgcolor);
                    347: #	$image->rectangle(0,0,$width,$height,$bgcolor);
                    348: 	    $image->fill(0,0,$bgcolor);
                    349: 	} else {
                    350: 	    $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    351: 	    $image->fill(0,0,$bgcolor);
                    352: 	    $image->transparent($bgcolor);
                    353: 	}
                    354:     } else {
1.29      albertel  355: 	&Apache::lonnet::logthis('Unable to create image object, no info '.$prefix);
1.16      albertel  356: 	return OK;
1.4       matthew   357:     }
1.1       albertel  358:     #binmode(STDOUT);
1.26      albertel  359:     my @objtypes=split(':',$env{$prefix."OBJTYPE"});
                    360:     foreach(my $i=0;$i<$env{$prefix."OBJCOUNT"};$i++) {
1.16      albertel  361: 	my $type=shift(@objtypes);
                    362: 	if ($type eq 'LINE') {
                    363: 	    my ($x1,$y1,$x2,$y2,$color,$thickness)=
1.26      albertel  364: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  365: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    366: 	    if (!defined($thickness)) { $thickness=1; }
                    367: 	    $image->setThickness($thickness);
1.21      albertel  368: #	    $image->setAntiAliased($imcolor);
                    369: 	    $image->line($x1,$y1,$x2,$y2,$imcolor);
1.16      albertel  370: 	} elsif ($type eq 'RECTANGLE') {
                    371: 	    my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
1.26      albertel  372: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  373: 	    if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    374: 	    if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    375: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    376: 	    if (!defined($thickness)) { $thickness=1; }
                    377: 	    $image->setThickness($thickness);
                    378: #	    $image->setAntiAliased($imcolor);
                    379: 	    if ($filled) {
                    380: 		$image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
                    381: 	    } else {
                    382: 		$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
                    383: 	    }
                    384: 	} elsif ($type eq 'POLYGON') {
1.26      albertel  385: 	    my ($color,$width,$open,$filled)=split(':',$env{$prefix."OBJ$i"});
1.16      albertel  386: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.21      albertel  387: 	    my $polygon = (($open && lc ($open ne 'no')) ?
                    388: 			   (new GD::Polyline) : (new GD::Polygon));
1.18      albertel  389: 	    my $added=0;
1.26      albertel  390: 	    foreach my $coord (split('-',$env{$prefix."OBJEXTRA$i"})) {
1.16      albertel  391: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    392: 		$polygon->addPt($x,$y);
1.18      albertel  393: 		$added++;
1.16      albertel  394: 	    }
                    395: 	    $image->setThickness($width);
1.18      albertel  396: 	    if ($added) {
1.21      albertel  397: 		if ($open && lc($open) ne 'no') {
1.18      albertel  398: 		    $image->polydraw($polygon,$imcolor);
1.21      albertel  399: 		} elsif ($filled && lc($filled) ne 'no') {
                    400: 		    $image->filledPolygon($polygon,$imcolor);
1.18      albertel  401: 		} else {
                    402: 		    $image->polygon($polygon,$imcolor);
                    403: 		}
1.16      albertel  404: 	    }
                    405: 	} elsif ($type eq 'ARC') {
                    406: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
1.26      albertel  407: 		split(':',$env{$prefix."OBJ$i"});
1.16      albertel  408: 	    if (!$color) { $color='000000'; }
                    409: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    410: 	    if (!defined($thickness)) { $thickness=1; }
                    411: 	    $image->setThickness($thickness);
                    412: #	    $image->setAntiAliased($imcolor);
                    413: 	    if ($filled) {
                    414: 		$image->filledArc($x,$y,$width,$height,$start,$end,
                    415: 				  $imcolor);
                    416: 	    } else {
                    417: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
                    418: 	    }
                    419: 	} elsif ($type eq 'FILL') {
1.26      albertel  420: 	    my ($x,$y,$color)=split(':',$env{$prefix."OBJ$i"});
1.16      albertel  421: 	    if (!$color) { $color='000000'; }
                    422: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    423: 	    $image->fill($x,$y,$imcolor);
                    424: 	} elsif ($type eq 'IMAGE') {
1.21      albertel  425: 	    my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
1.26      albertel  426: 		$srcH)=split(':',$env{$prefix."OBJ$i"});
1.28      www       427: 	    $file=&unescape($file);
1.16      albertel  428: 	    if (!defined($transparent)) { $transparent=1; }
                    429: 	    my $subimage=&get_image($file,$transparent);
                    430: 	    if (!defined($subimage)) {
                    431: 		&Apache::lonnet::logthis('Unable to create image object for '.
                    432: 					 $file);
                    433: 		next;
                    434: 	    }
1.21      albertel  435: 	    if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
                    436: 	    if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
                    437: 	    if (!defined($destW) or !$destW) { $destW=$srcW; }
                    438: 	    if (!defined($destH) or !$destH) { $destH=$srcH; }
                    439: 	    $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
                    440: 				$srcW,$srcH);
1.16      albertel  441: 	} elsif ($type eq 'LABEL') {
1.34      raeburn   442: 	    my ($x,$y,$text,$font,$color,$direction,$rotation)=
1.26      albertel  443: 		split(':',$env{$prefix."OBJ$i"});
1.28      www       444: 	    $text=&unescape($text);
1.16      albertel  445: 	    my $imcolor=&get_color_from_hexstring($image,$color);
1.19      albertel  446: 	    my $type='normal';
                    447: 	    my ($height,$fontref);
                    448: 	    if ($font eq 'tiny') {
                    449: 		$height=GD::Font->Tiny->height;
                    450: 		$fontref=GD::gdTinyFont;
                    451: 	    } elsif ($font eq 'small') {
                    452: 		$height=GD::Font->Small->height;
                    453: 		$fontref=GD::gdSmallFont;
                    454: 	    } elsif ($font eq 'medium') {
                    455: 		$height=GD::Font->MediumBold->height;
                    456: 		$fontref=GD::gdMediumBoldFont;
                    457: 	    } elsif ($font eq 'large') {
                    458: 		$height=GD::Font->Large->height;
                    459: 		$fontref=GD::gdLargeFont;
1.21      albertel  460: 	    } elsif ($font eq 'giant' || !$font) {
1.19      albertel  461: 		$height=GD::Font->Giant->height;
                    462: 		$fontref=GD::gdGiantFont;
1.33      raeburn   463: 	    } elsif ($image->useFontConfig(1)) {
1.19      albertel  464: 		$type='ttf';
                    465: 	    }
                    466: 	    if ($type eq 'normal' && $direction eq 'vertical') {
                    467: 		$image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
                    468: 	    } elsif ($type eq 'normal') {
                    469: 		$image->string($fontref,$x,$y-$height,$text,$imcolor);
                    470: 	    } elsif ($type eq 'ttf') {
                    471: 		my ($fontname,$ptsize)=split(/\s+/,$font);
1.33      raeburn   472:                 my $angle = 0;
1.34      raeburn   473:                 if ($rotation =~ /^(\-|\+|)\d+(|\.\d*)$/) {
                    474:                     $angle = Math::Trig::deg2rad($rotation);
                    475:                 } elsif ($direction eq 'vertical') {
1.33      raeburn   476:                     $angle = Math::Trig::deg2rad(90);
                    477:                 } elsif ($direction eq 'horizontal') {
                    478:                     $angle = 0;
                    479:                 }
                    480: 		$image->stringFT($imcolor,$fontname,$ptsize,$angle,$x,$y,$text);
1.19      albertel  481: 	    }
1.18      albertel  482: 	} else {
                    483: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
1.13      albertel  484: 	}
1.10      albertel  485:     }
1.30      albertel  486:     if (exists($env{'form.clickdata'})) { &add_click($image); }
1.10      albertel  487:     $image->setThickness(1);
1.36      raeburn   488:     if ($epsfile) {
                    489:         if (open(my $pipe, "| convert png:- $epsfile")) {
                    490:             print $pipe $image->png;
                    491:             close($pipe);
                    492:         } else {
                    493:             &Apache::lonnet::logthis("randomlylabel unable to open pipe to convert png to eps");
                    494:         }
                    495:     } else {
                    496:         $r->print($image->png);
                    497:     }
1.1       albertel  498:     return OK;
                    499: }
                    500: 
                    501: 1;

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