Annotation of loncom/homework/randomlylabel.pm, revision 1.19

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

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