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

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.17    ! albertel    5: # $Id: randomlylabel.pm,v 1.16 2004/02/23 22:52:30 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:     &Apache::lonnet::logthis("imagesrc1 is $imgsrc");
                     45:     if ($imgsrc !~ m|^(/home/)|) {
                     46: 	&Apache::lonnet::logthis("imagesrc2 is $imgsrc");
                     47: 	if ($imgsrc !~ /^http:/) {
                     48: 	    $imgsrc="http://".$ENV{'HTTP_HOST'}.$imgsrc;
                     49: 	}
                     50: 	&Apache::lonnet::logthis("imagesrc3 is $imgsrc");
                     51: 	&Apache::lonnet::logthis("LWP fetching image $imgsrc");
                     52: 	my $ua=new LWP::UserAgent;
                     53: 	my $request=new HTTP::Request('GET',"$imgsrc");
                     54: 	$request->header(Cookie => $ENV{'HTTP_COOKIE'});
                     55: 	my $file="/tmp/imagetmp".$$;
                     56: 	my $response=$ua->request($request,$file);
1.17    ! albertel   57: 	&Apache::lonnet::logthis("content is ".$response->content_type);
1.15      albertel   58: 	&Apache::lonnet::logthis($response->is_success);
                     59: 	&Apache::lonnet::logthis($response->status_line);
                     60: 	if ($response->is_success) {
                     61: 	    if ($response->content_type !~ m-/(png|jpg|jpeg)$-i) {
                     62: 		my $conv_image = Image::Magick->new;
                     63: 		my $current_figure = $conv_image->Read('filename'=>$file);
                     64: 		$conv_image->Set('magick'=>'png');
                     65: 		my @blobs=$conv_image->ImageToBlob();
                     66: 		undef $conv_image;
                     67: 		$image = GD::Image->new($blobs[0]);
                     68: 	    } else {
                     69: 		GD::Image->trueColor(1);
                     70: 		$image = GD::Image->new($file);
                     71: 	    }
                     72: 	}
                     73:     } elsif ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
1.3       albertel   74: 	my $conv_image = Image::Magick->new;
                     75: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
1.17    ! albertel   76: 	&Apache::lonnet::logthis("Read said $current_figure");
1.3       albertel   77: 	$conv_image->Set('magick'=>'png');
                     78: 	my @blobs=$conv_image->ImageToBlob();
                     79: 	undef $conv_image;
                     80: 	$image = GD::Image->new($blobs[0]);
                     81:     } else {
1.17    ! albertel   82: 	$image = GD::Image->trueColor(1);
1.3       albertel   83: 	$image = GD::Image->new($imgsrc);
1.17    ! albertel   84: 	if (!defined($image)) { 
        !            85: 	    &Apache::lonnet::logthis("Read said bluh");
        !            86: 	} else {
        !            87: 	    &Apache::lonnet::logthis("looks good");
        !            88: 	}
1.3       albertel   89:     }
1.9       albertel   90:     if ($set_trans && defined($image)) {
1.3       albertel   91: 	my $white=$image->colorExact(255,255,255);
                     92: 	if ($white != -1) { $image->transparent($white); }
                     93:     }
                     94:     return $image;
                     95: }
1.1       albertel   96: 
1.16      albertel   97: sub get_color_from_hexstring {
                     98:     my ($image,$color)=@_;
                     99:     if (!$color) { $color='000000'; }
                    100:     my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
                    101:     $red=hex($red);$green=hex($green);$blue=hex($blue);
                    102:     my $imcolor;
                    103:     if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
                    104: 	$imcolor = $image->colorClosestHWB($red,$green,$blue);
                    105:     }
                    106:     return $imcolor;
                    107: }
                    108: 
