Skip to content

Instantly share code, notes, and snippets.

@boboldehampsink
Created November 6, 2013 13:00
Show Gist options
  • Save boboldehampsink/7335716 to your computer and use it in GitHub Desktop.
Save boboldehampsink/7335716 to your computer and use it in GitHub Desktop.
Upload user photos in the front-end. Use <input type="file" name="qqfile" /> to trigger Craft's native upload mechanism.
<?php
namespace Craft;
class UserPhotoPlugin extends BasePlugin
{
function getName()
{
return Craft::t('Upload user photos on the front-end');
}
function getVersion()
{
return '1.0';
}
function getDeveloper()
{
return 'Bob Olde Hampsink';
}
function getDeveloperUrl()
{
return 'http://www.itmundi.nl';
}
function init() {
// Only works on the front-end
if(craft()->request->isSiteRequest()) {
// Only trigger when an user is saved
craft()->on('users.onSaveProfile', function(Event $event) {
// Get user
$user = $event->params['user'];
// Upload the file and drop it in the temporary folder
$uploader = new \qqFileUploader();
// Make sure a file was uploaded
if($uploader->file && $uploader->file->getSize()) {
// Get temp upload path for user
$folderPath = craft()->path->getTempUploadsPath().'userphotos/'.$user->username.'/';
// Clear temp upload path
IOHelper::clearFolder($folderPath);
// Make sure path exists
IOHelper::ensureFolderExists($folderPath);
// Clean uploaded filename
$fileName = IOHelper::cleanFilename($uploader->file->getName());
// Save uploaded file
$uploader->file->save($folderPath.$fileName);
// Test if we will be able to perform image actions on this image
if(!craft()->images->setMemoryForImage($folderPath.$fileName)) {
IOHelper::deleteFile($folderPath.$fileName);
exit(Craft::t('The uploaded image is too large'));
}
craft()->images->cleanImage($folderPath.$fileName);
$constraint = 500;
list ($width, $height) = getimagesize($folderPath.$fileName);
// If the file is in the format badscript.php.gif perhaps.
if($width && $height) {
// Crop from center
$centreX = round($width / 2);
$centreY = round($height / 2);
$cropWidth = $width > $height ? $height : $width;
$cropHeight = $cropWidth;
$cropWidthHalf = round($cropWidth / 2);
$cropHeightHalf = round($cropWidth / 2);
$x1 = max(0, $centreX - $cropWidthHalf);
$y1 = max(0, $centreY - $cropHeightHalf);
$x2 = min($width, $centreX + $cropWidthHalf);
$y2 = min($height, $centreY + $cropHeightHalf);
$source = $fileName;
// Strip off any querystring info, if any.
if(($qIndex = mb_strpos($source, '?')) !== false) {
$source = mb_substr($source, 0, mb_strpos($source, '?'));
}
$imagePath = craft()->path->getTempUploadsPath().'userphotos/'.$user->username.'/'.$source;
// Delete old user photo
craft()->users->deleteUserPhoto($user);
// Crop and save
craft()->users->cropAndSaveUserPhoto($imagePath, $x1, $x2, $y1, $y2, $user);
// Clear temp folder
IOHelper::clearFolder(craft()->path->getTempUploadsPath().'userphotos/'.$user->username);
}
}
});
}
}
}
@lumihq
Copy link

lumihq commented Apr 18, 2014

Tried installing this in Craft 2.0 but the plugin doesn't show up in the CP. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment