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

ng lon-capa-cvs@mail.lon-capa.org
Fri, 01 Aug 2003 14:22:07 -0000


ng		Fri Aug  1 10:22:07 2003 EDT

  Modified files:              
    /loncom/homework	imageresponse.pm 
  Log:
  Red X marks the spot using GD instead of imageMagick.
  
  
  
Index: loncom/homework/imageresponse.pm
diff -u loncom/homework/imageresponse.pm:1.32 loncom/homework/imageresponse.pm:1.33
--- loncom/homework/imageresponse.pm:1.32	Thu Jul 31 16:38:12 2003
+++ loncom/homework/imageresponse.pm	Fri Aug  1 10:22:07 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # image click response style
 #
-# $Id: imageresponse.pm,v 1.32 2003/07/31 20:38:12 ng Exp $
+# $Id: imageresponse.pm,v 1.33 2003/08/01 14:22:07 ng Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,13 +25,15 @@
 #
 # http://www.lon-capa.org/
 #
-
+# July,August 2003 H. K. Ng
+#
 #FIXME LATER assumes multiple possible submissions but only one is possible 
 #currently
 
 package Apache::imageresponse;
 use strict;
 use Image::Magick;
+use GD;
 
 BEGIN {
   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
@@ -312,6 +314,27 @@
   return $result;
 }
 
+sub get_image {
+    my ($imgsrc,$set_trans)=@_;
+    my $image;
+    if ($imgsrc !~ /\.(png|jpg|jpeg)$/i) {
+	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 {
+	GD::Image->trueColor(1);
+	$image = GD::Image->new($imgsrc);
+    }
+    if ($set_trans && defined($image)) {
+	my $white=$image->colorExact(255,255,255);
+	if ($white != -1) { $image->transparent($white); }
+    }
+    return $image;
+}
+
 sub end_image {
   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   my $result;
@@ -325,25 +348,27 @@
     my $y=$ENV{"form.HWVAL_$id:$temp.y"};
     if (defined ($x) && defined ($y)) {
 	&Apache::lonxml::debug("x and y defined as $x,$y");
-	my $magickImage = Image::Magick->new;
-	my $currentImage = $magickImage->Read('/home/httpd/html'.$image);
+	my $currentImage = &get_image('/home/httpd/html'.$image,1);
+	if (! defined($currentImage)) {
+	    &Apache::lonnet::logthis('Unable to create image object for '.$image);
+	    return '';
+	}
+	my $red;
+	if (!($red = $currentImage->colorResolve(255,0,0))) {
+	    $red = $currentImage->colorClosestHWB(255,0,0);
+	}
 	my $length = 6;
-	$currentImage = $magickImage->Draw(primitive=>'line',
-					   stroke=>'red',
-					   points=>($x-$length).','.($y-$length).' '.
-					   ($x+$length).','.($y+$length));
-	$currentImage = $magickImage->Draw(primitive=>'line',
-					   stroke=>'red',
-					   points=>($x-$length).','.($y+$length).' '.
-					   ($x+$length).','.($y-$length));
-	binmode STDOUT;
-	my ($graphExt) = ($image =~ /.*\.(.*)$/);
-	&Apache::lonxml::debug("graph mime type $graphExt");
-	my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".(time).'.'.$graphExt; #needs to be more random
+	$currentImage->line($x-$length,$y-$length,$x+$length,$y+$length,$red);
+	$currentImage->line($x-$length,$y+$length,$x+$length,$y-$length,$red);
+
+	my ($nameWOext) = ($image =~ /^.*\/(.*)\..*$/);
+	&Apache::lonxml::debug("graph name $nameWOext");
+	my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".
+	    $nameWOext.'.png'; #needs to be more random or specific
 	my $newImageName = '/home/httpd'.$webImageName;
 
-	$currentImage = $magickImage->Write($graphExt.':'.$newImageName);
-	undef $magickImage;
+	my $imgfh = Apache::File->new('>'.$newImageName); 
+	print $imgfh $currentImage->png;
 	$image = $webImageName;
     }
     &Apache::lonxml::debug("out image is $image");