Método para trocar avatar de usuário (Jom Social)

Categoria: Joomla
Publicado em 16 de Fevereiro de 2011

Para trocar o avatar do usuário logado (sistema Jom Social), usar o seguinte código:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    // Atualiza o avatar do Jom Social para o usuário atualmente logado. A localização da imagem deve ser informada por $pathImagem
    public static function jsAtualizarAvatar( $pathImagem = '' ) {
 
        require_once(JPATH_SITE . '/components/com_community/helpers/image.php');
 
        $my = CFactory::getUser();
 
        $userid	= $my->id;
 
        $size = getimagesize( $pathImagem );
        $file['tmp_name'] = $pathImagem;
        $file['type'] = $size['mime'];
 
 
        $config = CFactory::getConfig();
        $uploadLimit = (double) $config->get('maxuploadsize');
        $uploadLimit = ( $uploadLimit * 1024 * 1024 );
 
        // @rule: Limit image size based on the maximum upload allowed.
        if (filesize($file['tmp_name']) > $uploadLimit && $uploadLimit != 0) {
            return false;
        }
 
        if (!CImageHelper::isValidType($file['type'])) {
            $mainframe->enqueueMessage(JText::_('CC IMAGE FILE NOT SUPPORTED'), 'error');
 
            return false;
        }
 
        if (!CImageHelper::isValid($file['tmp_name'])) {
            $mainframe->enqueueMessage(JText::_('CC IMAGE FILE NOT SUPPORTED'), 'error');
 
            return false;
        } else {
            // @todo: configurable width?
            $imageMaxWidth = 160;
 
            // Get a hash for the file name.
            $profileType = $my->getProfileType();
            $fileName = JUtility::getHash($file['tmp_name'] . time());
            $hashFileName = JString::substr($fileName, 0, 24);
            $multiprofile = & JTable::getInstance('MultiProfile', 'CTable');
            $multiprofile->load($profileType);
 
            $useWatermark = $profileType != COMMUNITY_DEFAULT_PROFILE && $config->get('profile_multiprofile') && !empty($multiprofile->watermark) ? true : false;
            //@todo: configurable path for avatar storage?
 
            $storage = JPATH_ROOT . DS . $config->getString('imagefolder') . DS . 'avatar';
            $storageImage = $storage . DS . $hashFileName . CImageHelper::getExtension($file['type']);
            $storageThumbnail = $storage . DS . 'thumb_' . $hashFileName . CImageHelper::getExtension($file['type']);
            $image = $config->getString('imagefolder') . '/avatar/' . $hashFileName . CImageHelper::getExtension($file['type']);
            $thumbnail = $config->getString('imagefolder') . '/avatar/' . 'thumb_' . $hashFileName . CImageHelper::getExtension($file['type']);
 
            $userModel = CFactory::getModel('user');
 
 
            // Only resize when the width exceeds the max.
            if (!CImageHelper::resizeProportional($file['tmp_name'], $storageImage, $file['type'], $imageMaxWidth)) {
                $mainframe->enqueueMessage(JText::sprintf('CC ERROR MOVING UPLOADED FILE', $storageImage), 'error');
 
                return false;
            }
 
            // Generate thumbnail
            if (!CImageHelper::createThumb($file['tmp_name'], $storageThumbnail, $file['type'])) {
                $mainframe->enqueueMessage(JText::sprintf('CC ERROR MOVING UPLOADED FILE', $storageThumbnail), 'error');
 
                return false;
            }
 
            if ($useWatermark) {
                // @rule: Before adding the watermark, we should copy the user's original image so that when the admin tries to reset the avatar,
                // it will be able to grab the original picture.
                JFile::copy($storageImage, JPATH_ROOT . DS . 'images' . DS . 'watermarks' . DS . 'original' . DS . md5($my->id . '_avatar') . CImageHelper::getExtension($file['type']));
                JFile::copy($storageThumbnail, JPATH_ROOT . DS . 'images' . DS . 'watermarks' . DS . 'original' . DS . md5($my->id . '_thumb') . CImageHelper::getExtension($file['type']));
 
                $watermarkPath = JPATH_ROOT . DS . JString::str_ireplace('/', DS, $multiprofile->watermark);
 
                list( $watermarkWidth, $watermarkHeight ) = getimagesize($watermarkPath);
                list( $avatarWidth, $avatarHeight ) = getimagesize($storageImage);
                list( $thumbWidth, $thumbHeight ) = getimagesize($storageThumbnail);
 
                $watermarkImage = $storageImage;
                $watermarkThumbnail = $storageThumbnail;
 
                // Avatar Properties
                $avatarPosition = CImageHelper::getPositions($multiprofile->watermark_location, $avatarWidth, $avatarHeight, $watermarkWidth, $watermarkHeight);
 
                // The original image file will be removed from the system once it generates a new watermark image.
                CImageHelper::addWatermark($storageImage, $watermarkImage, 'image/jpg', $watermarkPath, $avatarPosition->x, $avatarPosition->y);
 
                //Thumbnail Properties
                $thumbPosition = CImageHelper::getPositions($multiprofile->watermark_location, $thumbWidth, $thumbHeight, $watermarkWidth, $watermarkHeight);
 
                // The original thumbnail file will be removed from the system once it generates a new watermark image.
                CImageHelper::addWatermark($storageThumbnail, $watermarkThumbnail, 'image/jpg', $watermarkPath, $thumbPosition->x, $thumbPosition->y);
 
                $my->set('_watermark_hash', $multiprofile->watermark_hash);
                $my->save();
            }
 
            $userModel->setImage($userid, $image, 'avatar');
            $userModel->setImage($userid, $thumbnail, 'thumb');
 
            // Update the user object so that the profile picture gets updated.
            $my->set('_avatar', $image);
            $my->set('_thumb', $thumbnail);
 
            // @rule: once user changes their profile picture, storage method should always be file.
            $my->set('_storage', 'file');
 
        }
 
 
    }

 

 

 

 

Copyright © Fernando Hidemi Uchiyama 2010 - Todos os direitos reservados