Limit the size of contact avatars
authorMichael <heluecht@pirati.ca>
Fri, 26 Mar 2021 06:56:08 +0000 (06:56 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 26 Mar 2021 06:56:08 +0000 (06:56 +0000)
src/Model/Photo.php
src/Object/Image.php

index 4c49bdf..1690388 100644 (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) {
index c1da711..4645a86 100644 (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