[英]Add avatar image to the joomla profiles - joomla 3.5.1
我正在嘗試通過遵循以下步驟將Avatar照片添加到jommla配置文件中1-我的服務器選項Windows上的PHP構建Windows NT ******* 6.1內部版本7600(Windows 7家庭高級版)i586數據庫版本5.5.5-10.1 .10-MariaDB數據庫排序規則utf8_general_ci數據庫連接排序規則utf8mb4_general_ci PHP版本5.6.19 Web服務器Apache / 2.4.17(Win32)OpenSSL / 1.0.2d PHP / 5.6.19 Web服務器到PHP的接口apache2handler Joomla! 版本Joomla! 3.5.1穩定[獨角獸] 2016年4月5日格林尼治標准時間Joomla! 平台版本Joomla平台13.1.0穩定[好奇心] 2013年4月24日00:00 GMT用戶代理Mozilla / 5.0(Windows NT 6.1)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 51.0.2704.79 Safari / 537.36
2-相關的PHP設置
安全模式關閉打開basedir無顯示錯誤關閉短打開標簽關閉文件上傳打開魔術引號關閉寄存器全局關閉輸出緩沖關閉會話保存路徑C:\\ xampp \\ tmp會話自動啟動0 XML啟用是Zlib啟用是本機ZIP啟用是禁用功能無多字節字符串(mbstring)啟用是Iconv可用是最大輸入變量1000
3-文件和字段編輯
我在文件中添加了文件上傳字段
joomla_path / plugins / user / profile / profile.xml
我將此文件添加到表單開頭的后端的joomla用戶配置文件插件中
<field name="register-require_avatar"
type="list"
description="PLG_USER_PROFILE_FIELD_AVATAR_DESC"
label="PLG_USER_PROFILE_FIELD_AVATAR_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
我將(multipart / form-data)添加到窗體。 我在文件中添加了文件上傳字段
joomla_path / plugins / user / profile / profiles / profile.xml我將此文件添加到表單頭部后端的joomla用戶配置文件插件中
<field
name="avatar"
type="file"
id="avatar"
description="PLG_USER_PROFILE_FIELD_AVATAR_DESC"
label="PLG_USER_PROFILE_FIELD_AVATAR_LABEL"
/>
在路徑中的文件profile.php中:joomla_path / plugins / user / profile / profile.php
我添加了此代碼來創建用戶頭像文件夾,並添加了上傳圖像文件的代碼(我在“ onUserBeforeSave”函數中添加了此代碼)
import joomla's filesystem classes
jimport('joomla.filesystem.folder');
// Get the user info
$user = JFactory::getUser();
//The right way to create user folder and avatar folder
$path = JPATH_SITE . '/images/users/'. $user->id . '-' . $user->username . '/avatar';
JFolder::create($path);
jimport('joomla.filesystem.file');
$file = JRequest::getVar('jform', null, 'files', 'array');
if (isset($file) && $file['size']['profile']['avatar'] != '') {
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//First check if the file has the right extension, we need jpg only
if ($file['type']['profile']['avatar'] == 'image/jpeg' || $file['type']['profile']['avatar'] == 'image/png' || $file['type']['profile']['avatar'] == 'image/gif') {
if ($file['type']['profile']['avatar'] == 'image/png') {
$image = imagecreatefrompng($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . '/avatar' . $filename .'.jpg', 80);
imagedestroy($image);
$image = JUri::root() . '/images/users/'. $user->id . '-' . $user->username . '/avatar/' . $filename;
} else if ($file['type']['profile']['avatar'] == 'image/jpeg') {
$image = imagecreatefromjpeg($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . '/avatar' . $filename .'.jpg', 80);
imagedestroy($image);
} else if ($file['type']['profile']['avatar'] == 'image/gif') {
$image = imagecreatefromgif($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . '/avatar' . $filename . '.jpg', 80);
imagedestroy($image);
}
} else {
//Redirect and notify user file is not right extension
throw new Exception(JText::_('PLG_USER_FILE_TYPE_INVALID'));
return false;
}
return true;
}
/////////////////////////////////////////////////
******問題是我無法渲染數據庫中的字段來保存頭像文件路徑****必須知道我可以創建用戶的頭像文件夾,並且可以將圖像文件上傳到該路徑,但是問題是是如何在數據庫中保存文件圖像的路徑,然后我可以通過鏈接到此路徑在任何頁面中調用它,因此如何正確執行此操作?
您可以創建一個將圖像插入數據庫的功能
function insertAvatar($userid, $filename, $ext){
// Get a db connection.
$db = JFactory::getDbo();
try {
// Create a new query object.
$query = $db->getQuery(true);
// Insert columns.
$columns = array('user_id', 'profile_key', 'profile_value', 'ordering');
// Insert values.
$values = array($userid,'profile.avatar', $filename.$ext, 1);
// Prepare the insert query.
$query
->insert($db->quoteName('#__user_profiles'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
return $db->execute();
}
catch (Exception $e){
return $e->getMessage();
}
}
在適當的地方調用該函數
if ($file['type']['profile']['avatar'] == 'image/png') {
$this->insertAvatar($user->id, $filename, 'png');
對於其余三種類型(即“ jpg”和“ gif”)的情況類似
$this->insertAvatar($user->id, $filename, 'jpg');
和
$this->insertAvatar($user->id, $filename, 'gif');
。 您可以像調用圖片文件名一樣調用其他配置文件參數。
這是您為我創建的功能
function insertAvatar($userid, $filename, $ext){
// Get a db connection.
$db = JFactory::getDbo();
try {
// Create a new query object.
$query = $db->getQuery(true);
// Insert columns.
$columns = array('user_id', 'profile_key', 'profile_value', 'ordering');
// Insert values.
$values = array($userid,'profile.avatar', $filename.$ext, 1);
// Prepare the insert query.
$query
->insert($db->quoteName('#__user_profiles'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
return $db->execute();
}
catch (Exception $e){
return $e->getMessage();
}
}
這是我放代碼的函數(onUserBeforeSave)
public function onUserBeforeSave($user, $isnew, $data)
{
/////////////////////////////////////////////////////
// import joomla's filesystem classes
jimport('joomla.filesystem.folder');
// Get the user info
$user = JFactory::getUser();
//The right way to create user folder and avatar folder
$path = JPATH_SITE . '/images/users/'. $user->id . '-' . $user->username . '/avatar/';
JFolder::create($path);
jimport('joomla.filesystem.file');
$file = JRequest::getVar('jform', null, 'files', 'array');
if (isset($file) && $file['size']['profile']['avatar'] != 0) {
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//First check if the file has the right extension, we need jpg only
if ($file['type']['profile']['avatar'] == 'image/jpeg' || $file['type']['profile']['avatar'] == 'image/png' || $file['type']['profile']['avatar'] == 'image/gif') {
if ($file['type']['profile']['avatar'] == 'image/png') {
$image = imagecreatefrompng($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . $filename .'.jpg', 80);
$this->insertAvatar($user->id, $filename, 'jpg');
imagedestroy($image);
} else if ($file['type']['profile']['avatar'] == 'image/jpeg') {
$image = imagecreatefromjpeg($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . $filename .'.jpg', 80);
$this->insertAvatar($user->id, $filename, 'jpg');
imagedestroy($image);
} else if ($file['type']['profile']['avatar'] == 'image/gif') {
$image = imagecreatefromgif($file['tmp_name']['profile']['avatar']);
imagejpeg($image, $path . $filename . '.jpg', 80);
$this->insertAvatar($user->id, $filename, 'jpg');
imagedestroy($image);
}
} else {
//Redirect and notify user file is not right extension
throw new Exception(JText::_('PLG_USER_FILE_TYPE_INVALID'));
return false;
}
return true;
}
/////////////////////////////////////////////////
// Check that the date is valid.
if (!empty($data['profile']['dob']))
{
try
{
$date = new JDate($data['profile']['dob']);
$this->date = $date->format('Y-m-d H:i:s');
}
catch (Exception $e)
{
// Throw an exception if date is not valid.
throw new InvalidArgumentException(JText::_('PLG_USER_PROFILE_ERROR_INVALID_DOB'));
}
if (JDate::getInstance('now') < $date)
{
// Throw an exception if dob is greather than now.
throw new InvalidArgumentException(JText::_('PLG_USER_PROFILE_ERROR_INVALID_DOB'));
}
}
// Check that the tos is checked if required ie only in registration from frontend.
$task = JFactory::getApplication()->input->getCmd('task');
$option = JFactory::getApplication()->input->getCmd('option');
$tosarticle = $this->params->get('register_tos_article');
$tosenabled = ($this->params->get('register-require_tos', 0) == 2) ? true : false;
if (($task == 'register') && ($tosenabled) && ($tosarticle) && ($option == 'com_user'))
{
// Check that the tos is checked.
if ((!($data['profile']['tos'])))
{
throw new InvalidArgumentException(JText::_('PLG_USER_PROFILE_FIELD_TOS_DESC_SITE'));
}
}
return true;
}
我的代碼位於注釋行之間///////////////////////////////////////
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.