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

De Basef
Revisão de 14h13min de 9 de setembro de 2015 por Admin (discussão | contribs) (Criou página com 'Usar função abaixo: <source lang="php"> public static function jsAtualizarAvatar( $pathImagem = '' ) { require_once(JPATH_SITE . '/components/com_community/help...')

(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)
Ir para: navegação, pesquisa

Usar função abaixo:

    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');
 
        }
    }