From 8e0e131824fe2cf6a563613007072a614fbb65a6 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 6 Sep 2023 09:54:59 +0100 Subject: [PATCH 1/2] Remove Special Chars from Initials --- app/controllers/api/avatars.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index 3b93348643..a4df9431eb 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -526,8 +526,10 @@ App::get('/v1/avatars/initials') $code = 0; foreach ($words as $key => $w) { - $initials .= $w[0] ?? ''; - $code += (isset($w[0])) ? \ord($w[0]) : 0; + if (ctype_alnum($w[0] ?? '')) { + $initials .= $w[0]; + $code += ord($w[0]); + } if ($key == 1) { break; From 1136dc9e646e952276a15af9e5fbf18fbd58e117 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 29 Sep 2023 16:27:59 +0100 Subject: [PATCH 2/2] Add Test and don't stop on special char --- app/controllers/api/avatars.php | 6 +++--- tests/e2e/Services/Avatars/AvatarsBase.php | 24 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index a4df9431eb..a39039973e 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -529,10 +529,10 @@ App::get('/v1/avatars/initials') if (ctype_alnum($w[0] ?? '')) { $initials .= $w[0]; $code += ord($w[0]); - } - if ($key == 1) { - break; + if ($key == 1) { + break; + } } } diff --git a/tests/e2e/Services/Avatars/AvatarsBase.php b/tests/e2e/Services/Avatars/AvatarsBase.php index 1652e01514..d88c3da90a 100644 --- a/tests/e2e/Services/Avatars/AvatarsBase.php +++ b/tests/e2e/Services/Avatars/AvatarsBase.php @@ -524,4 +524,28 @@ trait AvatarsBase $this->assertEquals('PNG', $image->getImageFormat()); $this->assertEquals(strlen(\file_get_contents(__DIR__ . '/../../../resources/initials.png')), strlen($response['body'])); } + + public function testSpecialCharsInitalImage() + { + $response = $this->client->call(Client::METHOD_GET, '/avatars/initials', [ + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'name' => 'W (Hello) W', + 'width' => 200, + 'height' => 200, + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('image/png', $response['headers']['content-type']); + $this->assertNotEmpty($response['body']); + + $image = new \Imagick(); + $image->readImageBlob($response['body']); + $original = new \Imagick(__DIR__ . '/../../../resources/initials.png'); + + $this->assertEquals($image->getImageWidth(), $original->getImageWidth()); + $this->assertEquals($image->getImageHeight(), $original->getImageHeight()); + $this->assertEquals('PNG', $image->getImageFormat()); + $this->assertEquals(strlen(\file_get_contents(__DIR__ . '/../../../resources/initials.png')), strlen($response['body'])); + } }