File:  [LON-CAPA] / loncom / homework / randomlylabel.pm
Revision 1.5: download - view: text, annotated - select for diffs
Thu Jan 23 18:26:10 2003 UTC (21 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
randomlabel.pm did not replicate the file (part of Bug #1187).
Calls repcopy now. Delivers "broken" image if file could not be replicated
for some reason.
Side fix:
Users had problems with foo.GIF as background image. Made regular expressions
case-insensitive.

    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.5 2003/01/23 18:26:10 www 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: 
   39: sub get_image {
   40:     my ($imgsrc,$set_trans)=@_;
   41:     my $image;
   42:     if ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
   43: 	my $conv_image = Image::Magick->new;
   44: 	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
   45: 	$conv_image->Set('magick'=>'png');
   46: 	my @blobs=$conv_image->ImageToBlob();
   47: 	undef $conv_image;
   48: 	$image = GD::Image->new($blobs[0]);
   49:     } else {
   50: 	$image = GD::Image->new($imgsrc);
   51:     }
   52:     if ($set_trans) {
   53: 	my $white=$image->colorExact(255,255,255);
   54: 	if ($white != -1) { $image->transparent($white); }
   55:     }
   56:     return $image;
   57: }
   58: 
   59: sub handler {
   60:     my $r = shift;
   61:     $r->content_type('image/png');
   62:     my (undef,$token) = split(/=/,$ENV{'QUERY_STRING'});
   63:     &Apache::loncommon::get_unprocessed_cgi(
   64:                &Apache::lonnet::unescape($ENV{'imagerequest.'.$token}));
   65:     &Apache::lonnet::delenv('imagerequest\.'.$token);
   66:     my $image=&get_image($ENV{"form.BGIMG"},0);
   67:     if (! defined($image)) {
   68:         &Apache::lonnet::logthis('Unable to create image object for '.$ENV{"form.BGIMG"});
   69:         return OK;
   70:     }
   71:     #binmode(STDOUT);
   72:     my $black = $image->colorAllocate(0,0,0);
   73:     for(my $i=0;$i<$ENV{"form.ICOUNT"};$i++) {
   74: 	my $subimage=&get_image($ENV{"form.IMG$i"},1);
   75: 	$image->copy($subimage,$ENV{"form.X$i"},$ENV{"form.Y$i"},
   76: 		     0,0,$subimage->getBounds());
   77:     }
   78:     my $height=GD::Font->Giant->height;
   79:     for(my $i=0;$i<$ENV{"form.COUNT"};$i++) {
   80: 	$image->string(gdGiantFont,$ENV{"form.X$i"},$ENV{"form.Y$i"}-$height,
   81: 		       $ENV{"form.LB$i"},$black);
   82:     }
   83:     $r->print($image->png);
   84:     return OK;
   85: }
   86: 
   87: 1;

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