[LON-CAPA-cvs] cvs: loncom /interface lonaboutme.pm /lonnet/perl lonnet.pm

raeburn raeburn@source.lon-capa.org
Sun, 21 Feb 2010 06:21:57 -0000


This is a MIME encoded message

--raeburn1266733317
Content-Type: text/plain

raeburn		Sun Feb 21 06:21:57 2010 EDT

  Modified files:              
    /loncom/interface	lonaboutme.pm 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  - Make scaling of uploaded photos more generic.
    - Eliminate package variable: $upload_photo_form.
    - Additional args for &lonnet::userfileupload() and &lonnet::finishuserfileupload()
      - $resizewidth and $resizeheight.
      - if defined and uploaded file is an image file (as determined by File::MMagic) perform scaling
        as required so image dimensions fall within $resizewidth and $resizeheight.
  - Eliminate call to &Apache::lonnet::resizeImage() when displaying AboutMe page.
  
  
--raeburn1266733317
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20100221062157.txt"

Index: loncom/interface/lonaboutme.pm
diff -u loncom/interface/lonaboutme.pm:1.142 loncom/interface/lonaboutme.pm:1.143
--- loncom/interface/lonaboutme.pm:1.142	Sat Jan 30 18:02:27 2010
+++ loncom/interface/lonaboutme.pm	Sun Feb 21 06:21:50 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Personal Information Page
 #
-# $Id: lonaboutme.pm,v 1.142 2010/01/30 18:02:27 raeburn Exp $
+# $Id: lonaboutme.pm,v 1.143 2010/02/21 06:21:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -327,7 +327,8 @@
                 &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
             }
             $syllabus{'uploaded.photourl'}=
-                &Apache::lonnet::userphotoupload('uploaddoc','aboutme');
+                &Apache::lonnet::userfileupload('uploaddoc',undef,'aboutme',
+                    undef,undef,undef,undef,undef,undef,undef,'400','500');
          }
         $syllabus{'uploaded.lastmodified'}=time;
         &Apache::lonnet::put('aboutme',\%syllabus,$cdom,$cnum);
@@ -356,22 +357,19 @@
         if ($syllabus{'uploaded.photourl'}) {
             &Apache::lonnet::allowuploaded('/adm/aboutme',$syllabus{'uploaded.photourl'});
 
-           #This call is to resize all "Personal Information" images in the LonCapa System. When its done, you can remove this line.
-            &Apache::lonnet::resizeImage(&Apache::lonnet::filelocation('',$syllabus{'uploaded.photourl'}));
-           #---End Resize---
-
             $image=qq|<img name="userPhoto" src="$syllabus{'uploaded.photourl'} " class="LC_AboutMe_Image" alt="Photo of the user" />|;
 
             if ($target eq 'tex') {
                 $image=&Apache::lonxml::xmlparse($r,'tex',$image);
             }
-
         }
 
         if ($allowed) {
             $r->print(
                 '<form name="UploadPhoto" method="post" enctype="multipart/form-data" action="">'.
                 '<h3>'.&mt('Upload a Photo').'</h3>'.
+                '<p class="LC_info">'.
+                &mt('LON-CAPA will automatically scale your uploaded file so the image will not exceed a width of 400px and a height of 500px.').'</p>'.
                 '<input type="file" name="uploaddoc" size="50" />'.
                 '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
                 '<input type="hidden" name="popup" value="'.$env{'form.popup'}.'" />'.
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1050 loncom/lonnet/perl/lonnet.pm:1.1051
--- loncom/lonnet/perl/lonnet.pm:1.1050	Sun Feb 21 02:38:31 2010
+++ loncom/lonnet/perl/lonnet.pm	Sun Feb 21 06:21:57 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1050 2010/02/21 02:38:31 raeburn Exp $
+# $Id: lonnet.pm,v 1.1051 2010/02/21 06:21:57 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -99,8 +99,6 @@
 my $readit;
 my $max_connection_retries = 10;     # Or some such value.
 
-my $upload_photo_form = 0; #Variable to check  when user upload a photo 0=not 1=true
-
 require Exporter;
 
 our @ISA = qw (Exporter);
@@ -2156,31 +2154,42 @@
     $fname=~s/\.(\d+)(?=\.)/_$1/g;
     return $fname;
 }
-#This Function check if a Image max 400px width and height 500px. If not then scale the image down
+# This Function checks if an Image's dimensions exceed either $resizewidth (width) 
+# or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
+# image with the same aspect ratio as the original, but with dimensions which do 
+# not exceed $resizewidth and $resizeheight.
+ 
 sub resizeImage {
-	my($img_url) = @_;	
-	my $ima = Image::Magick->new;                       
-        $ima->Read($img_url);
-	if($ima->Get('width') > 400)
-	{
-		my $factor = $ima->Get('width')/400;
-             	$ima->Scale( width=>400, height=>$ima->Get('height')/$factor );
-	}
-	if($ima->Get('height') > 500)
-        {
-        	my $factor = $ima->Get('height')/500;
-                $ima->Scale( width=>$ima->Get('width')/$factor, height=>500);
-        } 
-		
-	$ima->Write($img_url);
-}
-
-#Wrapper function for userphotoupload
-sub userphotoupload
-{
-	my($formname,$subdir) = @_;
-	$upload_photo_form = 1;
-	return &userfileupload($formname,undef,$subdir);
+    my ($img_path,$resizewidth,$resizeheight) = @_;
+    my $ima = Image::Magick->new;
+    my $resized;
+    if (-e $img_path) {
+        $ima->Read($img_path);
+        if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
+            my $width = $ima->Get('width');
+            my $height = $ima->Get('height');
+            if ($width > $resizewidth) {
+	        my $factor = $width/$resizewidth;
+                my $newheight = $height/$factor;
+                $ima->Scale(width=>$resizewidth,height=>$newheight);
+                $resized = 1;
+            }
+        }
+        if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
+            my $width = $ima->Get('width');
+            my $height = $ima->Get('height');
+            if ($height > $resizeheight) {
+                my $factor = $height/$resizeheight;
+                my $newwidth = $width/$factor;
+                $ima->Scale(width=>$newwidth,height=>$resizeheight);
+                $resized = 1;
+            }
+        }
+        if ($resized) {
+            $ima->Write($img_path);
+        }
+    }
+    return;
 }
 
 # --------------- Take an uploaded file and put it into the userfiles directory
