Site logo implementation

This commit is contained in:
Diego Najar 2018-12-21 19:45:53 +01:00
parent 2eea4cd3ba
commit a9372ccce3
4 changed files with 83 additions and 93 deletions

View File

@ -19,6 +19,7 @@
<a class="nav-item nav-link" id="nav-social-tab" data-toggle="tab" href="#social" role="tab" aria-controls="nav-social" aria-selected="false"><?php $L->p('Social Networks') ?></a>
<a class="nav-item nav-link" id="nav-images-tab" data-toggle="tab" href="#images" role="tab" aria-controls="nav-images" aria-selected="false"><?php $L->p('Images') ?></a>
<a class="nav-item nav-link" id="nav-language-tab" data-toggle="tab" href="#language" role="tab" aria-controls="nav-language" aria-selected="false"><?php $L->p('Language') ?></a>
<a class="nav-item nav-link" id="nav-language-tab" data-toggle="tab" href="#logo" role="tab" aria-controls="nav-logo" aria-selected="false"><?php $L->p('Logo') ?></a>
</div>
</nav>
@ -464,4 +465,33 @@
?>
</div>
<!-- Site logo tab -->
<div class="tab-pane" id="logo" role="tabpanel" aria-labelledby="logo-tab">
<div class="custom-file mb-2">
<input type="file" class="custom-file-input" id="jssiteLogoInputFile" name="inputFile">
<label class="custom-file-label" for="jssiteLogoInputFile"><?php $L->p('Choose images to upload'); ?></label>
</div>
<div>
<img id="jssiteLogoPreview" class="img-fluid img-thumbnail" alt="Site logo preview" src="<?php echo (Sanitize::pathFile(PATH_UPLOADS.$site->logo(false))?DOMAIN_UPLOADS.$site->logo(false).'?version='.time():HTML_PATH_ADMIN_THEME_IMG.'default.svg') ?>" />
</div>
<script>
$("#jssiteLogoInputFile").on("change", function() {
var formData = new FormData();
formData.append('tokenCSRF', tokenCSRF);
formData.append('inputFile', $(this)[0].files[0]);
$.ajax({
url: HTML_PATH_ADMIN_ROOT+"ajax/upload-logo",
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false
}).done(function(json) {
console.log(json);
$("#jssiteLogoPreview").attr('src',json.absoluteURL+"?time="+Math.random());
});
});
</script>
</div>
<?php echo Bootstrap::formClose(); ?>

View File

@ -1,92 +0,0 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
// Options allowed for this AJAX
$optionsAllowed = array(
'profilePicture',
'siteLogo',
'favicon'
);
// Image transformation
$transformation = false;
// $_POST
// ----------------------------------------------------------------------------
// (string) $_POST['option']
$option = empty($_POST['option']) ? false : $_POST['option'];
if (!in_array($option, $optionsAllowed)) {
exit (json_encode(array(
'status'=>1,
'message'=>'Error in option parameter.'
)));
}
// (string) $_POST['username']
if ($option=='profilePicture') {
$username = empty($_POST['username']) ? false : $_POST['username'];
if ($username===false) {
exit (json_encode(array(
'status'=>1,
'message'=>'Error in username parameter.'
)));
}
$transformation = array(
'resize'=>true,
'width'=>PROFILE_IMG_WIDTH,
'height'=>PROFILE_IMG_HEIGHT,
'quality'=>PROFILE_IMG_QUALITY,
'forceJPG'=>false,
'forcePNG'=>true,
'option'=>'crop'
);
$finalPath = PATH_UPLOADS_PROFILES;
$finalFilename = $username.'png';
} elseif ($option=='siteLogo') {
$finalPath = PATH_UPLOADS;
$finalFilename = 'logo';
}
// ----------------------------------------------------------------------------
if (!isset($_FILES['inputFile'])) {
exit (json_encode(array(
'status'=>1,
'message'=>'Error trying to upload the file.'
)));
}
// File extension
$fileExtension = pathinfo($_FILES['inputFile']['name'], PATHINFO_EXTENSION);
// Tmp filename
$tmpFilename = 'tmp.'.$fileExtension;
// Move from temporary directory to uploads folder
rename($_FILES['inputFile']['tmp_name'], PATH_TMP.$tmpFilename);
// Resize and crop image
if ($transformation['resize']) {
$image = new Image();
$image->setImage(PATH_TMP.$tmpFilename, $transformation['width'], $transformation['heigh'], $transformation['option']);
$image->saveImage($finalPath.$finalFilename, $transformation['quality'], $transformation['forceJPG'], $transformation['forcePNG']);
} else {
rename(PATH_TMP.$tmpFilename, $finalPath.$finalFilename.'.'.$fileExtension);
}
// Remove the tmp file
unlink(PATH_TMP.$tmpFilename);
// Permissions
chmod($finalPath.$finalFilename, 0644);
exit (json_encode(array(
'status'=>0,
'message'=>'Image uploaded success.',
'filename'=>$finalPath.$finalFilename
)));
?>

View File

@ -0,0 +1,40 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
if (!isset($_FILES['inputFile'])) {
exit (json_encode(array(
'status'=>1,
'message'=>'Error trying to upload the site logo.'
)));
}
// File extension
$fileExtension = pathinfo($_FILES['inputFile']['name'], PATHINFO_EXTENSION);
// Final filename
$filename = 'logo.'.$fileExtension;
// Delete old image
$oldFilename = $site->logo(false);
if ($oldFilename) {
Filesystem::rmfile(PATH_UPLOADS.$oldFilename);
}
// Move from temporary directory to uploads
rename($_FILES['inputFile']['tmp_name'], PATH_UPLOADS.$filename);
// Permissions
chmod(PATH_UPLOADS.$filename, 0644);
// Store the filename in the database
$site->set(array('logo'=>$filename));
exit (json_encode(array(
'status'=>0,
'message'=>'Image uploaded success.',
'filename'=>$filename,
'absoluteURL'=>DOMAIN_UPLOADS.$filename,
'absolutePath'=>PATH_UPLOADS.$filename
)));
?>

View File

@ -42,7 +42,8 @@ class Site extends dbJSON {
'imageRelativeToAbsolute'=> false,
'thumbnailWidth' => 400, // px
'thumbnailHeight' => 400, // px
'thumbnailQuality' => 100
'thumbnailQuality' => 100,
'logo'=> ''
);
function __construct()
@ -268,6 +269,17 @@ class Site extends dbJSON {
return $this->getField('titleFormatTag');
}
// Returns the absolute URL of the site logo
// If you set $absolute=false returns only the filename
public function logo($absolute=true)
{
$logo = $this->getField('logo');
if ($absolute) {
return DOMAIN_UPLOADS.$logo;
}
return $logo;
}
// Returns the full domain and base url
// For example, https://www.domain.com/bludit
public function url()