Example #1
0
 /**
  * Save to disk and attach to user
  *
  * @param object User (User MUST BE created in DB)
  * @param string content of image file
  * @param boolean TRUE - to expand photos to a square
  */
 function userimg_attach_photo(&$User, $image_content, $expand_pics)
 {
     if (empty($image_content)) {
         // No image content:
         return;
     }
     // Load FileRoot class:
     load_class('files/model/_fileroot.class.php', 'FileRoot');
     // Try to create FileRoot for the user:
     $FileRootCache =& get_FileRootCache();
     $fileroot_ID = FileRoot::gen_ID('user', $User->ID);
     $user_FileRoot =& $FileRootCache->get_by_ID($fileroot_ID, true);
     if (!$user_FileRoot) {
         // Impossible to create FileRoot for the User
         $this->debug_log(sprintf('FileRoot cannot be created for User #%s', $User->ID));
         // Exit here:
         return;
     }
     // Try to create a folder for image:
     $folder_name = 'profile_pictures';
     // Folder name in user dir where we should store image file
     $image_name = 'ldap.jpg';
     // File name of the image file
     $folder_path = $user_FileRoot->ads_path . $folder_name;
     $image_path = $folder_path . '/' . $image_name;
     if (!mkdir_r($folder_path)) {
         // Folder cannot be created
         $this->debug_log(sprintf('Cannot create image folder <b>%s</b>', $folder_path));
         // Exit here:
         return;
     }
     // Create/rewrite image file:
     $image_handle = fopen($image_path, 'w+');
     if ($image_handle === false) {
         // File cannot be created
         $this->debug_log(sprintf('Cannot create image file <b>%s</b>', $image_path));
         // Exit here:
         return;
     }
     // Write image content in the file:
     fwrite($image_handle, $image_content);
     fclose($image_handle);
     // Create file object to work with image:
     $File = new File('user', $User->ID, $folder_name . '/' . $image_name);
     $File->rm_cache();
     $File->load_meta(true);
     if ($expand_pics) {
         // Expand a photo to a square:
         $this->userimg_expand_to_square($File);
     }
     // Link image file to the user:
     $LinkOwner = new LinkUser($User);
     $File->link_to_Object($LinkOwner);
     $avatar_file_ID = $User->get('avatar_file_ID');
     if (empty($avatar_file_ID)) {
         // If user has no main avatar yet then use this new one:
         $User->set('avatar_file_ID', $File->ID);
         $User->dbupdate();
     }
 }