mirror of
https://github.com/friendica/friendica
synced 2025-04-24 13:10:11 +00:00
Move /profile_photo to Module\Settings\Profile\Photo
This commit is contained in:
parent
2fa873fe06
commit
0d1befdf2d
13 changed files with 426 additions and 421 deletions
203
src/Module/Settings/Profile/Photo/Crop.php
Normal file
203
src/Module/Settings/Profile/Photo/Crop.php
Normal file
|
@ -0,0 +1,203 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Module\Settings\Profile\Photo;
|
||||
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Core\Session;
|
||||
use Friendica\Core\Worker;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Photo;
|
||||
use Friendica\Module\BaseSettingsModule;
|
||||
use Friendica\Network\HTTPException;
|
||||
|
||||
class Crop extends BaseSettingsModule
|
||||
{
|
||||
public static function post(array $parameters = [])
|
||||
{
|
||||
if (!Session::isAuthenticated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$photo_prefix = $parameters['guid'];
|
||||
$resource_id = $photo_prefix;
|
||||
$scale = 0;
|
||||
if (substr($photo_prefix, -2, 1) == '-') {
|
||||
list($resource_id, $scale) = explode('-', $photo_prefix);
|
||||
}
|
||||
|
||||
self::checkFormSecurityTokenRedirectOnError('settings/profile/photo/crop/' . $photo_prefix, 'settings_profile_photo_crop');
|
||||
|
||||
$action = $_POST['action'] ?? 'crop';
|
||||
|
||||
// Image selection origin is top left
|
||||
$selectionX = intval($_POST['xstart'] ?? 0);
|
||||
$selectionY = intval($_POST['ystart'] ?? 0);
|
||||
$selectionW = intval($_POST['width'] ?? 0);
|
||||
$selectionH = intval($_POST['height'] ?? 0);
|
||||
|
||||
$path = 'profile/' . DI::app()->user['nickname'];
|
||||
|
||||
$base_image = Photo::selectFirst([], ['resource-id' => $resource_id, 'uid' => local_user(), 'scale' => $scale]);
|
||||
if (DBA::isResult($base_image)) {
|
||||
$Image = Photo::getImageForPhoto($base_image);
|
||||
if ($Image->isValid()) {
|
||||
// If setting for the default profile, unset the profile photo flag from any other photos I own
|
||||
DBA::update('photo', ['profile' => 0], ['uid' => local_user()]);
|
||||
|
||||
// Normalizing expected square crop parameters
|
||||
$selectionW = $selectionH = min($selectionW, $selectionH);
|
||||
|
||||
$imageIsSquare = $Image->getWidth() === $Image->getHeight();
|
||||
$selectionIsFullImage = $selectionX === 0 && $selectionY === 0 && $selectionW === $Image->getWidth() && $selectionH === $Image->getHeight();
|
||||
|
||||
// Bypassed UI with a rectangle image, we force a square cropped image
|
||||
if (!$imageIsSquare && $action == 'skip') {
|
||||
$selectionX = $selectionY = 0;
|
||||
$selectionW = $selectionH = min($Image->getWidth(), $Image->getHeight());
|
||||
$action = 'crop';
|
||||
}
|
||||
|
||||
// Selective crop if it was asked and the selection isn't the full image
|
||||
if ($action == 'crop'
|
||||
&& !($imageIsSquare && !$selectionIsFullImage)
|
||||
) {
|
||||
$Image->crop(300, $selectionX, $selectionY, $selectionW, $selectionH);
|
||||
$resource_id = Photo::newResource();
|
||||
} else {
|
||||
$Image->scaleDown(300);
|
||||
}
|
||||
|
||||
$r = Photo::store(
|
||||
$Image,
|
||||
local_user(),
|
||||
0,
|
||||
$resource_id,
|
||||
$base_image['filename'],
|
||||
DI::l10n()->t('Profile Photos'),
|
||||
4,
|
||||
1
|
||||
);
|
||||
if ($r === false) {
|
||||
notice(DI::l10n()->t('Image size reduction [%s] failed.', '300'));
|
||||
}
|
||||
|
||||
$Image->scaleDown(80);
|
||||
|
||||
$r = Photo::store(
|
||||
$Image,
|
||||
local_user(),
|
||||
0,
|
||||
$resource_id,
|
||||
$base_image['filename'],
|
||||
DI::l10n()->t('Profile Photos'),
|
||||
5,
|
||||
1
|
||||
);
|
||||
if ($r === false) {
|
||||
notice(DI::l10n()->t('Image size reduction [%s] failed.', '80'));
|
||||
}
|
||||
|
||||
$Image->scaleDown(48);
|
||||
|
||||
$r = Photo::store(
|
||||
$Image,
|
||||
local_user(),
|
||||
0,
|
||||
$resource_id,
|
||||
$base_image['filename'],
|
||||
DI::l10n()->t('Profile Photos'),
|
||||
6,
|
||||
1
|
||||
);
|
||||
if ($r === false) {
|
||||
notice(DI::l10n()->t('Image size reduction [%s] failed.', '48'));
|
||||
}
|
||||
|
||||
Contact::updateSelfFromUserID(local_user(), true);
|
||||
|
||||
info(DI::l10n()->t('Shift-reload the page or clear browser cache if the new photo does not display immediately.'));
|
||||
// Update global directory in background
|
||||
if ($path && strlen(DI::config()->get('system', 'directory'))) {
|
||||
Worker::add(PRIORITY_LOW, 'Directory', DI::baseUrl()->get() . '/' . $path);
|
||||
}
|
||||
|
||||
Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
|
||||
} else {
|
||||
notice(DI::l10n()->t('Unable to process image'));
|
||||
}
|
||||
}
|
||||
|
||||
DI::baseUrl()->redirect($path);
|
||||
}
|
||||
|
||||
public static function content(array $parameters = [])
|
||||
{
|
||||
if (!Session::isAuthenticated()) {
|
||||
throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
|
||||
}
|
||||
|
||||
parent::content();
|
||||
|
||||
$resource_id = $parameters['guid'];
|
||||
|
||||
$photos = Photo::selectToArray([], ['resource-id' => $resource_id, 'uid' => local_user()], ['order' => ['scale' => false]]);
|
||||
if (!DBA::isResult($photos)) {
|
||||
throw new HTTPException\NotFoundException(DI::l10n()->t('Photo not found.'));
|
||||
}
|
||||
|
||||
$havescale = false;
|
||||
$smallest = 0;
|
||||
foreach ($photos as $photo) {
|
||||
$smallest = $photo['scale'] == 1 ? 1 : $smallest;
|
||||
$havescale = $havescale || $photo['scale'] == 5;
|
||||
}
|
||||
|
||||
// set an already uloaded photo as profile photo
|
||||
// if photo is in 'Profile Photos', change it in db
|
||||
if ($photos[0]['album'] == DI::l10n()->t('Profile Photos') && $havescale) {
|
||||
Photo::update(['profile' => false], ['uid' => local_user()]);
|
||||
|
||||
Photo::update(['profile' => true], ['resource-id' => $resource_id, 'uid' => local_user()]);
|
||||
|
||||
Contact::updateSelfFromUserID(local_user(), true);
|
||||
|
||||
// Update global directory in background
|
||||
if (Session::get('my_url') && strlen(DI::config()->get('system', 'directory'))) {
|
||||
Worker::add(PRIORITY_LOW, 'Directory', Session::get('my_url'));
|
||||
}
|
||||
|
||||
notice(DI::l10n()->t('Profile picture successfully updated.'));
|
||||
|
||||
DI::baseUrl()->redirect('profile/' . DI::app()->user['nickname']);
|
||||
}
|
||||
|
||||
$Image = Photo::getImageForPhoto($photos[0]);
|
||||
|
||||
$imagecrop = [
|
||||
'resource-id' => $resource_id,
|
||||
'scale' => $smallest,
|
||||
'ext' => $Image->getExt(),
|
||||
];
|
||||
|
||||
$isSquare = $Image->getWidth() === $Image->getHeight();
|
||||
|
||||
DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/profile/photo/crop_head.tpl'), []);
|
||||
|
||||
$filename = $imagecrop['resource-id'] . '-' . $imagecrop['scale'] . '.' . $imagecrop['ext'];
|
||||
$tpl = Renderer::getMarkupTemplate('settings/profile/photo/crop.tpl');
|
||||
$o = Renderer::replaceMacros($tpl, [
|
||||
'$filename' => $filename,
|
||||
'$resource' => $imagecrop['resource-id'] . '-' . $imagecrop['scale'],
|
||||
'$image_url' => DI::baseUrl() . '/photo/' . $filename,
|
||||
'$title' => DI::l10n()->t('Crop Image'),
|
||||
'$desc' => DI::l10n()->t('Please adjust the image cropping for optimum viewing.'),
|
||||
'$form_security_token' => self::getFormSecurityToken('settings_profile_photo_crop'),
|
||||
'$skip' => $isSquare ? DI::l10n()->t('Use Image As Is') : '',
|
||||
'$crop' => DI::l10n()->t('Crop Image'),
|
||||
]);
|
||||
|
||||
return $o;
|
||||
}
|
||||
}
|
129
src/Module/Settings/Profile/Photo/Index.php
Normal file
129
src/Module/Settings/Profile/Photo/Index.php
Normal file
|
@ -0,0 +1,129 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Module\Settings\Profile\Photo;
|
||||
|
||||
use Friendica\App\Arguments;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Core\Session;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Photo;
|
||||
use Friendica\Module\BaseSettingsModule;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Object\Image;
|
||||
use Friendica\Util\Images;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
class Index extends BaseSettingsModule
|
||||
{
|
||||
public static function post(array $parameters = [])
|
||||
{
|
||||
if (!Session::isAuthenticated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
|
||||
|
||||
if (empty($_FILES['userfile'])) {
|
||||
notice(DI::l10n()->t('Missing uploaded image.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$src = $_FILES['userfile']['tmp_name'];
|
||||
$filename = basename($_FILES['userfile']['name']);
|
||||
$filesize = intval($_FILES['userfile']['size']);
|
||||
$filetype = $_FILES['userfile']['type'];
|
||||
if ($filetype == '') {
|
||||
$filetype = Images::guessType($filename);
|
||||
}
|
||||
|
||||
$maximagesize = DI::config()->get('system', 'maximagesize', 0);
|
||||
|
||||
if ($maximagesize && $filesize > $maximagesize) {
|
||||
notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
|
||||
@unlink($src);
|
||||
return;
|
||||
}
|
||||
|
||||
$imagedata = @file_get_contents($src);
|
||||
$Image = new Image($imagedata, $filetype);
|
||||
|
||||
if (!$Image->isValid()) {
|
||||
notice(DI::l10n()->t('Unable to process image.'));
|
||||
@unlink($src);
|
||||
return;
|
||||
}
|
||||
|
||||
$Image->orient($src);
|
||||
@unlink($src);
|
||||
|
||||
$max_length = DI::config()->get('system', 'max_image_length', 0);
|
||||
if ($max_length > 0) {
|
||||
$Image->scaleDown($max_length);
|
||||
}
|
||||
|
||||
$width = $Image->getWidth();
|
||||
$height = $Image->getHeight();
|
||||
|
||||
if ($width < 175 || $height < 175) {
|
||||
$Image->scaleUp(300);
|
||||
$width = $Image->getWidth();
|
||||
$height = $Image->getHeight();
|
||||
}
|
||||
|
||||
$resource_id = Photo::newResource();
|
||||
|
||||
$filename = '';
|
||||
|
||||
if (Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 0)) {
|
||||
info(DI::l10n()->t('Image uploaded successfully.'));
|
||||
} else {
|
||||
notice(DI::l10n()->t('Image upload failed.'));
|
||||
}
|
||||
|
||||
if ($width > 640 || $height > 640) {
|
||||
$Image->scaleDown(640);
|
||||
if (!Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 1)) {
|
||||
notice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
|
||||
}
|
||||
}
|
||||
|
||||
DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
|
||||
}
|
||||
|
||||
public static function content(array $parameters = [])
|
||||
{
|
||||
if (!Session::isAuthenticated()) {
|
||||
throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
|
||||
}
|
||||
|
||||
parent::content();
|
||||
|
||||
/** @var Arguments $args */
|
||||
$args = DI::args();
|
||||
|
||||
$newuser = $args->get($args->getArgc() - 1) === 'new';
|
||||
|
||||
$contact = Contact::selectFirst(['avatar'], ['uid' => local_user(), 'self' => true]);
|
||||
|
||||
$tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
|
||||
$o = Renderer::replaceMacros($tpl, [
|
||||
'$title' => DI::l10n()->t('Profile Picture Settings'),
|
||||
'$current_picture' => DI::l10n()->t('Current Profile Picture'),
|
||||
'$upload_picture' => DI::l10n()->t('Upload Profile Picture'),
|
||||
'$lbl_upfile' => DI::l10n()->t('Upload Picture:'),
|
||||
'$submit' => DI::l10n()->t('Upload'),
|
||||
'$avatar' => $contact['avatar'],
|
||||
'$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
|
||||
'$select' => sprintf('%s %s',
|
||||
DI::l10n()->t('or'),
|
||||
($newuser) ?
|
||||
'<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
|
||||
: '<a href="' . DI::baseUrl() . '/photos/' . DI::app()->user['nickname'] . '">'
|
||||
. DI::l10n()->t('select a photo from your photo albums') . '</a>'
|
||||
),
|
||||
]);
|
||||
|
||||
return $o;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue