Browse Source

Added new functions to manage images in backend

José Loguercio 9 years ago
parent
commit
fb78d0e4ed
2 changed files with 115 additions and 18 deletions
  1. 77 13
      main/inc/lib/course.lib.php
  2. 38 5
      main/inc/lib/usermanager.lib.php

+ 77 - 13
main/inc/lib/course.lib.php

@@ -3114,9 +3114,10 @@ class CourseManager
      * @param   string  Course code
      * @param   string  File name
      * @param   string  The full system name of the image from which course picture will be created.
+     * @param   string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format
      * @return  bool    Returns the resulting. In case of internal error or negative validation returns FALSE.
      */
-    public static function update_course_picture($course_code, $filename, $source_file = null)
+    public static function update_course_picture($course_code, $filename, $source_file = null, $cropParameters = null)
     {
         $course_info = api_get_course_info($course_code);
         // course path
@@ -3132,18 +3133,20 @@ class CourseManager
             unlink($course_medium_image);
         }
 
-        $my_course_image = new Image($source_file);
-        $result = $my_course_image->send_image($course_image, -1, 'png');
-        // Resize image to 100x85 (should be 85x85 but 100x85 visually gives
-        // better results for most images people put as course icon)
-        if ($result) {
-            $medium = new Image($course_image);
-            //$picture_infos = $medium->get_image_size();
-            $medium->resize(100, 85, 0, false);
-            $medium->send_image($store_path . '/course-pic85x85.png', -1, 'png');
-        }
-
-        return $result;
+        //Crop the image to adjust 4:3 ratio
+        $croppedImage = self::crop_image($source_file, $cropParameters);
+        $croppedPath = $croppedImage->image_wrapper->path;
+        
+        //Resize the images in two formats
+        $medium = self::resize_picture($croppedPath, 85);
+        $normal = self::resize_picture($croppedPath, 300);
+        
+        $medium->send_image($course_medium_image, -1, 'png');
+        $result = $normal->send_image($course_image, -1, 'png');
+        $result = $medium && $medium->send_image($course_medium_image, -1, 'png') &&
+                $normal && $normal->send_image($course_image, -1, 'png');
+
+        return $result ? $result : false;
     }
 
     /**
@@ -5375,6 +5378,67 @@ class CourseManager
 
         return $send_to;
     }
+    
+    /**
+     * Resize a picture
+     *
+     * @param  string file picture
+     * @param  int size in pixels
+     * @todo move this function somewhere else image.lib?
+     * @return obj image object
+     */
+    public static function resize_picture($file, $max_size_for_picture)
+    {
+        $temp = false;
+        if (file_exists($file)) {
+            $temp = new Image($file);
+            $image_size = $temp->get_image_size($file);
+            $width = $image_size['width'];
+            $height = $image_size['height'];
+            if ($width >= $height) {
+                if ($width >= $max_size_for_picture) {
+                    // scale height
+                    $new_height = round($height * ($max_size_for_picture / $width));
+                    $temp->resize($max_size_for_picture, $new_height, 0);
+                }
+            } else { // height > $width
+                if ($height >= $max_size_for_picture) {
+                    // scale width
+                    $new_width = round($width * ($max_size_for_picture / $height));
+                    $temp->resize($new_width, $max_size_for_picture, 0);
+                }
+            }
+        }
+        return $temp;
+    }
+    
+    /**
+     * Crop a Image
+     * 
+     * @author José Loguercio <jose.loguercio@beeznest.com>
+     * @param string file picture
+     * @param string crop Parameters (x, y, width, height)
+     * @return obj image object
+     */
+    public static function crop_image($file, $cropParameters)
+    {
+        $image = false;
+        if (file_exists($file)) {
+            $image = new Image($file);
+            $image_size = $image->get_image_size($file);
+            $src_width = $image_size['width'];
+            $src_height = $image_size['height'];
+            $cropParameters = explode(",", $cropParameters);
+            $x = intval($cropParameters[0]);
+            $y = intval($cropParameters[1]);
+            $width = intval($cropParameters[2]);
+            $height = intval($cropParameters[3]);
+            
+            $image->crop($x, $y, $width, $height, $src_width, $src_height);
+        }
+        
+        return $image;
+    }
 
     /**
      * this function shows the form for sending a message to a specific group or user.

+ 38 - 5
main/inc/lib/usermanager.lib.php

@@ -1547,10 +1547,11 @@ class UserManager
      *                       If an empty name is provided, then old user photos are deleted only,
      * @see     UserManager::delete_user_picture() as the prefered way for deletion.
      * @param   string $source_file The full system name of the image from which user photos will be created.
+     * @param   string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format
      * @return  string/bool Returns the resulting common file name of created images which usually should be stored in database.
      * When deletion is requested returns empty string. In case of internal error or negative validation returns FALSE.
      */
-    public static function update_user_picture($user_id, $file = null, $source_file = null)
+    public static function update_user_picture($user_id, $file = null, $source_file = null, $cropParameters)
     {
         if (empty($user_id)) {
             return false;
@@ -1618,13 +1619,17 @@ class UserManager
             $filename = $user_id.'_'.$filename;
         }
 
+        //Crop the image to adjust 1:1 ratio
+        $croppedImage = self::crop_image($source_file, $cropParameters);
+        $croppedPath = $croppedImage->image_wrapper->path;
+        
         // Storing the new photos in 4 versions with various sizes.
 
-        $small = self::resize_picture($source_file, 22);
-        $medium = self::resize_picture($source_file, 85);
-        $normal = self::resize_picture($source_file, 200);
+        $small = self::resize_picture($croppedPath, 22);
+        $medium = self::resize_picture($croppedPath, 85);
+        $normal = self::resize_picture($croppedPath, 200);
 
-        $big = new Image($source_file); // This is the original picture.
+        $big = new Image($croppedPath); // This is the original picture.
 
         $ok = $small && $small->send_image($path.'small_'.$filename) &&
             $medium && $medium->send_image($path.'medium_'.$filename) &&
@@ -3057,6 +3062,34 @@ class UserManager
         }
         return $temp;
     }
+    
+    /**
+     * Crop a Image
+     * 
+     * @author José Loguercio <jose.loguercio@beeznest.com>
+     * @param string file picture
+     * @param string crop Parameters (x, y, width, height)
+     * @return obj image object
+     */
+    public static function crop_image($file, $cropParameters)
+    {
+        $image = false;
+        if (file_exists($file)) {
+            $image = new Image($file);
+            $image_size = $image->get_image_size($file);
+            $src_width = $image_size['width'];
+            $src_height = $image_size['height'];
+            $cropParameters = explode(",", $cropParameters);
+            $x = intval($cropParameters[0]);
+            $y = intval($cropParameters[1]);
+            $width = intval($cropParameters[2]);
+            $height = intval($cropParameters[3]);
+            
+            $image->crop($x, $y, $width, $height, $src_width, $src_height);
+        }
+        
+        return $image;
+    }
 
     /**
      * @author Isaac flores <isaac.flores@dokeos.com>