File:  [LON-CAPA] / loncom / homework / randomlylabel.pm
Revision 1.22: download - view: text, annotated - select for diffs
Wed Feb 23 15:48:54 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding docs for randomlylabels, syntax

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: # randomlabel.png: composite together text and images into 1 image
    4: #
    5: # $Id: randomlylabel.pm,v 1.22 2005/02/23 15:48:54 albertel Exp $
    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: =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
  181: 
  182: package Apache::randomlylabel;
  183: 
  184: use strict;
  185: use Image::Magick;
  186: use Apache::Constants qw(:common);
  187: use Apache::loncommon();
  188: use GD;
  189: use GD::Polyline();
  190: use LWP::UserAgent();
  191: 
  192: sub get_image {
  193:     my ($imgsrc,$set_trans)=@_;
  194:     my $image;
  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);
  208: 		$conv_image->Set('type'=>'TrueColor');
  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) {
  219: 	my $conv_image = Image::Magick->new;
  220: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
  221: 	$conv_image->Set('type'=>'TrueColor');
  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 {
  227: 	$image = GD::Image->trueColor(1);
  228: 	$image = GD::Image->new($imgsrc);
  229:     }
  230:     if ($set_trans && defined($image)) {
  231: 	my $white=$image->colorExact(255,255,255);
  232: 	if ($white != -1) { $image->transparent($white); }
  233:     }
  234:     return $image;
  235: }
  236: 
  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: 
  249: sub handler {
  250:     my $r = shift;
  251:     $r->content_type('image/png');
  252:     $r->send_http_header;
  253:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
  254:     my $image;
  255:     if (defined($ENV{"cgi.$id.BGIMG"})) {
  256: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
  257: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
  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: 	}
  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;
  280:     }
  281:     #binmode(STDOUT);
  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);
  291: #	    $image->setAntiAliased($imcolor);
  292: 	    $image->line($x1,$y1,$x2,$y2,$imcolor);
  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') {
  308: 	    my ($color,$width,$open,$filled)=split(':',$ENV{"cgi.$id.OBJ$i"});
  309: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  310: 	    my $polygon = (($open && lc ($open ne 'no')) ?
  311: 			   (new GD::Polyline) : (new GD::Polygon));
  312: 	    my $added=0;
  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);
  316: 		$added++;
  317: 	    }
  318: 	    
  319: 	    $image->setThickness($width);
  320: 	    if ($added) {
  321: 		if ($open && lc($open) ne 'no') {
  322: 		    $image->polydraw($polygon,$imcolor);
  323: 		} elsif ($filled && lc($filled) ne 'no') {
  324: 		    $image->filledPolygon($polygon,$imcolor);
  325: 		} else {
  326: 		    $image->polygon($polygon,$imcolor);
  327: 		}
  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') {
  349: 	    my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
  350: 		$srcH)=split(':',$ENV{"cgi.$id.OBJ$i"});
  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: 	    }
  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);
  365: 	} elsif ($type eq 'LABEL') {
  366: 	    my ($x,$y,$text,$font,$color,$direction)=
  367: 		split(':',$ENV{"cgi.$id.OBJ$i"});
  368: 	    $text=&Apache::lonnet::unescape($text);
  369: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  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;
  384: 	    } elsif ($font eq 'giant' || !$font) {
  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: 	    }
  398: 	} else {
  399: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
  400: 	}
  401:     }
  402:     $image->setThickness(1);
  403:     $r->print($image->png);
  404:     return OK;
  405: }
  406: 
  407: 1;

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