1
0
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:
tux3 2015-05-10 00:56:41 +02:00
parent 2ff2414627
commit 8407bbada9
No known key found for this signature in database
GPG Key ID: 7E086DD661263264

View File

@ -192,6 +192,16 @@ void ProfileForm::setToxId(const QString& id)
void ProfileForm::onAvatarClicked() 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, QString filename = QFileDialog::getOpenFileName(0,
tr("Choose a profile picture"), tr("Choose a profile picture"),
QDir::homePath(), QDir::homePath(),
@ -214,11 +224,27 @@ void ProfileForm::onAvatarClicked()
return; return;
} }
QByteArray bytes; // Limit the avatar size to 64kB
QBuffer buffer(&bytes); // We do a first rescale to 256x256 in case the image was huge, then keep tryng from here
buffer.open(QIODevice::WriteOnly); QByteArray bytes{picToPng(pic)};
pic.save(&buffer, "PNG"); if (bytes.size() > 65535)
buffer.close(); {
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); Nexus::getCore()->setAvatar(bytes);
} }