Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Change picture (crop) image source

Hi Team,

I managed to reference all user photo to external resource (s3), by putting userPhoto & userPhotoUrl function on a plugin (thanks to this https://open.vanillaforums.com/discussion/33180/i-need-to-reference-external-avatar-source-please-help). Profile avatar & discussion avatar are all referencing external however the change picture page (image to be crop & thumbnail view) are still referencing image source from local uploads folder not the external (s3) configured on userPhoto & userPhotoUrl function.

BTW, uploading using the image crop are going to external s3 bucket, that's part is done. The challenge is the image crop & thumbnail view referencing local "/uploads" folder which is being wipe out every time we deploy code, we persist images on s3 bucket.

I tried looking for profile/picture.php file to overwrite but no luck.

TIA

Comments

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    I have no solution, only two ideas. Try

    public function profileController_render_before($sender) {
       if ($sender->RequestMethod !== 'picture') {
            // This is not the method you are looking for.
            return;
        }
    
        // Local pictures begin with "userpics/" in my installation.
        if(substr($sender->User->Photo, 0, 9) !== 'userpics/') {
            // No local image.
            return;
        }
    
        $sender->User->Photo = $amazonPrefix.$sender->User->Photo;
    }
    

    Maybe that hook is too late and you need to change the photo as soon as the user information is provided. The this might be your only chance:

    public function profileController_userLoaded_handler($sender) {
       if ($sender->RequestMethod !== 'picture') {
            // This is not the method you are looking for.
            return;
        }
    
        // Local pictures begin with "userpics/" in my installation.
        if(substr($sender->User->Photo, 0, 9) !== 'userpics/') {
            // No local image.
            return;
        }
    
        $sender->User->Photo = $amazonPrefix.$sender->User->Photo;
    }
    

    The "UserLoaded" event is fired everywhere in the profile controller. Therefore it is not very efficient to solve the problem that way.

  • Options

    profileController_userLoaded_handler is not firing, while profileController_render_before does. However changing $sender->User->Photo or $sender->User->PhotoUrl did not update the img source on the "Image to be crop" & "thumbnail view".

    Tomorrow I'll try the preg_replace approach, soon as I can find picture.php from other themes.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    Not fired? The line $this->getUserInfo($userReference, $username, $userID, true); calls the function which fires this event. Therefore I'm sure that it will be fired. Just skip the checks for ReqestMethod and that substr test.


    Sure changing the view will help? It is /applications/dashboard/views/profile/picture.php, but it has only this code where you like to see the changes.

    if ($this->data('crop') && $allowImages) {
        echo $this->data('crop');
    } else { ?>
        <div class="avatars">
            <div class="Padded current-avatar">
                <?php echo img($this->data('avatar'), ['style' => 'width: '.c('Garden.Thumbnail.Size').'px; height: '.c('Garden.Thumbnail.Size').'px;']); ?>
            </div>
        </div>
    <?php } ?>
    

    The information comes from ProfileController->picture(). There I can find

            if ($this->isUploadedAvatar($avatar)) {
                // Get the image source so we can manipulate it in the crop module.
                $upload = new Gdn_UploadImage();
                $thumbnailSize = c('Garden.Thumbnail.Size');
                $basename = changeBasename($avatar, "p%s");
                $source = $upload->copyLocal($basename);
    
                // Set up cropping.
                $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
                $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
                $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
                $this->setData('crop', $crop);
    

    So if Gdn_Upload::url is providig you with the correct address, all other things should work again. I know that based on a question from you I had a look at that function and there has been something with a prefixed namespace that was used here. But I do not remember the details...

  • Options

    Sorry @R_J but I still haven't figured out how do I modify the Gdn_Upload::url, that's why on my previous post I opted to manipulate the view by doing preg_replace + regex.

  • Options

    I get it to work by editing the profile/edit.php.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    NO! If that is your solution, I would count it as a failure. You have edited a core file and now a) your plugin isn't deployable and b) your vanilla installation cannot be updated without hassles and c) this community might be unable to support you when you have issues.

    Do you mind to show the changes you made here? Just to see if there is a way to make it work without changing any files.

  • Options

    Sorry for confusion, I did not edited the core profile/edit.php nor the profile/picture, I overwrite it by creating profile/picture.php on our custom theme.

Sign In or Register to comment.