@@ -2196,14 +2205,15 @@
 #        $dsetudom - domain for permanaent storage of uploaded file
 #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
+#        $resizewidth - width (pixels) to which to resize uploaded image
+#        $resizeheight - height (pixels) to which to resize uploaded image
 # 
 # output: url of file in userspace, or error: <message> 
 #             or /adm/notfound.html if failure to upload occurse
 
-
 sub userfileupload {
     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
-        $destudom,$thumbwidth,$thumbheight)=@_;
+        $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight)=@_;
     if (!defined($subdir)) { $subdir='unknown'; }
     my $fname=$env{'form.'.$formname.'.filename'};
     $fname=&clean_filename($fname);
@@ -2253,7 +2263,8 @@
         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
             return &finishuserfileupload($docuname,$docudom,
 					 $formname,$fname,$parser,$allfiles,
-					 $codebase,$thumbwidth,$thumbheight);
+					 $codebase,$thumbwidth,$thumbheight,
+                                         $resizewidth,$resizeheight);
         } else {
             $fname=$env{'form.folder'}.'/'.$fname;
             return &process_coursefile('uploaddoc',$docuname,$docudom,
@@ -2265,7 +2276,8 @@
         my $docudom=$destudom;
 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 				     $parser,$allfiles,$codebase,
-                                     $thumbwidth,$thumbheight);
+                                     $thumbwidth,$thumbheight,
+                                     $resizewidth,$resizeheight);
         
     } else {
         my $docuname=$env{'user.name'};
@@ -2276,13 +2288,14 @@
         }
 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 				     $parser,$allfiles,$codebase,
-                                     $thumbwidth,$thumbheight);
+                                     $thumbwidth,$thumbheight,
+                                     $resizewidth,$resizeheight);
     }
 }
 
 sub finishuserfileupload {
     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
-        $thumbwidth,$thumbheight) = @_;
+        $thumbwidth,$thumbheight,$resizewidth,$resizeheight) = @_;
     my $path=$docudom.'/'.$docuname.'/';
     my $filepath=$perlvar{'lonDocRoot'};
   
@@ -2314,10 +2327,12 @@
 	    return '/adm/notfound.html';
 	}
 	close(FH);
-	if($upload_photo_form==1)
-	{
-		resizeImage($filepath.'/'.$file);		
-		$upload_photo_form = 0;
+        if ($resizewidth && $resizeheight) {
+            my $mm = new File::MMagic;
+            my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
+            if ($mime_type =~ m{^image/}) {
+	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
+            }  
 	}
     }
     if ($parser eq 'parse') {

--raeburn1266733317--