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