### Eclipse Workspace Patch 1.0
#P 18stable
Index: lib/gdlib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/gdlib.php,v
retrieving revision 1.13
diff -u -r1.13 gdlib.php
--- lib/gdlib.php	11 Feb 2006 16:17:06 -0000	1.13
+++ lib/gdlib.php	30 Sep 2007 01:07:27 -0000
@@ -269,4 +269,122 @@
     }
     return 0;
 }
+
+function save_profile_image2($id, $filname, $dir='users') { //JCP-18
+// 
+
+    global $CFG;
+
+    if (empty($CFG->gdversion)) {
+        return false;
+    }
+
+ // JCP-18   if (!$uploadmanager) {
+//  JCP-18        return false;
+//  JCP-18     }
+
+    umask(0000);
+
+    if (!file_exists($CFG->dataroot .'/'. $dir)) {
+        if (! mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) {
+            return false;
+        }
+    }
+
+    if (!file_exists($CFG->dataroot .'/'. $dir .'/'. $id)) {
+        if (! mkdir($CFG->dataroot .'/'. $dir .'/'. $id, $CFG->directorypermissions)) {
+            return false;
+        }
+    }
+
+    $destination = $CFG->dataroot .'/'. $dir .'/'. $id;
+    // JCP-18   if (!$uploadmanager->save_files($destination)) {
+    // JCP-18       return false;
+    // JCP-18   }
+
+    $originalfile =$filename; //JCP-18
+
+    $imageinfo = GetImageSize($originalfile);
+    
+    if (empty($imageinfo)) {
+        if (file_exists($originalfile)) {
+            unlink($originalfile);
+        }
+        return false;
+    }
+    
+    $image->width  = $imageinfo[0];
+    $image->height = $imageinfo[1];
+    $image->type   = $imageinfo[2];
+
+    switch ($image->type) {
+        case 1: 
+            if (function_exists('ImageCreateFromGIF')) {
+                $im = ImageCreateFromGIF($originalfile); 
+            } else {
+                notice('GIF not supported on this server');
+                unlink($originalfile);
+                return false;
+            }
+            break;
+        case 2: 
+            if (function_exists('ImageCreateFromJPEG')) {
+                $im = ImageCreateFromJPEG($originalfile); 
+            } else {
+                notice('JPEG not supported on this server');
+                unlink($originalfile);
+                return false;
+            }
+            break;
+        case 3:
+            if (function_exists('ImageCreateFromPNG')) {
+                $im = ImageCreateFromPNG($originalfile); 
+            } else {
+                notice('PNG not supported on this server');
+                unlink($originalfile);
+                return false;
+            }
+            break;
+        default: 
+            unlink($originalfile);
+            return false;
+    }
+
+    unlink($originalfile);
+
+    if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
+        $im1 = ImageCreateTrueColor(100,100);
+        $im2 = ImageCreateTrueColor(35,35);
+    } else {
+        $im1 = ImageCreate(100,100);
+        $im2 = ImageCreate(35,35);
+    }
+    
+    $cx = $image->width / 2;
+    $cy = $image->height / 2;
+
+    if ($image->width < $image->height) {
+        $half = floor($image->width / 2.0);
+    } else {
+        $half = floor($image->height / 2.0);
+    }
+
+    ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
+    ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
+
+    if (function_exists('ImageJpeg')) {
+        @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');  // Helps in Safe mode
+        @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg');  // Helps in Safe mode
+        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and 
+            ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
+            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
+            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
+            return 1;
+        }
+    } else {
+        notify('PHP has not been configured to support JPEG images.  Please correct this.');
+    }
+    return 0;
+}
+
 ?>
