[LON-CAPA-cvs] cvs: loncom /interface domainprefs.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sun, 27 May 2007 21:21:27 -0000


raeburn		Sun May 27 17:21:27 2007 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm 
  Log:
  Only create thumbnail images where the width and height of the full size image are both larger than the width and height of the standard thumbnail (200 x 50).
  
  
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.15 loncom/interface/domainprefs.pm:1.16
--- loncom/interface/domainprefs.pm:1.15	Sun May 27 12:31:54 2007
+++ loncom/interface/domainprefs.pm	Sun May 27 17:21:26 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.15 2007/05/27 16:31:54 raeburn Exp $
+# $Id: domainprefs.pm,v 1.16 2007/05/27 21:21:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -550,6 +550,7 @@
                     $showfile = '';
                 }
             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
+                $showfile = $imgfile;
                 my $imgdir = $1;
                 my $filename = $2;
                 if (-e "/home/httpd/html/$imgdir/tn-".$filename) {
@@ -559,12 +560,17 @@
                     my $output = '/home/httpd/html/'.$imgdir.'/tn-'.$filename;
                     if (!-e $output) {
                         my ($width,$height) = &thumb_dimensions();
-                        my $size = $width.'x'.$height;
-                        system("convert -sample $size $input $output");
+                        my ($fullwidth,$fullheight) = &check_dimensions($input);
+                        if ($fullwidth ne '' && $fullheight ne '') {
+                            if ($fullwidth > $width && $fullheight > $height) { 
+                                my $size = $width.'x'.$height;
+                                system("convert -sample $size $input $output");
+                                $showfile = '/'.$imgdir.'/tn-'.$filename;
+                            }
+                        }
                     }
-                    $showfile = '/'.$imgdir.'/tn-'.$filename;
                 }
-            } 
+            }
             if ($showfile) {
                 $showfile = &Apache::loncommon::lonhttpdurl($showfile);
                 $fullsize =  &Apache::loncommon::lonhttpdurl($imgfile);
@@ -1237,6 +1243,26 @@
     return ('200','50');
 }
 
+sub check_dimensions {
+    my ($inputfile) = @_;
+    my ($fullwidth,$fullheight);
+    if ($inputfile =~ m|^[/\w.\-]+$|) {
+        if (open(PIPE,"identify $inputfile 2>&1 |")) {
+            my $imageinfo = <PIPE>;
+            if (!close(PIPE)) {
+                &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
+            }
+            chomp($imageinfo);
+            my ($fullsize) = 
+                ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)\s+/);
+            if ($fullsize) {
+                ($fullwidth,$fullheight) = split(/x/,$fullsize);
+            }
+        }
+    }
+    return ($fullwidth,$fullheight);
+}
+
 sub check_configuser {
     my ($uhome,$dom,$confname,$servadm) = @_;
     my ($configuserok,%currroles);
@@ -1369,17 +1395,23 @@
             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                 my $inputfile = $filepath.'/'.$file;
                 my $outfile = $filepath.'/'.'tn-'.$file;
-                my $thumbsize = $thumbwidth.'x'.$thumbheight;
-                system("convert -sample $thumbsize $inputfile $outfile");
-                chmod(0660, $filepath.'/tn-'.$file);
-                if (-e $outfile) {
-                    my $copyfile=$targetdir.'/tn-'.$file;
-                    if (copy($outfile,$copyfile)) {
-                        print $logfile "\nCopied source to ".$copyfile."\n";
-                        &write_metadata($dom,$confname,$formname,$targetdir,
-                                        'tn-'.$file,$logfile);
-                    } else {
-                        print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
+                my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
+                if ($fullwidth ne '' && $fullheight ne '') { 
+                    if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
+                        my $thumbsize = $thumbwidth.'x'.$thumbheight;
+                        system("convert -sample $thumbsize $inputfile $outfile");
+                        chmod(0660, $filepath.'/tn-'.$file);
+                        if (-e $outfile) {
+                            my $copyfile=$targetdir.'/tn-'.$file;
+                            if (copy($outfile,$copyfile)) {
+                                print $logfile "\nCopied source to ".$copyfile."\n";
+                                &write_metadata($dom,$confname,$formname,
+                                                $targetdir,'tn-'.$file,$logfile);
+                            } else {
+                                print $logfile "\nUnable to write ".$copyfile.
+                                               ':'.$!."\n";
+                            }
+                        }
                     }
                 }
             }