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