mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-06-26 03:30:48 +03:00
Модуль Image: реализована загрузка\удаление аватары, загрузка\удаления фотографии. Добавлена возможность кастомизировать конфигурационные параметры обработки изображений для различных их групп.
This commit is contained in:
parent
54f0c59419
commit
ef8da40a13
|
@ -265,20 +265,14 @@ class ActionSettings extends Action {
|
|||
}
|
||||
/**
|
||||
* Загрузка аватара, делаем ресайзы
|
||||
*/
|
||||
if (isset($_FILES['avatar']) and is_uploaded_file($_FILES['avatar']['tmp_name'])) {
|
||||
$sFileTmp=$_FILES['avatar']['tmp_name'];
|
||||
if ($sFileAvatar=func_img_resize($sFileTmp,Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId(),'avatar_100x100',3000,3000,100,100)) {
|
||||
func_img_resize($sFileTmp,Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId(),'avatar_64x64',3000,3000,64,64);
|
||||
func_img_resize($sFileTmp,Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId(),'avatar_48x48',3000,3000,48,48);
|
||||
func_img_resize($sFileTmp,Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId(),'avatar_24x24',3000,3000,24,24);
|
||||
func_img_resize($sFileTmp,Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId(),'avatar',3000,3000);
|
||||
*/
|
||||
if (isset($_FILES['avatar']) and is_uploaded_file($_FILES['avatar']['tmp_name'])) {
|
||||
if($sExtension=$this->Image_UploadAvatar($_FILES['avatar'],$this->oUserCurrent)) {
|
||||
$this->oUserCurrent->setProfileAvatar(1);
|
||||
$aFileInfo=pathinfo($sFileAvatar);
|
||||
$this->oUserCurrent->setProfileAvatarType($aFileInfo['extension']);
|
||||
$this->oUserCurrent->setProfileAvatarType($sExtension);
|
||||
} else {
|
||||
$bError=true;
|
||||
$this->Message_AddError($this->Lang_Get('settings_profile_avatar_error'),$this->Lang_Get('error'));
|
||||
$this->Message_AddError($this->Lang_Get('settings_profile_avatar_error'),$this->Lang_Get('error'));
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -286,20 +280,14 @@ class ActionSettings extends Action {
|
|||
*/
|
||||
if (isset($_REQUEST['avatar_delete'])) {
|
||||
$this->oUserCurrent->setProfileAvatar(0);
|
||||
@unlink(Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId().'/avatar_100x100.'.$this->oUserCurrent->getProfileAvatarType());
|
||||
@unlink(Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId().'/avatar_64x64.'.$this->oUserCurrent->getProfileAvatarType());
|
||||
@unlink(Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId().'/avatar_48x48.'.$this->oUserCurrent->getProfileAvatarType());
|
||||
@unlink(Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId().'/avatar_24x24.'.$this->oUserCurrent->getProfileAvatarType());
|
||||
@unlink(Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$this->oUserCurrent->getId().'/avatar.'.$this->oUserCurrent->getProfileAvatarType());
|
||||
$this->Image_DeleteAvatar($this->oUserCurrent);
|
||||
}
|
||||
/**
|
||||
* Загрузка фото, делаем ресайзы
|
||||
*/
|
||||
if (isset($_FILES['foto']) and is_uploaded_file($_FILES['foto']['tmp_name'])) {
|
||||
$sDirUpload=Config::Get('path.uploads.images').'/'.func_generator(1).'/'.func_generator(1).'/'.func_generator(1).'/'.func_generator(1).'/'.$this->oUserCurrent->getId();
|
||||
$sFileTmp=$_FILES['foto']['tmp_name'];
|
||||
if ($sFileFoto=func_img_resize($sFileTmp,$sDirUpload,func_generator(6),3000,3000,250)) {
|
||||
$this->oUserCurrent->setProfileFoto($sDirUpload.'/'.$sFileFoto);
|
||||
if (isset($_FILES['foto']) and is_uploaded_file($_FILES['foto']['tmp_name'])) {
|
||||
if ($sFileFoto=$this->Image_UploadFoto($_FILES['foto'],$this->oUserCurrent)) {
|
||||
$this->oUserCurrent->setProfileFoto($sFileFoto);
|
||||
} else {
|
||||
$bError=true;
|
||||
$this->Message_AddError($this->Lang_Get('settings_profile_foto_error'),$this->Lang_Get('error'));
|
||||
|
@ -308,8 +296,8 @@ class ActionSettings extends Action {
|
|||
/**
|
||||
* Удалить фото
|
||||
*/
|
||||
if (isset($_REQUEST['foto_delete'])) {
|
||||
@unlink(Config::Get('path.root.server').$this->oUserCurrent->getProfileFoto());
|
||||
if (isset($_REQUEST['foto_delete'])) {
|
||||
$this->Image_DeleteFoto($this->oUserCurrent);
|
||||
$this->oUserCurrent->setProfileFoto(null);
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -305,22 +305,25 @@ $config['module']['notify']['delayed'] = false; // Указывает н
|
|||
$config['module']['notify']['insert_single'] = false; // Если опция установлена в true, систему будет собирать записи заданий удаленной публикации, для вставки их в базу единым INSERT
|
||||
$config['module']['notify']['per_process'] = 10; // Количество отложенных заданий, обрабатываемых одним крон-процессом
|
||||
// Модуль Image
|
||||
$config['module']['image']['watermark_use'] = false;
|
||||
$config['module']['image']['watermark_type'] = 'text';
|
||||
$config['module']['image']['watermark_position'] = '0,24';
|
||||
$config['module']['image']['watermark_text'] = '(c) LiveStreet';
|
||||
$config['module']['image']['watermark_font'] = 'arial';
|
||||
$config['module']['image']['watermark_font_color'] = '255,255,255';
|
||||
$config['module']['image']['watermark_font_size'] = '10';
|
||||
$config['module']['image']['watermark_font_alfa'] = '0';
|
||||
$config['module']['image']['watermark_back_color'] = '0,0,0';
|
||||
$config['module']['image']['watermark_back_alfa'] = '40';
|
||||
$config['module']['image']['watermark_image'] = false;
|
||||
$config['module']['image']['round_corner'] = false;
|
||||
$config['module']['image']['round_corner_radius'] = '18';
|
||||
$config['module']['image']['round_corner_rate'] = '40';
|
||||
$config['module']['image']['path']['watermarks'] = $config['path']['root']['server'].'/engine/lib/external/LiveImage/watermarks/';
|
||||
$config['module']['image']['path']['fonts'] = $config['path']['root']['server'].'/engine/lib/external/LiveImage/fonts/';
|
||||
$config['module']['image']['default']['watermark_use'] = false;
|
||||
$config['module']['image']['default']['watermark_type'] = 'text';
|
||||
$config['module']['image']['default']['watermark_position'] = '0,24';
|
||||
$config['module']['image']['default']['watermark_text'] = '(c) LiveStreet';
|
||||
$config['module']['image']['default']['watermark_font'] = 'arial';
|
||||
$config['module']['image']['default']['watermark_font_color'] = '255,255,255';
|
||||
$config['module']['image']['default']['watermark_font_size'] = '10';
|
||||
$config['module']['image']['default']['watermark_font_alfa'] = '0';
|
||||
$config['module']['image']['default']['watermark_back_color'] = '0,0,0';
|
||||
$config['module']['image']['default']['watermark_back_alfa'] = '40';
|
||||
$config['module']['image']['default']['watermark_image'] = false;
|
||||
$config['module']['image']['default']['round_corner'] = false;
|
||||
$config['module']['image']['default']['round_corner_radius'] = '18';
|
||||
$config['module']['image']['default']['round_corner_rate'] = '40';
|
||||
$config['module']['image']['default']['path']['watermarks'] = $config['path']['root']['server'].'/engine/lib/external/LiveImage/watermarks/';
|
||||
$config['module']['image']['default']['path']['fonts'] = $config['path']['root']['server'].'/engine/lib/external/LiveImage/fonts/';
|
||||
|
||||
$config['module']['image']['foto']['watermark_use'] = true;
|
||||
$config['module']['image']['foto']['round_corner'] = true;
|
||||
|
||||
// Какие модули должны быть загружены на старте
|
||||
$config['module']['autoLoad'] = array('Cache','Session','User', 'Lang', 'Message');
|
||||
|
|
|
@ -300,33 +300,33 @@ function func_img_resize($sFileSrc,$sDirDest,$sFileDest,$iWidthMax,$iHeightMax,$
|
|||
* Если не задана новая высота, то применяем масштабирование.
|
||||
* Если нужно добавить Watermark, то запрещаем ручное управление alfa-каналом
|
||||
*/
|
||||
$oImage->resize($iWidthDest,$iHeightDest,(!$iHeightDest),(!Config::Get('module.image.watermark_use')));
|
||||
$oImage->resize($iWidthDest,$iHeightDest,(!$iHeightDest),(!Config::Get('module.image.default.watermark_use')));
|
||||
|
||||
/**
|
||||
* Добавляем watermark согласно в конфигурации заданым параметрам
|
||||
*/
|
||||
if(Config::Get('module.image.watermark_use')) {
|
||||
switch(Config::Get('module.image.watermark_type')) {
|
||||
if(Config::Get('module.image.default.default.watermark_use')) {
|
||||
switch(Config::Get('module.image.default.watermark_type')) {
|
||||
default:
|
||||
case 'text':
|
||||
$oImage->set_font(
|
||||
Config::Get('module.image.watermark_font_size'), 0,
|
||||
Config::Get('module.image.path.fonts').Config::Get('module.image.watermark_font').'.ttf'
|
||||
Config::Get('module.image.default.watermark_font_size'), 0,
|
||||
Config::Get('module.image.default.path.fonts').Config::Get('module.image.default.watermark_font').'.ttf'
|
||||
);
|
||||
|
||||
$oImage->watermark(
|
||||
Config::Get('module.image.watermark_text'),
|
||||
explode(',',Config::Get('module.image.watermark_position'),2),
|
||||
explode(',',Config::Get('module.image.watermark_font_color')),
|
||||
explode(',',Config::Get('module.image.watermark_back_color')),
|
||||
Config::Get('module.image.watermark_font_alfa'),
|
||||
Config::Get('module.image.watermark_back_alfa')
|
||||
Config::Get('module.image.default.watermark_text'),
|
||||
explode(',',Config::Get('module.image.default.watermark_position'),2),
|
||||
explode(',',Config::Get('module.image.default.watermark_font_color')),
|
||||
explode(',',Config::Get('module.image.default.watermark_back_color')),
|
||||
Config::Get('module.image.default.watermark_font_alfa'),
|
||||
Config::Get('module.image.default.watermark_back_alfa')
|
||||
);
|
||||
break;
|
||||
case 'image':
|
||||
$oImage->paste_image(
|
||||
Config::Get('module.image.path.watermarks').Config::Get('module.image.watermark_image'),
|
||||
true, explode(',',Config::Get('module.image.watermark_position'),2)
|
||||
Config::Get('module.image.default.path.watermarks').Config::Get('module.image.default.watermark_image'),
|
||||
true, explode(',',Config::Get('module.image.default.watermark_position'),2)
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
@ -335,10 +335,10 @@ function func_img_resize($sFileSrc,$sDirDest,$sFileDest,$iWidthMax,$iHeightMax,$
|
|||
/**
|
||||
* Скругляем углы
|
||||
*/
|
||||
if(Config::Get('module.image.round_corner')) {
|
||||
if(Config::Get('module.image.default.round_corner')) {
|
||||
$oImage->round_corners(
|
||||
Config::Get('module.image.round_corner_radius'),
|
||||
Config::Get('module.image.round_corner_rate')
|
||||
Config::Get('module.image.default.round_corner_radius'),
|
||||
Config::Get('module.image.default.round_corner_rate')
|
||||
);
|
||||
}
|
||||
|
||||
|
|
240
engine/modules/image/Image.class.php
Normal file
240
engine/modules/image/Image.class.php
Normal file
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
/*-------------------------------------------------------
|
||||
*
|
||||
* LiveStreet Engine Social Networking
|
||||
* Copyright © 2008 Mzhelskiy Maxim
|
||||
*
|
||||
*--------------------------------------------------------
|
||||
*
|
||||
* Official site: www.livestreet.ru
|
||||
* Contact e-mail: rus.engine@gmail.com
|
||||
*
|
||||
* GNU General Public License, version 2:
|
||||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*
|
||||
---------------------------------------------------------
|
||||
*/
|
||||
|
||||
require_once Config::Get('path.root.engine').'/lib/external/LiveImage/Image.php';
|
||||
|
||||
/**
|
||||
* Модуль обработки изображений
|
||||
* Использует библиотеку LiveImage
|
||||
*
|
||||
*/
|
||||
class LsImage extends Module {
|
||||
protected $aParamsDefault = array();
|
||||
/**
|
||||
* Инициализация модуля
|
||||
*/
|
||||
public function Init() {
|
||||
$this->aParamsDefault = array(
|
||||
'watermark_use'=>false,
|
||||
'round_corner' =>false
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Merge default and named params for images
|
||||
*
|
||||
* @param string $sName
|
||||
* @return array
|
||||
*/
|
||||
protected function BuildParams($sName=null) {
|
||||
if(is_null($sName)) {
|
||||
return Config::Get('module.image.default');
|
||||
}
|
||||
|
||||
$aDefault = (array)Config::Get('module.image.default');
|
||||
$aNamed = (array)Config::Get('module.image.'.strtolower($sName));
|
||||
|
||||
return func_array_merge_assoc($aDefault,$aNamed);
|
||||
}
|
||||
/**
|
||||
* Resize,copy image,
|
||||
* make rounded corners and add watermark
|
||||
*
|
||||
* @param unknown_type $sFileSrc
|
||||
* @param unknown_type $sDirDest
|
||||
* @param unknown_type $sFileDest
|
||||
* @param unknown_type $iWidthMax
|
||||
* @param unknown_type $iHeightMax
|
||||
* @param unknown_type $iWidthDest
|
||||
* @param unknown_type $iHeightDest
|
||||
* @param unknown_type $bForcedMinSize
|
||||
* @param unknown_type $aParams
|
||||
* @return unknown
|
||||
*/
|
||||
public function Resize($sFileSrc,$sDirDest,$sFileDest,$iWidthMax,$iHeightMax,$iWidthDest=null,$iHeightDest=null,$bForcedMinSize=true,$aParams=null) {
|
||||
/**
|
||||
* Если параметры не переданы, устанавливаем действия по умолчанию
|
||||
*/
|
||||
if(!is_array($aParams)) {
|
||||
$aParams=$this->aParamsDefault;
|
||||
}
|
||||
|
||||
$oImage=new LiveImage($sFileSrc);
|
||||
|
||||
if($oImage->get_last_error()){
|
||||
return false;
|
||||
}
|
||||
$sFileDest.='.'.$oImage->get_image_params('format');
|
||||
if (($oImage->get_image_params('width')>$iWidthMax)
|
||||
or ($oImage->get_image_params('height')>$iHeightMax)) {
|
||||
return false;
|
||||
}
|
||||
$sFileFullPath=Config::Get('path.root.server').'/'.$sDirDest.'/'.$sFileDest;
|
||||
@func_mkdir(Config::Get('path.root.server'),$sDirDest);
|
||||
|
||||
if ($iWidthDest) {
|
||||
if (!$bForcedMinSize and ($iWidthDest>$oImage->get_image_params('width'))) {
|
||||
$iWidthDest=$oImage->get_image_params('width');
|
||||
}
|
||||
/**
|
||||
* Ресайзим и выводим результат в файл.
|
||||
* Если не задана новая высота, то применяем масштабирование.
|
||||
* Если нужно добавить Watermark, то запрещаем ручное управление alfa-каналом
|
||||
*/
|
||||
$oImage->resize($iWidthDest,$iHeightDest,(!$iHeightDest),(!Config::Get('module.image.watermark_use')));
|
||||
|
||||
/**
|
||||
* Добавляем watermark согласно в конфигурации заданым параметрам
|
||||
*/
|
||||
if($aParams['watermark_use']) {
|
||||
switch($aParams['watermark_type']) {
|
||||
default:
|
||||
case 'text':
|
||||
$oImage->set_font(
|
||||
$aParams['watermark_font_size'], 0,
|
||||
$aParams['path']['fonts'].$aParams['watermark_font'].'.ttf'
|
||||
);
|
||||
|
||||
$oImage->watermark(
|
||||
$aParams['watermark_text'],
|
||||
explode(',',$aParams['watermark_position'],2),
|
||||
explode(',',$aParams['watermark_font_color']),
|
||||
explode(',',$aParams['watermark_back_color']),
|
||||
$aParams['watermark_font_alfa'],
|
||||
$aParams['watermark_back_alfa']
|
||||
);
|
||||
break;
|
||||
case 'image':
|
||||
$oImage->paste_image(
|
||||
$aParams['path']['watermarks'].$aParams['watermark_image'],
|
||||
true, explode(',',$aParams['watermark_position'],2)
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Скругляем углы
|
||||
*/
|
||||
if($aParams['round_corner']) {
|
||||
$oImage->round_corners(
|
||||
$aParams['round_corner_radius'],
|
||||
$aParams['round_corner_rate']
|
||||
);
|
||||
}
|
||||
|
||||
$oImage->output(null,$sFileFullPath);
|
||||
|
||||
chmod($sFileFullPath,0666);
|
||||
return $sFileDest;
|
||||
} elseif (copy($sFileSrc,$sFileFullPath)) {
|
||||
chmod($sFileFullPath,0666);
|
||||
return $sFileDest;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Upload user avatar on server
|
||||
* Make resized images
|
||||
*
|
||||
* @param array $aFile
|
||||
* @param UserEntity_User $oUser
|
||||
* @return (string|bool)
|
||||
*/
|
||||
public function UploadAvatar($aFile,$oUser) {
|
||||
if(!is_array($aFile) || !isset($aFile['tmp_name'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sFileTmp=$aFile['tmp_name'];
|
||||
$sPath=Config::Get('path.uploads.images').'/'.$oUser->getId();
|
||||
$aParams=$this->BuildParams('avatar');
|
||||
|
||||
if ($sFileAvatar=$this->Resize($sFileTmp,$sPath,'avatar_100x100',3000,3000,100,100,true,$aParams)) {
|
||||
$this->Resize($sFileTmp,$sPath,'avatar_64x64',3000,3000,64,64,true,$aParams);
|
||||
$this->Resize($sFileTmp,$sPath,'avatar_48x48',3000,3000,48,48,true,$aParams);
|
||||
$this->Resize($sFileTmp,$sPath,'avatar_24x24',3000,3000,24,24,true,$aParams);
|
||||
$this->Resize($sFileTmp,$sPath,'avatar',3000,3000,true,$aParams);
|
||||
|
||||
/**
|
||||
* Если все нормально, возвращаем расширение загруженного аватара
|
||||
*/
|
||||
$aFileInfo=pathinfo($sFileAvatar);
|
||||
return $aFileInfo['extension'];
|
||||
}
|
||||
/**
|
||||
* В случае ошибки, возвращаем false
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Delete avatar from server
|
||||
*
|
||||
* @param UserEntity_User $oUser
|
||||
*/
|
||||
public function DeleteAvatar($oUser) {
|
||||
$sPath = Config::Get('path.root.server').Config::Get('path.uploads.images').'/'.$oUser->getId();
|
||||
/**
|
||||
* Удаляем аватар и его рейсайзы
|
||||
*/
|
||||
@unlink($sPath.'/avatar_100x100.'.$oUser->getProfileAvatarType());
|
||||
@unlink($sPath.'/avatar_64x64.'.$oUser->getProfileAvatarType());
|
||||
@unlink($sPath.'/avatar_48x48.'.$oUser->getProfileAvatarType());
|
||||
@unlink($sPath.'/avatar_24x24.'.$oUser->getProfileAvatarType());
|
||||
@unlink($sPath.'/avatar.'.$oUser->getProfileAvatarType());
|
||||
}
|
||||
/**
|
||||
* Upload user foto
|
||||
*
|
||||
* @param array $aFile
|
||||
* @param UserEntity_User $oUser
|
||||
* @return string
|
||||
*/
|
||||
public function UploadFoto($aFile,$oUser) {
|
||||
if(!is_array($aFile) || !isset($aFile['tmp_name'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sDirUpload=Config::Get('path.uploads.images').'/'.func_generator(1).'/'.func_generator(1).'/'.func_generator(1).'/'.func_generator(1).'/'.$oUser->getId();
|
||||
$sFileTmp=$aFile['tmp_name'];
|
||||
$aParams=$this->BuildParams('foto');
|
||||
|
||||
if ($sFileFoto=$this->Resize($sFileTmp,$sDirUpload,func_generator(6),3000,3000,250,null,true,$aParams)) {
|
||||
/**
|
||||
* удаляем старое фото
|
||||
*/
|
||||
$this->DeleteFoto($oUser);
|
||||
return $sDirUpload.'/'.$sFileFoto;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Delete user foto from server
|
||||
*
|
||||
* @param UserEntity_User $oUser
|
||||
*/
|
||||
public function DeleteFoto($oUser) {
|
||||
@unlink(Config::Get('path.root.server').$oUser->getProfileFoto());
|
||||
}
|
||||
|
||||
/**
|
||||
* Завершение работы модуля
|
||||
*/
|
||||
public function Shutdown() {
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue