public function updateAvatar()
{
if (!$this->fileName)
{
return $this->throwException(new \LogicException("No source file for avatar set"));
}
if (!$this->user->exists())
{
return $this->throwException(new \LogicException("User does not exist, cannot update avatar"));
}
$imageManager = $this->app->imageManager();
$outputFiles = [];
$baseFile = $this->fileName;
$origSize = $this->sizeMap['o'];
$shortSide = min($this->width, $this->height);
if ($shortSide > $origSize)
{
$image = $imageManager->imageFromFile($this->fileName);
if (!$image)
{
return false;
}
$image->resizeShortEdge($origSize);
$newTempFile = \XF\Util\File::getTempFile();
if ($newTempFile && $image->save($newTempFile, null, 95))
{
$outputFiles['o'] = $newTempFile;
$baseFile = $newTempFile;
$width = $image->getWidth();
$height = $image->getHeight();
}
else
{
return $this->throwException(new \RuntimeException("Failed to save image to temporary file; check internal_data/data permissions"));
}
unset($image);
}
else
{
$outputFiles['o'] = $this->fileName;
$width = $this->width;
$height = $this->height;
}
$crop = [
'm' => [0, 0]
];
foreach ($this->sizeMap AS $code => $size)
{
if (isset($outputFiles[$code]))
{
continue;
}
$image = $imageManager->imageFromFile($baseFile);
if (!$image)
{
continue;
}
$crop[$code] = $this->resizeAvatarImage($image, $size);
$newTempFile = \XF\Util\File::getTempFile();
if ($newTempFile && $image->save($newTempFile))
{
$outputFiles[$code] = $newTempFile;
}
unset($image);
}
if (count($outputFiles) != count($this->sizeMap))
{
return $this->throwException(new \RuntimeException("Failed to save image to temporary file; image may be corrupt or check internal_data/data permissions"));
}
foreach ($outputFiles AS $code => $file)
{
$dataFile = $this->user->getAbstractedCustomAvatarPath($code);
\XF\Util\File::copyFileToAbstractedPath($file, $dataFile);
}
$user = $this->user;
$user->bulkSet([
'avatar_date' => \XF::$time,
'avatar_width' => $width,
'avatar_height' => $height,
'avatar_highdpi' => ($width >= self::HIGH_DPI_THRESHOLD && $height >= self::HIGH_DPI_THRESHOLD),
'gravatar' => ''
]);
$profile = $user->getRelationOrDefault('Profile');
$profile->bulkSet([
'avatar_crop_x' => $crop['m'][0],
'avatar_crop_y' => $crop['m'][1]
]);
$user->addCascadedSave($profile);
if ($this->logChange == false)
{
$user->getBehavior('XF:ChangeLoggable')->setOption('enabled', false);
}
$user->save();
if ($this->logIp)
{
$ip = ($this->logIp === true ? $this->app->request()->getIp() : $this->logIp);
$this->writeIpLog('update', $ip);
}
return true;
}