Hi @servingpixels ,
you need custom preHook snippet (loadUserPhoto
in example below), roughly idea is the follows - you need something like the next(there is deletion functionality, please remove unnecessary lines):
<? php
// Get user profile
$ profile = $ modx-> user-> getOne ('Profile');
// Get the value of the photo field
$ pathToPhoto = $ profile-> get ('photo');
// Set the photo field to the current value
$ hook-> setValue ('photo', $ pathToPhoto);
// Request to delete a picture (the global POST array has a delete-photo-btn key with a non-NULL value)
if (isset ($ _ POST ['delete-photo-btn'])) {
// If the photo field is not empty, then ..
if ($ pathToPhoto) {
// Generate the full path to the file (photo)
$ fullPathToPhoto = $ modx-> config ['base_path']. $ pathToPhoto;
// If there is a file (photo), then delete it
if (file_exists ($ fullPathToPhoto))
unlink ($ fullPathToPhoto);
// Set the photo field to empty
$ hook-> setValue ('photo', '');
}
}
// Update request (the global POST array has a login-updprof-btn key with a non-NULL value)
if (isset ($ _ POST ['login-updprof-btn'])) {
// Allowed extensions (jpg, png, jpeg)
$ validExt = array ('jpg', 'png', 'jpeg');
// Directory for storing user photos
$ pathToPhoto = $ modx-> config ['base_path']. 'assets / photouser /';
// User file name
$ nameFile = $ _FILES ['photo'] ['name'];
// Get the extension of the file uploaded by the user in lowercase
$ extFile = mb_strtolower (pathinfo ($ nameFile, PATHINFO_EXTENSION));
// Temporary name with which the received file was saved on the server
$ tmpFile = $ _FILES ['photo'] ['tmp_name'];
// If the file was uploaded via HTTP POST and no errors occurred during the upload process, then ...
if ((is_uploaded_file ($ tmpFile)) &&! ($ _ FILES ['photo'] ['error'])) {
// Checks if the file extension matches a valid one. If all is well, then ...
if (in_array ($ extFile, $ validExt)) {
// Form a name for the file (photo)
$ nameFilePhoto = 'user'. $ modx-> user-> get ('id'). '.' ... $ extFile;
// Get the full file name (photo)
$ fullNameFilePhoto = $ pathToPhoto. $ nameFilePhoto;
// Move the temporary file to a new location $ fullNameFilePhoto. If everything went well, then ...
if (move_uploaded_file ($ tmpFile, $ fullNameFilePhoto)) {
// if the phpthumb.class.php file was not included, then enable it
require_once MODX_CORE_PATH.'model / phpthumb / phpthumb.class.php ';
// Create a new instance of the phpThumb class
$ phpThumb = new phpThumb ();
// Specify the original image
$ phpThumb-> setSourceFilename ($ fullNameFilePhoto);
// Set the width of the image
$ phpThumb-> setParameter ('w', 100);
// Set the height of the image
$ phpThumb-> setParameter ('h', 100);
// Set the trim type
$ phpThumb-> setParameter ('zc', '1');
// Set the image quality
$ phpThumb-> setParameter ('q', '80');
// Generate a thumbnail image. If the action was successful, then ...
if ($ phpThumb-> GenerateThumbnail ()) {
// Save the image to $ fullNameFilePhoto. If this action succeeds, then ..
if ($ phpThumb-> RenderToFile ($ fullNameFilePhoto)) {
// Set the path to the file in the photo field
$ hook-> setValue ('photo', $ modx-> getOption ('assets_url'). 'photouser /'. $ nameFilePhoto);
}
else {
$ modx-> log (modX :: LOG_LEVEL_ERROR, 'Error while saving image to file'. $ fullNameFilePhoto);
}
}
else {
// Write the received error to the MODX log
$ modx-> log (modX :: LOG_LEVEL_ERROR, print_r ($ phpThumb-> debugmessages, 1));
}
}
else {
// Write to the log that an error occurred while moving the file to a new location
$ modx-> log (modX :: LOG_LEVEL_ERROR, 'Error moving temporary file'. $ tmpFile. 'to new location'. $ fullNameFilePhoto);
}
}
else {
// Log a message stating that the file extension does not match the allowed one
$ modx-> log (modX :: LOG_LEVEL_ERROR, 'The image has an invalid extension');
}
}
else {
// Write to the log that an error occurred while uploading the file
$ modx-> log (modX :: LOG_LEVEL_ERROR, 'Error loading file. Error code:'. $ _ FILES ['photo'] ['error']);
}
}
return true;
and UpdateProfile snippet call will be:
[[!UpdateProfile? &validate=`fullname:required` &preHooks=`loadUserPhoto`]]
and html form like the next:
<form action="[[~[[*id]]]]" method="post" class="form-horizontal" enctype="multipart/form-data">
<div class="form-group">
<label for="photo" class="col-sm-4 control-label" style="padding-top: 0px; padding-bottom: 6px;">Avatar</label>
<div id="crop" class="img-rounded center-block" style="height: 100px; width: 100px; overflow: hidden;">
<img id="img-photo" src="[[+photo:default=`/assets/photouser/default.jpg`]]">
</div>
<p class="lead text-center">[[+fullname]]</p>
<hr>
<div class="col-sm-8" style="padding-top: 0px; padding-bottom: 6px;">
<input type="file" id="photo" name="photo" value="[[+photo]]">
</div>
</div>
</form>