File:  [LON-CAPA] / loncom / homework / randomlylabel.pm
Revision 1.20: download - view: text, annotated - select for diffs
Thu Jul 15 18:06:09 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, HEAD
- BUG#3213, Fedora Core 2 GD isn't reading pallatized PNGs corectly, force them to truecolor mode

    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.20 2004/07/15 18:06:09 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('type'=>'TrueColor');
   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) {
   68: 	my $conv_image = Image::Magick->new;
   69: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
   70: 	$conv_image->Set('type'=>'TrueColor');
   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 {
   76: 	$image = GD::Image->trueColor(1);
   77: 	$image = GD::Image->new($imgsrc);
   78:     }
   79:     if ($set_trans && defined($image)) {
   80: 	my $white=$image->colorExact(255,255,255);
   81: 	if ($white != -1) { $image->transparent($white); }
   82:     }
   83:     return $image;
   84: }
   85: 
   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: 
   98: sub handler {
   99:     my $r = shift;
  100:     $r->content_type('image/png');
  101:     $r->send_http_header;
  102:     my (undef,$id) = split(/=/,$ENV{'QUERY_STRING'});
  103:     my $image;
  104:     if (defined($ENV{"cgi.$id.BGIMG"})) {
  105: 	my $bgimg=&Apache::lonnet::unescape($ENV{"cgi.$id.BGIMG"});
  106: 	#&Apache::lonnet::logthis("BGIMG is ".$bgimg);
  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: 	}
  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;
  129:     }
  130:     #binmode(STDOUT);
  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);
  140: 	    $image->setAntiAliased($imcolor);
  141: 	    $image->line($x1,$y1,$x2,$y2,gdAntiAliased);
  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') {
  157: 	    my ($color,$width,$open)=split(':',$ENV{"cgi.$id.OBJ$i"});
  158: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  159: 	    my $polygon = (($open) ? (new GD::Polyline) : (new GD::Polygon));
  160: 	    my $added=0;
  161: 	    foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
  162: 		my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  163: 		$polygon->addPt($x,$y);
  164: 		$added++;
  165: 	    }
  166: 	    
  167: 	    $image->setThickness($width);
  168: 	    if ($added) {
  169: 		if ($open) {
  170: 		    $image->polydraw($polygon,$imcolor);
  171: 		} else {
  172: 		    $image->polygon($polygon,$imcolor);
  173: 		}
  174: 	    }
  175: 	} elsif ($type eq 'ARC') {
  176: 	    my ($x,$y,$width,$height,$start,$end,$color,$thickness,$filled)=
  177: 		split(':',$ENV{"cgi.$id.OBJ$i"});
  178: 	    if (!$color) { $color='000000'; }
  179: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  180: 	    if (!defined($thickness)) { $thickness=1; }
  181: 	    $image->setThickness($thickness);
  182: #	    $image->setAntiAliased($imcolor);
  183: 	    if ($filled) {
  184: 		$image->filledArc($x,$y,$width,$height,$start,$end,
  185: 				  $imcolor);
  186: 	    } else {
  187: 		$image->arc($x,$y,$width,$height,$start,$end,$imcolor);
  188: 	    }
  189: 	} elsif ($type eq 'FILL') {
  190: 	    my ($x,$y,$color)=split(':',$ENV{"cgi.$id.OBJ$i"});
  191: 	    if (!$color) { $color='000000'; }
  192: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  193: 	    $image->fill($x,$y,$imcolor);
  194: 	} elsif ($type eq 'IMAGE') {
  195: 	    my ($x,$y,$file,$transparent)=split(':',$ENV{"cgi.$id.OBJ$i"});
  196: 	    $file=&Apache::lonnet::unescape($file);
  197: 	    if (!defined($transparent)) { $transparent=1; }
  198: 	    my $subimage=&get_image($file,$transparent);
  199: 	    if (!defined($subimage)) {
  200: 		&Apache::lonnet::logthis('Unable to create image object for '.
  201: 					 $file);
  202: 		next;
  203: 	    }
  204: 	    $image->copy($subimage,$x,$y,0,0,$subimage->getBounds());
  205: 	} elsif ($type eq 'LABEL') {
  206: 	    my ($x,$y,$text,$font,$color,$direction)=
  207: 		split(':',$ENV{"cgi.$id.OBJ$i"});
  208: 	    $text=&Apache::lonnet::unescape($text);
  209: 	    my $imcolor=&get_color_from_hexstring($image,$color);
  210: 	    my $type='normal';
  211: 	    my ($height,$fontref);
  212: 	    if ($font eq 'tiny') {
  213: 		$height=GD::Font->Tiny->height;
  214: 		$fontref=GD::gdTinyFont;
  215: 	    } elsif ($font eq 'small') {
  216: 		$height=GD::Font->Small->height;
  217: 		$fontref=GD::gdSmallFont;
  218: 	    } elsif ($font eq 'medium') {
  219: 		$height=GD::Font->MediumBold->height;
  220: 		$fontref=GD::gdMediumBoldFont;
  221: 	    } elsif ($font eq 'large') {
  222: 		$height=GD::Font->Large->height;
  223: 		$fontref=GD::gdLargeFont;
  224: 	    } elsif ($font eq 'giant' || !defined($font)) {
  225: 		$height=GD::Font->Giant->height;
  226: 		$fontref=GD::gdGiantFont;
  227: 	    } else {
  228: 		$type='ttf';
  229: 	    }
  230: 	    if ($type eq 'normal' && $direction eq 'vertical') {
  231: 		$image->stringUp($fontref,$x,$y-$height,$text,$imcolor);
  232: 	    } elsif ($type eq 'normal') {
  233: 		$image->string($fontref,$x,$y-$height,$text,$imcolor);
  234: 	    } elsif ($type eq 'ttf') {
  235: 		my ($fontname,$ptsize)=split(/\s+/,$font);
  236: 		$image->stringFT($imcolor,$fontname,$ptsize,90,$x,$y,$text);
  237: 	    }
  238: 	} else {
  239: 	    &Apache::lonnet::logthis("randomlylabel unable to handle object of type $type");
  240: 	}
  241:     }
  242:     $image->setThickness(1);
  243:     $r->print($image->png);
  244:     return OK;
  245: }
  246: 
  247: 1;

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