File:
[LON-CAPA] /
loncom /
homework /
randomlylabel.pm
Revision
1.34:
download - view:
text,
annotated -
select for diffs
Fri Apr 5 02:31:23 2024 UTC (4 months, 1 week ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Add rotation attribute to text tag for drawimage. Rotation value is the
number of degrees to rotate the text, relative to the horizontal.
- Only used if font attribute is set to freetype font (e.g., helvetica 12).
- If set to a valid value will override the value for direction attribute.
1: # The LearningOnline Network with CAPA
2: # randomlabel.png: composite together text and images into 1 image
3: #
4: # $Id: randomlylabel.pm,v 1.34 2024/04/05 02:31:23 raeburn Exp $
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: #
28:
29: =pod
30:
31: =head1 Syntax of randomlylabel commands
32:
33: Required items are: (one of BGIMG or SIZE) and OBJCOUNT
34:
35: =over 4
36:
37: =item BGIMG
38:
39: /home/... file
40: /res/ ... URL
41: or href (href must contain http://...)
42: Expected to be HTTP escaped
43:
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:
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:rotation
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
197:
198: package Apache::randomlylabel;
199:
200: use strict;
201: use Image::Magick;
202: use Apache::Constants qw(:common);
203: use Apache::loncommon();
204: use Math::Trig();
205: use GD;
206: use GD::Polyline();
207: use Apache::lonnet;
208: use lib '/home/httpd/lib/perl/';
209: use LONCAPA;
210: use LONCAPA::LWPReq;
211:
212: #
213: # Note: Math::Trig is included in the standard perl package for many distros.
214: #
215: # For distros which use rpm the following command will show whether Trig.pm is
216: # included in the system perl: rpm -q --provides perl |grep Math::Trig
217: #
218: # For distros which use deb the following command will show whether Trig.pm is
219: # included in the system perl: dpkg -S perl |grep Math\/Trig\.pm
220: #
221:
222: sub get_image {
223: my ($imgsrc,$set_trans)=@_;
224: my $image;
225: if ($imgsrc !~ m|^(/home/)|) {
226: if ($imgsrc !~ /^https?\:/) {
227: $imgsrc=&Apache::lonnet::absolute_url($ENV{'HTTP_HOST'}).$imgsrc;
228: }
229: my $request=new HTTP::Request('GET',"$imgsrc");
230: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
231: my $file="/tmp/imagetmp".$$;
232: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
233: my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,$file,'','','',1);
234: if ($response->is_success) {
235: if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
236: my $conv_image = Image::Magick->new;
237: my $current_figure = $conv_image->Read('filename'=>$file);
238: $conv_image->Set('type'=>'TrueColor');
239: $conv_image->Set('magick'=>'png');
240: my @blobs=$conv_image->ImageToBlob();
241: undef $conv_image;
242: $image = GD::Image->new($blobs[0]);
243: } else {
244: GD::Image->trueColor(1);
245: $image = GD::Image->new($file);
246: }
247: }
248: } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
249: my $conv_image = Image::Magick->new;
250: my $current_figure = $conv_image->Read('filename'=>$imgsrc);
251: $conv_image->Set('type'=>'TrueColor');
252: $conv_image->Set('magick'=>'png');
253: my @blobs=$conv_image->ImageToBlob();
254: undef $conv_image;
255: $image = GD::Image->new($blobs[0]);
256: } else {
257: $image = GD::Image->trueColor(1);
258: $image = GD::Image->new($imgsrc);
259: }
260: if ($set_trans && defined($image)) {
261: my $white=$image->colorExact(255,255,255);
262: if ($white != -1) { $image->transparent($white); }
263: }
264: return $image;
265: }
266:
267: sub get_color_from_hexstring {
268: my ($image,$color)=@_;
269: if (!$color) { $color='000000'; }
270: $color=~s/^[x\#]//;
271: my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
272: $red=hex($red);$green=hex($green);$blue=hex($blue);
273: my $imcolor;
274: if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
275: $imcolor = $image->colorClosestHWB($red,$green,$blue);
276: }
277: return $imcolor;
278: }
279:
280: sub add_click {
281: my ($image) = @_;
282:
283: my $length=6;
284: my $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
285: my $fgcolor=&get_color_from_hexstring($image,'009999');
286:
287: my ($x,$y) = split(':',$env{'form.clickdata'});
288:
289: $image->setThickness(3);
290: $image->line($x-$length,$y, $x+$length,$y, $bgcolor);
291: $image->line($x, $y-$length,$x, $y+$length,$bgcolor);
292: $image->setThickness(1);
293: $image->line($x-$length,$y, $x+$length,$y, $fgcolor);
294: $image->line($x, $y-$length,$x, $y+$length,$fgcolor);
295: }
296:
297: sub handler {
298: my $r = shift;
299: $r->content_type('image/png');
300: $r->send_http_header;
301:
302: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
303:
304: my $prefix;
305: if ($ENV{'QUERY_STRING'}=~/OBJCOUNT\=/) {
306: $prefix='form.';
307: } else {
308: $prefix="cgi.$env{'form.token'}.";
309: }
310:
311: my $image;
312: if (defined($env{$prefix."BGIMG"})) {
313: my $bgimg=&unescape($env{$prefix."BGIMG"});
314: #&Apache::lonnet::logthis("BGIMG is ".$bgimg);
315: $image=&get_image($bgimg,0);
316: if (! defined($image)) {
317: &Apache::lonnet::logthis('Unable to create image object for -'.
318: $env{'form.token'}.'-'.$bgimg);
319: return OK;
320: }
321: } elsif (defined($env{$prefix."SIZE"})) {
322: my ($width,$height)=split(':',$env{$prefix."SIZE"});
323: $image = new GD::Image($width,$height,1);
324: my ($bgcolor)=split(':',$env{$prefix."BGCOLOR"});
325: if ($bgcolor ne 'transparent') {
326: $bgcolor=&get_color_from_hexstring($image,$bgcolor);
327: # $image->rectangle(0,0,$width,$height,$bgcolor);
328: $image->fill(0,0,$bgcolor);
329: } else {
330: $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
331: $image->fill(0,0,$bgcolor);
332: $image->transparent($bgcolor);
333: }
334: } else {
335: &Apache::lonnet::logthis('Unable to create image object, no info '.$prefix);
336: return OK;
337: }
338: #binmode(STDOUT);
339: my @objtypes=split(':',$env{$prefix."OBJTYPE"});
340: foreach(my $i=0;$i<$env{$prefix."OBJCOUNT"};$i++) {
341: my $type=shift(@objtypes);
342: if ($type eq 'LINE') {
343: my ($x1,$y1,$x2,$y2,$color,$thickness)=
344: split(':',$env{$prefix."OBJ$i"});
345: my $imcolor=&get_color_from_hexstring($image,$color);
346: if (!defined($thickness)) { $thickness=1; }
347: $image->setThickness($thickness);
348: # $image->setAntiAliased($imcolor);
349: $image->line($x1,$y1,$x2,$y2,$imcolor);
350: } elsif ($type eq 'RECTANGLE') {
351: my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
352: split(':',$env{$prefix."OBJ$i"});
353: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
354: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
355: my $imcolor=&get_color_from_hexstring($image,$color);
356: if (!defined($thickness)) { $thickness=1; }
357: $image->setThickness($thickness);
358: # $image->setAntiAliased($imcolor);
359: if ($filled) {
360: $image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
361: } else {
362: $image->rectangle($x1,$y1,$x2,$y2,$imcolor);
363: }
364: } elsif ($type eq 'POLYGON') {
365: my ($color,$width,$open,$filled)=split(':',$env{$prefix."OBJ$i"});
366: my $imcolor=&get_color_from_hexstring($image,$color);
367: my $polygon = (($open && lc ($open ne 'no')) ?
368: (new GD::Polyline) : (new GD::Polygon));
369: my $added=0;
370: foreach my $coord (split('-',$env{$prefix."OBJEXTRA$i"})) {
371: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
372: $polygon->addPt($x,$y);
373: $added++;
374: }
375:
376: $image->setThickness($width);
377: if ($added) {
378: if ($open && lc($open) ne 'no') {
379: $image->polydraw($polygon,$imcolor);
380: } elsif ($filled && lc($filled) ne 'no') {
381: $image->filledPolygon($polygon,$imcolor);
382: } else {
383: $image->polygon($polygon,$imcolor);
384: }
385: }
386: } elsif ($type eq 'ARC') {
387: my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
388: split(':',$env{$prefix."OBJ$i"});
389: if (!$color) { $color='000000'; }
390: my $imcolor=&get_color_from_hexstring($image,$color);
391: if (!defined($thickness)) { $thickness=1; }
392: $image->setThickness($thickness);
393: # $image->setAntiAliased($imcolor);
394: if ($filled) {
395: $image->filledArc($x,$y,$width,$height,$start,$end,
396: $imcolor);
397: } else {
398: $image->arc($x,$y,$width,$height,$start,$end,$imcolor);
399: }
400: } elsif ($type eq 'FILL') {
401: my ($x,$y,$color)=split(':',$env{$prefix."OBJ$i"});
402: if (!$color) { $color='000000'; }
403: my $imcolor=&get_color_from_hexstring($image,$color);
404: $image->fill($x,$y,$imcolor);
405: } elsif ($type eq 'IMAGE') {
406: my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
407: $srcH)=split(':',$env{$prefix."OBJ$i"});
408: $file=&unescape($file);
409: if (!defined($transparent)) { $transparent=1; }
410: my $subimage=&get_image($file,$transparent);
411: if (!defined($subimage)) {
412: &Apache::lonnet::logthis('Unable to create image object for '.
413: $file);
414: next;
415: }
416: if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
417: if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
418: if (!defined($destW) or !$destW) { $destW=$srcW; }
419: if (!defined($destH) or !$destH) { $destH=$srcH; }
420: $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
421: $srcW,$srcH);
422: } elsif ($type eq 'LABEL') {
423: my ($x,$y,$text,$font,$color,$direction,$rotation)=
424: split(':',$env{$prefix."OBJ$i"});
425: $text=&unescape($text);
426: my $imcolor=&get_color_from_hexstring($image,$color);
427: my $type='normal';
428: my ($height,$fontref);
429: if ($font eq 'tiny') {
430: $height=GD::Font->Tiny->height;
431: $fontref=GD::gdTinyFont;
432: } elsif ($font eq 'small') {
433: $height=GD::Font->Small->height;
434: $fontref=GD::gdSmallFont;
435: } elsif ($font eq 'medium') {
436: $height=GD::Font->MediumBold->height;
437: $fontref=GD::gdMediumBoldFont;
438: } elsif ($font eq 'large') {
439: $height=GD::Font->Large->height;
440: $fontref=GD::gdLargeFont;
441: } elsif ($font eq 'giant' || !$font) {
442: $height=GD::Font->Giant->height;
443: $fontref=GD::gdGiantFont;
444: } elsif ($image->useFontConfig(1)) {
445: $type='ttf';
446: }
447: if ($type eq 'normal' && $direction eq 'vertical') {
448: $image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
449: } elsif ($type eq 'normal') {
450: $image->string($fontref,$x,$y-$height,$text,$imcolor);
451: } elsif ($type eq 'ttf') {
452: my ($fontname,$ptsize)=split(/\s+/,$font);
453: my $angle = 0;
454: if ($rotation =~ /^(\-|\+|)\d+(|\.\d*)$/) {
455: $angle = Math::Trig::deg2rad($rotation);
456: } elsif ($direction eq 'vertical') {
457: $angle = Math::Trig::deg2rad(90);
458: } elsif ($direction eq 'horizontal') {
459: $angle = 0;
460: }
461: $image->stringFT($imcolor,$fontname,$ptsize,$angle,$x,$y,$text);
462: }
463: } else {
464: &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
465: }
466: }
467: if (exists($env{'form.clickdata'})) { &add_click($image); }
468: $image->setThickness(1);
469: $r->print($image->png);
470: return OK;
471: }
472:
473: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>