1.1       albertel  109: sub handler {
                    110:     my $r = shift;
                    111:     $r->content_type('image/png');
1.15      albertel  112:     $r->send_http_header;
1.11      albertel  113:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
1.17    ! albertel  114:     my $image;
        !           115:     if (defined($ENV{"cgi.$id.BGIMG"})) {
        !           116: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
        !           117: 	&Apache::lonnet::logthis("BGIMG is ".$bgimg);
        !           118: 	$image=&get_image($bgimg,0);
        !           119: 	if (! defined($image)) {
        !           120: 	    &Apache::lonnet::logthis('Unable to create image object for -'.
        !           121: 				     $id.'-'.$bgimg);
        !           122: 	    return OK;
        !           123: 	}
1.16      albertel  124:     } elsif (defined($ENV{"cgi.$id.SIZE"})) {
                    125: 	my ($width,$height)=split(':',$ENV{"cgi.$id.SIZE"});
                    126: 	$image = new GD::Image($width,$height,1);
                    127: 	my ($bgcolor)=split(':',$ENV{"cgi.$id.BGCOLOR"});
                    128: 	if ($bgcolor ne 'transparent') {
                    129: 	    $bgcolor=&get_color_from_hexstring($image,$bgcolor);
                    130: #	$image->rectangle(0,0,$width,$height,$bgcolor);
                    131: 	    $image->fill(0,0,$bgcolor);
                    132: 	} else {
                    133: 	    $bgcolor=&get_color_from_hexstring($image,'FFFFFF');
                    134: 	    $image->fill(0,0,$bgcolor);
                    135: 	    $image->transparent($bgcolor);
                    136: 	}
                    137:     } else {
                    138: 	&Apache::lonnet::logthis('Unable to create image object, no info');
                    139: 	return OK;
1.4       matthew   140:     }
1.1       albertel  141:     #binmode(STDOUT);
1.16      albertel  142:     my @objtypes=split(':',$ENV{"cgi.$id.OBJTYPE"});
                    143:     foreach(my $i=0;$i<$ENV{"cgi.$id.OBJCOUNT"};$i++) {
                    144: 	my $type=shift(@objtypes);
                    145: 	&Apache::lonnet::logthis("type is $type");
                    146: 	if ($type eq 'LINE') {
                    147: 	    my ($x1,$y1,$x2,$y2,$color,$thickness)=
                    148: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    149: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    150: 	    if (!defined($thickness)) { $thickness=1; }
                    151: 	    $image->setThickness($thickness);
                    152: 	    $image->setAntiAliased($imcolor);
                    153: 	    $image->line($x1,$y1,$x2,$y2,gdAntiAliased);
                    154: 	} elsif ($type eq 'RECTANGLE') {
                    155: 	    my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
                    156: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    157: 	    if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    158: 	    if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    159: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    160: 	    if (!defined($thickness)) { $thickness=1; }
                    161: 	    $image->setThickness($thickness);
                    162: #	    $image->setAntiAliased($imcolor);
                    163: 	    if ($filled) {
                    164: 		$image->filledRectangle($x1,$y1,$x2,$y2,$imcolor);
                    165: 	    } else {
                    166: 		$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
                    167: 	    }
                    168: 	} elsif ($type eq 'POLYGON') {
                    169: 	    my ($color,$width,$open)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    170: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    171: 	    my $polygon = (($open) ? (new GD::Polyline) : (new GD::Polygon));
                    172: 	    foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
                    173: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    174: 		$polygon->addPt($x,$y);
                    175: 	    }
                    176: 	    $image->setThickness($width);
                    177: 	    if ($open) {
                    178: 		$image->polydraw($polygon,$imcolor);
                    179: 	    } else {
                    180: 		$image->polygon($polygon,$imcolor);
                    181: 	    }
                    182: 	} elsif ($type eq 'ARC') {
                    183: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
                    184: 		split(':',$ENV{"cgi.$id.OBJ$i"});
                    185: 	    if (!$color) { $color='000000'; }
                    186: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    187: 	    if (!defined($thickness)) { $thickness=1; }
                    188: 	    $image->setThickness($thickness);
                    189: #	    $image->setAntiAliased($imcolor);
                    190: 	    if ($filled) {
                    191: 		$image->filledArc($x,$y,$width,$height,$start,$end,
                    192: 				  $imcolor);
                    193: 	    } else {
                    194: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
                    195: 	    }
                    196: 	} elsif ($type eq 'FILL') {
                    197: 	    my ($x,$y,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    198: 	    if (!$color) { $color='000000'; }
                    199: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    200: 	    $image->fill($x,$y,$imcolor);
                    201: 	} elsif ($type eq 'IMAGE') {
                    202: 	    my ($x,$y,$file,$transparent)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    203: 	    $file=&Apache::lonnet::unescape($file);
                    204: 	    if (!defined($transparent)) { $transparent=1; }
                    205: 	    my $subimage=&get_image($file,$transparent);
                    206: 	    if (!defined($subimage)) {
                    207: 		&Apache::lonnet::logthis('Unable to create image object for '.
                    208: 					 $file);
                    209: 		next;
                    210: 	    }
                    211: 	    $image->copy($subimage,$x,$y,0,0,$subimage->getBounds());
                    212: 	} elsif ($type eq 'LABEL') {
                    213: 	    my ($x,$y,$text,$font,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
                    214: 	    $text=&Apache::lonnet::unescape($text);
                    215: 	    my $imcolor=&get_color_from_hexstring($image,$color);
                    216: 	    my $height=GD::Font->Giant->height;
1.17    ! albertel  217: 	    $image->string(GD::gdGiantFont,$x,$y-$height,$text,$imcolor);
1.13      albertel  218: 	}
1.10      albertel  219:     }
                    220:     $image->setThickness(1);
1.3       albertel  221:     $r->print($image->png);
1.1       albertel  222:     return OK;
                    223: }
                    224: 
                    225: 1;

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