Merge pull request #10081 from annando/avatar-size

Limit the size of contact avatars
This commit is contained in:
Hypolite Petovan 2021-03-26 11:01:10 -04:00 committed by GitHub
commit e5b276f58b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -460,6 +460,29 @@ class Photo
if ($Image->isValid()) {
$Image->scaleToSquare(300);
$filesize = strlen($Image->asString());
$maximagesize = DI::config()->get('system', 'maximagesize');
if (!empty($maximagesize) && ($filesize > $maximagesize)) {
Logger::info('Avatar exceeds image limit', ['uid' => $uid, 'cid' => $cid, 'maximagesize' => $maximagesize, 'size' => $filesize, 'type' => $Image->getType()]);
if ($Image->getType() == 'image/gif') {
$Image->toStatic();
$Image = new Image($Image->asString(), 'image/png');
$filesize = strlen($Image->asString());
Logger::info('Converted gif to a static png', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'type' => $Image->getType()]);
}
if ($filesize > $maximagesize) {
foreach ([160, 80] as $pixels) {
if ($filesize > $maximagesize) {
Logger::info('Resize', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'max' => $maximagesize, 'pixels' => $pixels, 'type' => $Image->getType()]);
$Image->scaleDown($pixels);
$filesize = strlen($Image->asString());
}
}
}
Logger::info('Avatar is resized', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'type' => $Image->getType()]);
}
$r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 4);
if ($r === false) {

View file

@ -575,6 +575,21 @@ class Image
return true;
}
/**
* Convert a GIF to a PNG to make it static
*/
public function toStatic()
{
if ($this->type != 'image/gif') {
return;
}
if ($this->isImagick()) {
$this->type == 'image/png';
$this->image->setFormat('png');
}
}
/**
* @param integer $max maximum
* @param integer $x x coordinate