File:  [LON-CAPA] / loncom / homework / randomlylabel.pm
Revision 1.15: download - view: text, annotated - select for diffs
Wed Feb 11 21:51:15 2004 UTC (20 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start of support for LWP gotten images

    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.15 2004/02/11 21:51:15 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:     &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);
   57: 	&Apache::lonnet::logthis("contetn is ".$response->content_type);
   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) {
   74: 	my $conv_image = Image::Magick->new;
   75: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
   76: 	$conv_image->Set('magick'=>'png');
   77: 	my @blobs=$conv_image->ImageToBlob();
   78: 	undef $conv_image;
   79: 	$image = GD::Image->new($blobs[0]);
   80:     } else {
   81: 	GD::Image->trueColor(1);
   82: 	$image = GD::Image->new($imgsrc);
   83:     }
   84:     if ($set_trans && defined($image)) {
   85: 	my $white=$image->colorExact(255,255,255);
   86: 	if ($white != -1) { $image->transparent($white); }
   87:     }
   88:     return $image;
   89: }
   90: 
   91: sub handler {
   92:     my $r = shift;
   93:     $r->content_type('image/png');
   94:     $r->send_http_header;
   95:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
   96:     &Apache::lonnet::logthis("BGIMG is ".$ENV{"cgi.$id.BGIMG"});
   97:     my $image=&get_image(&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"}),0);
   98:     if (! defined($image)) {
   99:         &Apache::lonnet::logthis('Unable to create image object for -'.$id.'-'.
  100: 				 $ENV{"cgi.$id.BGIMG"});
  101:         return OK;
  102:     }
  103:     #binmode(STDOUT);
  104:     my $black;
  105:     if (!($black=$image->colorResolve(0,0,0))) {
  106: 	$black = $image->colorClosestHWB(0,0,0);
  107:     }
  108:     for(my $i=0;$i<$ENV{"cgi.$id.ICOUNT"};$i++) {
  109: 	my $subimage=&get_image(&Apache::lonnet::unescape($ENV{"cgi.$id.IMG$i"}),1);
  110: 	if (!defined($subimage)) {
  111:             &Apache::lonnet::logthis('Unable to create image object for '.
  112:                                  $ENV{"cgi.$id.BGIMG"});
  113:             next;
  114:         }
  115: 	$image->copy($subimage,$ENV{"cgi.$id.IX$i"},$ENV{"cgi.$id.IY$i"},
  116: 		     0,0,$subimage->getBounds());
  117:     }
  118:     my $height=GD::Font->Giant->height;
  119:     for(my $i=0;$i<$ENV{"cgi.$id.COUNT"};$i++) {
  120: 	$image->string(GD::gdGiantFont,$ENV{"cgi.$id.X$i"},
  121: 		       $ENV{"cgi.$id.Y$i"}-$height,
  122: 		       &Apache::lonnet::unescape($ENV{"cgi.$id.LB$i"}),$black);
  123:     }
  124:     for(my $i=0;$i<$ENV{"cgi.$id.LINECOUNT"};$i++) {
  125: 	my ($x1,$y1,$x2,$y2,$color,$width)=split(':',$ENV{"cgi.$id.LINE$i"});
  126: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
  127: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
  128: 	my $imcolor;
  129: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
  130: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
  131: 	}
  132: 	$image->setThickness($width);
  133:        	$image->line($x1,$y1,$x2,$y2,$imcolor);
  134:     }
  135:     for(my $i=0;$i<$ENV{"cgi.$id.BOXCOUNT"};$i++) {
  136: 	my ($x1,$y1,$x2,$y2,$color,$width)=split(':',$ENV{"cgi.$id.BOX$i"});
  137: 	if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  138: 	if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  139: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
  140: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
  141: 	my $imcolor;
  142: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
  143: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
  144: 	}
  145: 	$image->setThickness($width);
  146:        	$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
  147:     }
  148:     for(my $i=0;$i<$ENV{"cgi.$id.POLYCOUNT"};$i++) {
  149: 	my ($color,$width,$open)=split(':',$ENV{"cgi.$id.POLYOPT$i"});
  150: 	my (undef,$red,undef,$green,undef,$blue)=split(/(..)/,$color);
  151: 	$red=hex($red);$green=hex($green);$blue=hex($blue);
  152: 	my $imcolor;
  153: 	if (!($imcolor = $image->colorResolve($red,$green,$blue))) {
  154: 	    $imcolor = $image->colorClosestHWB($red,$green,$blue);
  155: 	}
  156: 	my $polygon;
  157: 	if ($open) {
  158: 	    $polygon = new GD::Polyline;
  159: 	} else {
  160: 	    $polygon = new GD::Polygon;
  161: 	}
  162: 	foreach my $coord (split('-',$ENV{"cgi.$id.POLY$i"})) {
  163: 	    my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  164: 	    $polygon->addPt($x,$y);
  165: 	}
  166: 	$image->setThickness($width);
  167: 	if ($open) {
  168: 	    $image->polydraw($polygon,$imcolor);
  169: 	} else {
  170: 	    $image->polygon($polygon,$imcolor);
  171: 	}
  172:     }
  173:     $image->setThickness(1);
  174:     $r->print($image->png);
  175:     return OK;
  176: }
  177: 
  178: 1;

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