We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46886
    • 1,154 Posts
    One of my goals is to have profile pics for my users. They are younger and like that kind of stuff, and they don't use gravetar.

    So I was searching yesterday, and found this solution which should work. The final version is on the 3rd page of the discussion, while the info on how to use it is on the 2nd page.

    forums.modx.com/thread/?thread=80740&page=3

    I will be checking this one out asap, unlike the more technical stuff, which I am irrationally hesitant to dive into. So, will report back soon.
      • 46220
      • 66 Posts
      How have you been able to get the profile pictures like this? I think I am nearly there, but the pictures will not show and I rather not use gravatar for profile pictures.
        • 46886
        • 1,154 Posts
        Ok let me give you what I got. This is all from my consultant, I hop he doesn't mind.

        First, instructions:

        You have to install phpThumbOf extra or better pThumb - http://modx.com/extras/package/pthumb to make the avatar images smaller in the upload section.
        pThumb works faster then phpThumbOf, but you have to uninstall the phpThumbOf first if you want to install pThumb.
        It works with the same syntax like phpThumbOf, so you don't have to replace the code.

        pThumb System settings:
        Go to System - System Settings - choose phpthumbof from the dropdown menu:
        Check file modification time phpthumbof.check_mod_time Yes (by default No - image in Update section remain the first uploaded,
        if Yes the image in the Update profile page is the actual one);
        * issue:
        When updating profile with new avatar, after the update the old avatar remains in the forum pages.
        After refreshing the page the new one pop up. It can be happen because of the subdomain testing.

        Things to do:
        Finding where the [[+avatar]] code comes from and replace the image source above (assets/userfiles/[[+username]]/avatar.gif) within.
        In this situation there will be no need to specify the extension .gif and the uploaded image files in the snippet .

        And here are the places to change the code in the templates:

        /* Avatar image in Header @wrapper.tpl*/
        core/components/discuss/themes/default/pages/wrapper.tpl

        Line 47-48
        Replaced <img src="[[+discuss.user.avatar_url]]" alt="[[+discuss.user.username]]'s Avatar">
        with
        <img src="[[+site_url]]assets/userfiles/[[+discuss.user.username]]/avatar.gif" alt="[[+discuss.user.username]]'s Avatar">

        ---

        /* Avatar image in Post @disthreadpost.chunk.tpl */
        core/components/discuss/themes/default/chunks/post/disthreadpost.chunk.tpl

        Line 35-36
        Replaced <img src="[[+author.avatar]]" alt="[[+author.username]]">
        with
        <img src="assets/userfiles/[[+author.username]]/avatar.gif" alt="[[+author.username]]">

        ---

        /* Avatar image in Preview Post @dispostpreview.chunk.tpl */
        core/components/discuss/themes/default/chunks/post/dispostpreview.chunk.tpl

        Line 5-6
        Replaced <img src="[[+author.avatar]]" alt="[[+author.username]]">
        with
        <img src="assets/userfiles/[[+author.username]]/avatar.gif" alt="[[+author.username]]">

        ---

        /* Avatar image in Profile Page Sidebar @user-sidebar.chunk.tpl */
        core/components/discuss/themes/default/modules/user-sidebar.chunk.tpl

        Line 7
        Changing the link https://en.gravatar.com/site/login#your-images
        to Edit Profile Link
        https://www.rayxilawforums.com/forums/login/update-profile.html?discuss=1

        Line 8
        Replaced <img src="[[+avatarUrl]]" alt="[[+username]]" />
        with
        <img src="assets/userfiles/[[+username]]/avatar.gif" alt="[[+username]]">

        You have to style the PanelBox-avatar image in the css file to be 80x80px (by default the images are getting their default resolution):
        /assets/components/discuss/themes/default/css/redo/forums-styles.css

        .PanelBox-avatar img{width:80px;height:80px}

        .m-profile-box li.Heading img {
        height: 80px;
        width: 80px;
        }
          • 46886
          • 1,154 Posts
          Snippet called avatarUpload

          <?php
          //LINE 13 IS IMPORTANT - PATH WHERE THE AVATARS ARE SAVED
          //LINE 41 IS IMPORTANT - FILE NAME
          // initialize output;
          $output = true;
          // get the current user name to for dicroty placement
          $userName = $modx->user->get('username');
           
          // valid extensions
          $ext_array = array('jpg', 'jpeg', 'gif', 'png');
           
          // create unique path for this form submission
          $uploadpath = 'assets/userfiles/' . $userName .'/';
           
          // get full path to unique folder
          $target_path = $modx->config['base_path'] . $uploadpath;
           
          // get uploaded file names:
          $submittedfiles = array_keys($_FILES);
           
           
          //get exsisting user profile data for Profile_Photos
          $fields = $modx->user->getOne('Profile')->get('extended');
          $Photo_fields = $fields['Profile_Photos'];
           
          // loop through files
          foreach ($submittedfiles as $sf) {
           
            // Get Filename and make sure its good.
            $filename = basename( $_FILES[$sf]['name'] );
           
            // Get file's extension
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            $ext = mb_strtolower($ext); // case insensitive
           
            if($filename != '') {
              $modx->log(modX::LOG_LEVEL_ERROR,'in file name loop'.$filename);
                              // is this the right type of file?
              if(in_array($ext, $ext_array)) {
                //create file called the filename that has been sanitized
                //$filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename));
                //$filename = $filename . '.'.$ext  ;
                $filename = avatar . '.'.gif  ; //replaced $filename . '.'.$ext with avatar .gif because of avatar_url chunk;
           
           
                // full path to new file
                $myTarget = $target_path . $filename;
           
                // create directory to move file into if it doesn't exist
                mkdir($target_path, 0755, true);
                if(file_exists($myTarget)) {
                  chmod($myTarget,0755); //Change the file permissions if allowed
                  unlink($myTarget); //remove the file
                }
           
                // is the file moved to the proper folder successfully?
                if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                  // set a new placeholder with the new full path (if you need it in subsequent hooks)
                  $hook->setValue($sf, $uploadpath . $filename);
                  // set the permissions on the file
                  if (!chmod($myTarget, 0644)) { /*some debug function*/ }  
                } 
                else {
                  // File not uploaded
                  $errorMsg = 'There was a problem uploading the file.';
                  $hook->addError($sf, $errorMsg);
                  $output = false; // generate submission error
                }
              } 
              else {
                // File type not allowed
                $errorMsg = 'Type of file not allowed.';
                $hook->addError($sf, $errorMsg);
                $output = false; // generate submission error
              }
            } 
            else {
              //check to see if there is exsisting photofields value and return value if so
              if ($Photo_fields != '')
              {
                $hook->setValue($sf, $Photo_fields);
                 
              }
              else{       
                // if no file, don't error, but return blank
                // is the file name empty (no file uploaded) and exsiting photofields empty
                $hook->setValue($sf, '');
              }
            }
          }
          return $output; 
            • 46886
            • 1,154 Posts
            The last thing is the update profile template, which is where the user can upload their pic. I am on a mac and don't know how to open it as code ;-( So will send later if you still need it.
              • 46220
              • 66 Posts
              Many thanks! I am almost there, the picture will not upload I think. But as it is past midnight now, I will try again later. It has been really helpfull!
                • 46886
                • 1,154 Posts
                How's it going now? Get it yet?
                  • 46220
                  • 66 Posts
                  It's not working and as far as I can tell, it will not upload/ save the picture. Haven't found out why or how yet.

                  Now that I think of it, it may have something to do with saving the update profile form itself. I'll check that first.
                    • 46220
                    • 66 Posts
                    Put me in a can and call me Blinky..... I've tried everything within my knowledge and I can't get it to work sad.
                      • 46886
                      • 1,154 Posts
                      Ok let's troubleshoot.

                      This should create a folder in your modx called images or avatars or user data or something, for me its in the part of the tree where templates are kept, but at a higher level than the templates. Do you have such a folder?

                      The deal works by taking whatever is uploaded (although be careful, the list of what files may be uploaded limits this), converts it to a gif format I believe, then saves in in the user folder inside the folder I mentioned above. And I believe the default name of that gif file is avatar.gif. So its like userdata/userone/avatar.gif.

                      The error system sucks so we can't get a lot of info. But pls check this first, it can tell us something perhaps.

                      I will think about it, so all that happens is when click on the upload pic button...nothing happens? The dialog comes up? Or what, more info pls. wink