[LON-CAPA-cvs] cvs: loncom /homework randomlylabel.pm

albertel lon-capa-cvs@mail.lon-capa.org
Sun, 17 Nov 2002 09:43:40 -0000


albertel		Sun Nov 17 04:43:40 2002 EDT

  Modified files:              
    /loncom/homework	randomlylabel.pm 
  Log:
  - now uses GD to composite/annotate
  - Uses Image::Magick to read files other than png/jpeg
  
  
Index: loncom/homework/randomlylabel.pm
diff -u loncom/homework/randomlylabel.pm:1.2 loncom/homework/randomlylabel.pm:1.3
--- loncom/homework/randomlylabel.pm:1.2	Wed Nov 13 18:52:41 2002
+++ loncom/homework/randomlylabel.pm	Sun Nov 17 04:43:40 2002
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # randomlabel.png: composite together text and images into 1 image
 #
-# $Id: randomlylabel.pm,v 1.2 2002/11/13 23:52:41 albertel Exp $
+# $Id: randomlylabel.pm,v 1.3 2002/11/17 09:43:40 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -34,6 +34,27 @@
 use Image::Magick;
 use Apache::Constants qw(:common);
 use Apache::loncommon();
+use GD;
+
+sub get_image {
+    my ($imgsrc,$set_trans)=@_;
+    my $image;
+    if ($imgsrc !~ /\.(png|jpg|jpeg)$/) {
+	my $conv_image = Image::Magick->new;
+	my $current_figure = $conv_image->Read('filename'=>$imgsrc);
+	$conv_image->Set('magick'=>'png');
+	my @blobs=$conv_image->ImageToBlob();
+	undef $conv_image;
+	$image = GD::Image->new($blobs[0]);
+    } else {
+	$image = GD::Image->new($imgsrc);
+    }
+    if ($set_trans) {
+	my $white=$image->colorExact(255,255,255);
+	if ($white != -1) { $image->transparent($white); }
+    }
+    return $image;
+}
 
 sub handler {
     my $r = shift;
@@ -42,26 +63,20 @@
     &Apache::loncommon::get_unprocessed_cgi(
                &Apache::lonnet::unescape($ENV{'imagerequest.'.$token}));
     &Apache::lonnet::delenv('imagerequest\.'.$token);
-    my $image = Image::Magick->new;
-    my $current_figure = $image->Read('filename'=>$ENV{"form.BGIMG"});
+    my $image=&get_image($ENV{"form.BGIMG"},0);
     #binmode(STDOUT);
+    my $black = $image->colorAllocate(0,0,0);
     for(my $i=0;$i<$ENV{"form.ICOUNT"};$i++) {
-	my $subimage = Image::Magick->new;
-	$subimage->Read('filename' => $ENV{"form.IMG$i"});
-	$image->Composite('x' => $ENV{"form.X$i"},'y' => $ENV{"form.Y$i"},
-			 'compose'=> 'Over', 'image' => $subimage,
-			 'gravity' => 'NorthWest');
-	undef($subimage);
+	my $subimage=&get_image($ENV{"form.IMG$i"},1);
+	$image->copy($subimage,$ENV{"form.X$i"},$ENV{"form.Y$i"},
+		     0,0,$subimage->getBounds());
     }
+    my $height=GD::Font->Giant->height;
     for(my $i=0;$i<$ENV{"form.COUNT"};$i++) {
-	$image->Annotate('text' => $ENV{"form.LB$i"},'x' => $ENV{"form.X$i"},
-			 'y' => $ENV{"form.Y$i"}, 'font' => 'Times-Bold',
-			 'pointsize' => 18, 'antialias' => 'true',
-			 'gravity' => 'NorthWest');
+	$image->string(gdGiantFont,$ENV{"form.X$i"},$ENV{"form.Y$i"}-$height,
+		       $ENV{"form.LB$i"},$black);
     }
-    $image->Set('magick'=>'png');
-    my @blobs=$image->ImageToBlob();
-    $r->print($blobs[0]);
+    $r->print($image->png);
     return OK;
 }