File:  [LON-CAPA] / loncom / homework / randomlylabel.pm
Revision 1.18: download - view: text, annotated - select for diffs
Mon Feb 23 23:54:14 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- some debugging cleanup
- no longer blowup on polygon's with no points

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: # randomlabel.png: composite together text and images into 1 image
    4: #
    5: # $Id: randomlylabel.pm,v 1.18 2004/02/23 23:54:14 albertel Exp $
    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();
   37: use GD;
   38: use GD::Polyline();
   39: use LWP::UserAgent();
   40: 
   41: sub get_image {
   42:     my ($imgsrc,$set_trans)=@_;
   43:     my $image;
   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) {
   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 {
   74: 	$image = GD::Image->trueColor(1);
   75: 	$image = GD::Image->new($imgsrc);
   76:     }
   77:     if ($set_trans && defined($image)) {
   78: 	my $white=$image->colorExact(255,255,255);
   79: 	if ($white != -1) { $image->transparent($white); }
   80:     }
   81:     return $image;
   82: }
   83: 
   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: 
   96: sub handler {
   97:     my $r = shift;
   98:     $r->content_type('image/png');
   99:     $r->send_http_header;
  100:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
  101:     my $image;
  102:     if (defined($ENV{"cgi.$id.BGIMG"})) {
  103: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
  104: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
  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: 	}
  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;
  127:     }
  128:     #binmode(STDOUT);
  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));
  158: 	    my $added=0;
  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);
  162: 		$added++;
  163: 	    }
  164: 	    
  165: 	    $image->setThickness($width);
  166: 	    if ($added) {
  167: 		if ($open) {
  168: 		    $image->polydraw($polygon,$imcolor);
  169: 		} else {
  170: 		    $image->polygon($polygon,$imcolor);
  171: 		}
  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') {
  204: 	    my ($x,$y,$text,$font,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
  205: 	    $text=&Apache::lonnet::unescape($text);
  206: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  207: 	    my $height=GD::Font->Giant->height;
  208: 	    $image->string(GD::gdGiantFont,$x,$y-$height,$text,$imcolor);
  209: 	} else {
  210: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
  211: 	}
  212:     }
  213:     $image->setThickness(1);
  214:     $r->print($image->png);
  215:     return OK;
  216: }
  217: 
  218: 1;

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