mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Limit outbound avatar size to 64kB
This commit is contained in:
parent
2ff2414627
commit
8407bbada9
|
@ -192,6 +192,16 @@ void ProfileForm::setToxId(const QString& id)
|
|||
|
||||
void ProfileForm::onAvatarClicked()
|
||||
{
|
||||
auto picToPng = [](QPixmap pic)
|
||||
{
|
||||
QByteArray bytes;
|
||||
QBuffer buffer(&bytes);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
pic.save(&buffer, "PNG");
|
||||
buffer.close();
|
||||
return bytes;
|
||||
};
|
||||
|
||||
QString filename = QFileDialog::getOpenFileName(0,
|
||||
tr("Choose a profile picture"),
|
||||
QDir::homePath(),
|
||||
|
@ -214,11 +224,27 @@ void ProfileForm::onAvatarClicked()
|
|||
return;
|
||||
}
|
||||
|
||||
QByteArray bytes;
|
||||
QBuffer buffer(&bytes);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
pic.save(&buffer, "PNG");
|
||||
buffer.close();
|
||||
// Limit the avatar size to 64kB
|
||||
// We do a first rescale to 256x256 in case the image was huge, then keep tryng from here
|
||||
QByteArray bytes{picToPng(pic)};
|
||||
if (bytes.size() > 65535)
|
||||
{
|
||||
pic = pic.scaled(256,256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
bytes = picToPng(pic);
|
||||
}
|
||||
if (bytes.size() > 65535)
|
||||
bytes = picToPng(pic.scaled(128,128, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
if (bytes.size() > 65535)
|
||||
bytes = picToPng(pic.scaled(64,64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
if (bytes.size() > 65535)
|
||||
bytes = picToPng(pic.scaled(32,32, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
|
||||
// If this happens, you're really doing it on purpose.
|
||||
if (bytes.size() > 65535)
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("This image is too big"));
|
||||
return;
|
||||
}
|
||||
|
||||
Nexus::getCore()->setAvatar(bytes);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user