Annotation of loncom/homework/randomlylabel.pm, revision 1.21
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.21 ! albertel 5: # $Id: randomlylabel.pm,v 1.20 2004/07/15 18:06:09 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: #
29: ###
30:
31: package Apache::randomlylabel;
32:
33: use strict;
34: use Image::Magick;
35: use Apache::Constants qw(:common);
36: use Apache::loncommon();
1.17 albertel 37: use GD;
1.13 albertel 38: use GD::Polyline();
1.15 albertel 39: use LWP::UserAgent();
1.3 albertel 40:
41: sub get_image {
42: my ($imgsrc,$set_trans)=@_;
43: my $image;
1.15 albertel 44: if ($imgsrc !~ m|^(/home/)|) {
45: if ($imgsrc !~ /^http:/) {
46: $imgsrc="http://".$ENV{'HTTP_HOST'}.$imgsrc;
47: }
48: my $ua=new LWP::UserAgent;
49: my $request=new HTTP::Request('GET',"$imgsrc");
50: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
51: my $file="/tmp/imagetmp".$$;
52: my $response=$ua->request($request,$file);
53: if ($response->is_success) {
54: if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
55: my $conv_image = Image::Magick->new;
56: my $current_figure = $conv_image->Read('filename'=>$file);
1.20 albertel 57: $conv_image->Set('type'=>'TrueColor');
1.15 albertel 58: $conv_image->Set('magick'=>'png');
59: my @blobs=$conv_image->ImageToBlob();
60: undef $conv_image;
61: $image = GD::Image->new($blobs[0]);
62: } else {
63: GD::Image->trueColor(1);
64: $image = GD::Image->new($file);
65: }
66: }
67: } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3 albertel 68: my $conv_image = Image::Magick->new;
69: my $current_figure = $conv_image->Read('filename'=>$imgsrc);
1.20 albertel 70: $conv_image->Set('type'=>'TrueColor');
1.3 albertel 71: $conv_image->Set('magick'=>'png');
72: my @blobs=$conv_image->ImageToBlob();
73: undef $conv_image;
74: $image = GD::Image->new($blobs[0]);
75: } else {
1.17 albertel 76: $image = GD::Image->trueColor(1);
1.3 albertel 77: $image = GD::Image->new($imgsrc);
78: }
1.9 albertel 79: if ($set_trans && defined($image)) {
1.3 albertel 80: my $white=$image->colorExact(255,255,255);
81: if ($white != -1) { $image->transparent($white); }
82: }
83: return $image;
84: }
1.1 albertel 85:
1.16 albertel 86: sub get_color_from_hexstring {
87: my ($image,$color)=@_;
88: if (!$color) { $color='000000'; }
89: my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
90: $red=hex($red);$green=hex($green);$blue=hex($blue);
91: my $imcolor;
92: if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
93: $imcolor = $image->colorClosestHWB($red,$green,$blue);
94: }
95: return $imcolor;
96: }
97:
1.1 albertel 98: sub handler {
99: my $r = shift;
100: $r->content_type('image/png');
1.15 albertel 101: $r->send_http_header;
1.11 albertel 102: my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.17 albertel 103: my $image;
104: if (defined($ENV{"cgi.$id.BGIMG"})) {
105: my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
1.18 albertel 106: #&Apache::lonnet::logthis("BGIMG is ".$bgimg);
1.17 albertel 107: $image=&get_image($bgimg,0);
108: if (! defined($image)) {
109: &Apache::lonnet::logthis('Unable to create image object for -'.
110: $id.'-'.$bgimg);
111: return OK;
112: }
1.16 albertel 113: } elsif (defined($ENV{"cgi.$id.SIZE"})) {
114: my ($width,$height)=split(':',$ENV{"cgi.$id.SIZE"});
115: $image = new GD::Image($width,$height,1);
116: my ($bgcolor)=split(':',$ENV{"cgi.$id.BGCOLOR"});
117: if ($bgcolor ne 'transparent') {
118: $bgcolor=&get_color_from_hexstring($image,$bgcolor);
119: # $image->rectangle(0,0,$width,$height,$bgcolor);
120: $image->fill(0,0,$bgcolor);
121: } else {
122: $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
123: $image->fill(0,0,$bgcolor);
124: $image->transparent($bgcolor);
125: }
126: } else {
127: &Apache::lonnet::logthis('Unable to create image object, no info');
128: return OK;
1.4 matthew 129: }
1.1 albertel 130: #binmode(STDOUT);
1.16 albertel 131: my @objtypes=split(':',$ENV{"cgi.$id.OBJTYPE"});
132: foreach(my $i=0;$i<$ENV{"cgi.$id.OBJCOUNT"};$i++) {
133: my $type=shift(@objtypes);
134: if ($type eq 'LINE') {
135: my ($x1,$y1,$x2,$y2,$color,$thickness)=
136: split(':',$ENV{"cgi.$id.OBJ$i"});
137: my $imcolor=&get_color_from_hexstring($image,$color);
138: if (!defined($thickness)) { $thickness=1; }
139: $image->setThickness($thickness);
1.21 ! albertel 140: # $image->setAntiAliased($imcolor);
! 141: $image->line($x1,$y1,$x2,$y2,$imcolor);
1.16 albertel 142: } elsif ($type eq 'RECTANGLE') {
143: my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
144: split(':',$ENV{"cgi.$id.OBJ$i"});
145: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
146: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
147: my $imcolor=&get_color_from_hexstring($image,$color);
148: if (!defined($thickness)) { $thickness=1; }
149: $image->setThickness($thickness);
150: # $image->setAntiAliased($imcolor);
151: if ($filled) {
152: $image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
153: } else {
154: $image->rectangle($x1,$y1,$x2,$y2,$imcolor);
155: }
156: } elsif ($type eq 'POLYGON') {
1.21 ! albertel 157: my ($color,$width,$open,$filled)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16 albertel 158: my $imcolor=&get_color_from_hexstring($image,$color);
1.21 ! albertel 159: my $polygon = (($open && lc ($open ne 'no')) ?
! 160: (new GD::Polyline) : (new GD::Polygon));
1.18 albertel 161: my $added=0;
1.16 albertel 162: foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
163: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
164: $polygon->addPt($x,$y);
1.18 albertel 165: $added++;
1.16 albertel 166: }
1.18 albertel 167:
1.16 albertel 168: $image->setThickness($width);
1.18 albertel 169: if ($added) {
1.21 ! albertel 170: if ($open && lc($open) ne 'no') {
1.18 albertel 171: $image->polydraw($polygon,$imcolor);
1.21 ! albertel 172: } elsif ($filled && lc($filled) ne 'no') {
! 173: $image->filledPolygon($polygon,$imcolor);
1.18 albertel 174: } else {
175: $image->polygon($polygon,$imcolor);
176: }
1.16 albertel 177: }
178: } elsif ($type eq 'ARC') {
179: my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
180: split(':',$ENV{"cgi.$id.OBJ$i"});
181: if (!$color) { $color='000000'; }
182: my $imcolor=&get_color_from_hexstring($image,$color);
183: if (!defined($thickness)) { $thickness=1; }
184: $image->setThickness($thickness);
185: # $image->setAntiAliased($imcolor);
186: if ($filled) {
187: $image->filledArc($x,$y,$width,$height,$start,$end,
188: $imcolor);
189: } else {
190: $image->arc($x,$y,$width,$height,$start,$end,$imcolor);
191: }
192: } elsif ($type eq 'FILL') {
193: my ($x,$y,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
194: if (!$color) { $color='000000'; }
195: my $imcolor=&get_color_from_hexstring($image,$color);
196: $image->fill($x,$y,$imcolor);
197: } elsif ($type eq 'IMAGE') {
1.21 ! albertel 198: my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
! 199: $srcH)=split(':',$ENV{"cgi.$id.OBJ$i"});
1.16 albertel 200: $file=&Apache::lonnet::unescape($file);
201: if (!defined($transparent)) { $transparent=1; }
202: my $subimage=&get_image($file,$transparent);
203: if (!defined($subimage)) {
204: &Apache::lonnet::logthis('Unable to create image object for '.
205: $file);
206: next;
207: }
1.21 ! albertel 208: if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
! 209: if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
! 210: if (!defined($destW) or !$destW) { $destW=$srcW; }
! 211: if (!defined($destH) or !$destH) { $destH=$srcH; }
! 212: $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
! 213: $srcW,$srcH);
1.16 albertel 214: } elsif ($type eq 'LABEL') {
1.19 albertel 215: my ($x,$y,$text,$font,$color,$direction)=
216: split(':',$ENV{"cgi.$id.OBJ$i"});
1.16 albertel 217: $text=&Apache::lonnet::unescape($text);
218: my $imcolor=&get_color_from_hexstring($image,$color);
1.19 albertel 219: my $type='normal';
220: my ($height,$fontref);
221: if ($font eq 'tiny') {
222: $height=GD::Font->Tiny->height;
223: $fontref=GD::gdTinyFont;
224: } elsif ($font eq 'small') {
225: $height=GD::Font->Small->height;
226: $fontref=GD::gdSmallFont;
227: } elsif ($font eq 'medium') {
228: $height=GD::Font->MediumBold->height;
229: $fontref=GD::gdMediumBoldFont;
230: } elsif ($font eq 'large') {
231: $height=GD::Font->Large->height;
232: $fontref=GD::gdLargeFont;
1.21 ! albertel 233: } elsif ($font eq 'giant' || !$font) {
1.19 albertel 234: $height=GD::Font->Giant->height;
235: $fontref=GD::gdGiantFont;
236: } else {
237: $type='ttf';
238: }
239: if ($type eq 'normal' && $direction eq 'vertical') {
240: $image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
241: } elsif ($type eq 'normal') {
242: $image->string($fontref,$x,$y-$height,$text,$imcolor);
243: } elsif ($type eq 'ttf') {
244: my ($fontname,$ptsize)=split(/\s+/,$font);
245: $image->stringFT($imcolor,$fontname,$ptsize,90,$x,$y,$text);
246: }
1.18 albertel 247: } else {
248: &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
1.13 albertel 249: }
1.10 albertel 250: }
251: $image->setThickness(1);
1.3 albertel 252: $r->print($image->png);
1.1 albertel 253: return OK;
254: }
255:
256: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>