From ab2f9227e5aed4c1125f298447dbf678c65ae9b6 Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana <37348419+PedroCisnerosSantana@users.noreply.github.com> Date: Tue, 6 Oct 2020 17:58:06 +0200 Subject: [PATCH 1/5] Added QRCode dependency --- composer.json | 5 ++- composer.lock | 121 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index f0ce2624cb..7f186fd59e 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "domnikl/statsd": "3.0.*", "influxdb/influxdb-php": "1.15.*", "bacon/bacon-qr-code": "2.0.2", - "phpmailer/phpmailer": "6.1.7" + "phpmailer/phpmailer": "6.1.7", + "chillerlan/php-qrcode": "^4.1" }, "require-dev": { "swoole/ide-helper": "4.5.4", @@ -72,4 +73,4 @@ "php": "7.4" } } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index 1392b6fdd0..810eb51efe 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "07a5b2d2e742e8651d58889c3253c3b5", + "content-hash": "6b4324f0371e508d691337551c00c74e", "packages": [ { "name": "appwrite/php-clamav", @@ -100,6 +100,125 @@ "homepage": "https://github.com/Bacon/BaconQrCode", "time": "2020-07-30T16:40:58+00:00" }, + { + "name": "chillerlan/php-qrcode", + "version": "4.1.0", + "source": { + "type": "git", + "url": "https://github.com/chillerlan/php-qrcode.git", + "reference": "2cecb32cf618319dd01d9bc1fa64dc6bb683df85" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/chillerlan/php-qrcode/zipball/2cecb32cf618319dd01d9bc1fa64dc6bb683df85", + "reference": "2cecb32cf618319dd01d9bc1fa64dc6bb683df85", + "shasum": "" + }, + "require": { + "chillerlan/php-settings-container": "^2.0", + "ext-mbstring": "*", + "php": "^7.4" + }, + "require-dev": { + "phan/phan": "^2.7", + "phpunit/phpunit": "^9.1", + "setasign/fpdf": "^1.8.2" + }, + "suggest": { + "chillerlan/php-authenticator": "Yet another Google authenticator! Also creates URIs for mobile apps.", + "setasign/fpdf": "Required to use the QR FPDF output." + }, + "type": "library", + "autoload": { + "psr-4": { + "chillerlan\\QRCode\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kazuhiko Arase", + "homepage": "https://github.com/kazuhikoarase" + }, + { + "name": "Smiley", + "email": "smiley@chillerlan.net", + "homepage": "https://github.com/codemasher" + }, + { + "name": "Contributors", + "homepage": "https://github.com/chillerlan/php-qrcode/graphs/contributors" + } + ], + "description": "A QR code generator. PHP 7.4+", + "homepage": "https://github.com/chillerlan/php-qrcode", + "keywords": [ + "phpqrcode", + "qr", + "qr code", + "qrcode", + "qrcode-generator" + ], + "funding": [ + { + "url": "https://ko-fi.com/codemasher", + "type": "ko_fi" + } + ], + "time": "2020-06-04T17:07:12+00:00" + }, + { + "name": "chillerlan/php-settings-container", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/chillerlan/php-settings-container.git", + "reference": "75888345532373074fba482a6642c0f8cda996f0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/chillerlan/php-settings-container/zipball/75888345532373074fba482a6642c0f8cda996f0", + "reference": "75888345532373074fba482a6642c0f8cda996f0", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.4" + }, + "require-dev": { + "phan/phan": "^2.6", + "phpunit/phpunit": "^9.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "chillerlan\\Settings\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Smiley", + "email": "smiley@chillerlan.net", + "homepage": "https://github.com/codemasher" + } + ], + "description": "A container class for immutable settings objects. Not a DI container. PHP 7.4+", + "homepage": "https://github.com/chillerlan/php-settings-container", + "keywords": [ + "PHP7", + "Settings", + "container", + "helper" + ], + "time": "2020-04-16T16:56:44+00:00" + }, { "name": "colinmollenhour/credis", "version": "1.11.2", From f91a64103225abfd09242062701d7764e369048f Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana <37348419+PedroCisnerosSantana@users.noreply.github.com> Date: Thu, 22 Oct 2020 20:50:57 +0200 Subject: [PATCH 2/5] Added QRCode dependency --- app/controllers/api/avatars.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index 0930806983..c0ccbcb2d4 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -11,12 +11,15 @@ use Utopia\Cache\Cache; use Utopia\Cache\Adapter\Filesystem; use Appwrite\Resize\Resize; use Appwrite\URL\URL as URLParse; -use BaconQrCode\Renderer\ImageRenderer; +/* use BaconQrCode\Renderer\ImageRenderer; use BaconQrCode\Renderer\Image\ImagickImageBackEnd; use BaconQrCode\Renderer\RendererStyle\RendererStyle; -use BaconQrCode\Writer; +use BaconQrCode\Writer; */ use Utopia\Config\Config; use Utopia\Validator\HexColor; +use chillerlan\QRCode\QRCode; +use chillerlan\QRCode\QROptions; +/* use SebastianBergmann\CodeCoverage\Report\Html\Renderer; */ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) { /** @var Utopia\Response $response */ @@ -366,12 +369,17 @@ App::get('/v1/avatars/qr') $download = ($download === '1' || $download === 'true' || $download === 1 || $download === true); - $renderer = new ImageRenderer( + /* $renderer = new ImageRenderer( new RendererStyle($size, $margin), new ImagickImageBackEnd('png', 100) - ); + ); */ + $qropts = new QROptions([ + 'quietzone' => $size + ]); + $qrcode = new QRCode($qropts); + $qrcode->render($text); - $writer = new Writer($renderer); + /* $writer = new Writer($renderer); */ if ($download) { $response->addHeader('Content-Disposition', 'attachment; filename="qr.png"'); @@ -380,7 +388,7 @@ App::get('/v1/avatars/qr') $response ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache ->setContentType('image/png') - ->send($writer->writeString($text)) + /* ->send($writer->writeString($text)) */ ; }, ['response']); From bff0e4db58ce6d60f91d00d494a9d5ea40c40b85 Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana <37348419+PedroCisnerosSantana@users.noreply.github.com> Date: Mon, 26 Oct 2020 14:23:58 +0100 Subject: [PATCH 3/5] Bugs fixed - Updated qrcode to v4.2 - Removed baconqrcode from composer - Removed comments - Fixed some indents - Fixed the QR endpoint --- app/controllers/api/avatars.php | 97 +++++++++++++-------------------- composer.json | 3 +- 2 files changed, 40 insertions(+), 60 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index c0ccbcb2d4..386406a252 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -11,22 +11,17 @@ use Utopia\Cache\Cache; use Utopia\Cache\Adapter\Filesystem; use Appwrite\Resize\Resize; use Appwrite\URL\URL as URLParse; -/* use BaconQrCode\Renderer\ImageRenderer; -use BaconQrCode\Renderer\Image\ImagickImageBackEnd; -use BaconQrCode\Renderer\RendererStyle\RendererStyle; -use BaconQrCode\Writer; */ use Utopia\Config\Config; use Utopia\Validator\HexColor; use chillerlan\QRCode\QRCode; use chillerlan\QRCode\QROptions; -/* use SebastianBergmann\CodeCoverage\Report\Html\Renderer; */ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) { /** @var Utopia\Response $response */ $code = \strtolower($code); $type = \strtolower($type); - $set = Config::getParam('avatar-'.$type, []); + $set = Config::getParam('avatar-' . $type, []); if (empty($set)) { throw new Exception('Avatar set not found', 404); @@ -41,16 +36,16 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) } $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache - $key = \md5('/v1/avatars/:type/:code-'.$code.$width.$height.$quality.$output); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache + $key = \md5('/v1/avatars/:type/:code-' . $code . $width . $height . $quality . $output); $path = $set[$code]; $type = 'png'; if (!\is_readable($path)) { - throw new Exception('File not readable in '.$path, 500); + throw new Exception('File not readable in ' . $path, 500); } - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); if ($data) { @@ -60,8 +55,7 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data) - ; + ->send($data); } $resize = new Resize(\file_get_contents($path)); @@ -71,15 +65,14 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) $output = (empty($output)) ? $type : $output; $data = $resize->output($output, $quality); - + $cache->save($key, $data); - + $response ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data, null); - ; + ->send($data, null);; unset($resize); }; @@ -93,7 +86,7 @@ App::get('/v1/avatars/credit-cards/:code') ->label('sdk.method', 'getCreditCard') ->label('sdk.methodType', 'location') ->label('sdk.description', '/docs/references/avatars/get-credit-card.md') - ->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: '.\implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))).'.') + ->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: ' . \implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))) . '.') ->param('width', 100, new Range(0, 2000), 'Image width. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('height', 100, new Range(0, 2000), 'Image height. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('quality', 100, new Range(0, 100), 'Image quality. Pass an integer between 0 to 100. Defaults to 100.', true) @@ -152,10 +145,10 @@ App::get('/v1/avatars/image') $quality = 80; $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache - $key = \md5('/v2/avatars/images-'.$url.'-'.$width.'/'.$height.'/'.$quality); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache + $key = \md5('/v2/avatars/images-' . $url . '-' . $width . '/' . $height . '/' . $quality); $type = 'png'; - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 7 /* 1 week */); if ($data) { @@ -163,8 +156,7 @@ App::get('/v1/avatars/image') ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data) - ; + ->send($data); } if (!\extension_loaded('imagick')) { @@ -186,17 +178,16 @@ App::get('/v1/avatars/image') $resize->crop((int) $width, (int) $height); $output = (empty($output)) ? $type : $output; - + $data = $resize->output($output, $quality); - + $cache->save($key, $data); $response ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data); - ; + ->send($data);; unset($resize); }, ['response']); @@ -218,10 +209,10 @@ App::get('/v1/avatars/favicon') $height = 56; $quality = 80; $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache - $key = \md5('/v2/avatars/favicon-'.$url); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache + $key = \md5('/v2/avatars/favicon-' . $url); $type = 'png'; - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); if ($data) { @@ -229,8 +220,7 @@ App::get('/v1/avatars/favicon') ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data) - ; + ->send($data); } if (!\extension_loaded('imagick')) { @@ -244,7 +234,8 @@ App::get('/v1/avatars/favicon') CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, CURLOPT_URL => $url, - CURLOPT_USERAGENT => \sprintf(APP_USERAGENT, + CURLOPT_USERAGENT => \sprintf( + APP_USERAGENT, App::getEnv('_APP_VERSION', 'UNKNOWN'), App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) ), @@ -305,7 +296,7 @@ App::get('/v1/avatars/favicon') if (empty($outputHref) || empty($outputExt)) { $default = \parse_url($url); - $outputHref = $default['scheme'].'://'.$default['host'].'/favicon.ico'; + $outputHref = $default['scheme'] . '://' . $default['host'] . '/favicon.ico'; $outputExt = 'ico'; } @@ -322,8 +313,7 @@ App::get('/v1/avatars/favicon') ->setContentType('image/x-icon') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data) - ; + ->send($data); } $fetch = @\file_get_contents($outputHref, false); @@ -368,28 +358,20 @@ App::get('/v1/avatars/qr') /** @var Utopia\Response $response */ $download = ($download === '1' || $download === 'true' || $download === 1 || $download === true); - - /* $renderer = new ImageRenderer( - new RendererStyle($size, $margin), - new ImagickImageBackEnd('png', 100) - ); */ $qropts = new QROptions([ 'quietzone' => $size ]); $qrcode = new QRCode($qropts); $qrcode->render($text); - /* $writer = new Writer($renderer); */ - if ($download) { $response->addHeader('Content-Disposition', 'attachment; filename="qr.png"'); } $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache ->setContentType('image/png') - /* ->send($writer->writeString($text)) */ - ; + ->send($qrcode->render($text)); }, ['response']); App::get('/v1/avatars/initials') @@ -423,7 +405,7 @@ App::get('/v1/avatars/initials') ['color' => '#610008', 'background' => '#f6d2d5'] // RED ]; - $rand = \rand(0, \count($themes)-1); + $rand = \rand(0, \count($themes) - 1); $name = (!empty($name)) ? $name : $user->getAttribute('name', $user->getAttribute('email', '')); $words = \explode(' ', \strtoupper($name)); @@ -440,23 +422,23 @@ App::get('/v1/avatars/initials') } $length = \count($words); - $rand = \substr($code,-1); - $background = (!empty($background)) ? '#'.$background : $themes[$rand]['background']; - $color = (!empty($color)) ? '#'.$color : $themes[$rand]['color']; + $rand = \substr($code, -1); + $background = (!empty($background)) ? '#' . $background : $themes[$rand]['background']; + $color = (!empty($color)) ? '#' . $color : $themes[$rand]['color']; $image = new \Imagick(); $draw = new \ImagickDraw(); $fontSize = \min($width, $height) / 2; - - $draw->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); - $image->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); + + $draw->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf"); + $image->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf"); $draw->setFillColor(new \ImagickPixel($color)); $draw->setFontSize($fontSize); - + $draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->annotation($width / 1.97, ($height / 2) + ($fontSize / 3), $initials); - + $image->newImage($width, $height, $background); $image->setImageFormat("png"); $image->drawImage($draw); @@ -464,8 +446,7 @@ App::get('/v1/avatars/initials') //$image->setImageCompressionQuality(9 - round(($quality / 100) * 9)); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache ->setContentType('image/png') - ->send($image->getImageBlob()) - ; - }, ['response', 'user']); \ No newline at end of file + ->send($image->getImageBlob()); + }, ['response', 'user']); diff --git a/composer.json b/composer.json index 7f186fd59e..cff7b542bc 100644 --- a/composer.json +++ b/composer.json @@ -49,9 +49,8 @@ "dragonmantank/cron-expression": "3.0.1", "domnikl/statsd": "3.0.*", "influxdb/influxdb-php": "1.15.*", - "bacon/bacon-qr-code": "2.0.2", "phpmailer/phpmailer": "6.1.7", - "chillerlan/php-qrcode": "^4.1" + "chillerlan/php-qrcode": "^4.2" }, "require-dev": { "swoole/ide-helper": "4.5.4", From c286f23e1ab2dc8dfd978d240a7d24842c73fe89 Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana <37348419+PedroCisnerosSantana@users.noreply.github.com> Date: Mon, 26 Oct 2020 19:14:04 +0100 Subject: [PATCH 4/5] Removed extra spaces --- app/controllers/api/avatars.php | 85 ++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index 386406a252..b2ff5b50e1 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -21,7 +21,7 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) $code = \strtolower($code); $type = \strtolower($type); - $set = Config::getParam('avatar-' . $type, []); + $set = Config::getParam('avatar-'.$type, []); if (empty($set)) { throw new Exception('Avatar set not found', 404); @@ -36,16 +36,16 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) } $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache - $key = \md5('/v1/avatars/:type/:code-' . $code . $width . $height . $quality . $output); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache + $key = \md5('/v1/avatars/:type/:code-'.$code.$width.$height.$quality.$output); $path = $set[$code]; $type = 'png'; if (!\is_readable($path)) { - throw new Exception('File not readable in ' . $path, 500); + throw new Exception('File not readable in '.$path, 500); } - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); if ($data) { @@ -55,7 +55,8 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data); + ->send($data) + ; } $resize = new Resize(\file_get_contents($path)); @@ -65,14 +66,15 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response) $output = (empty($output)) ? $type : $output; $data = $resize->output($output, $quality); - + $cache->save($key, $data); - + $response ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data, null);; + ->send($data, null); + ; unset($resize); }; @@ -86,7 +88,7 @@ App::get('/v1/avatars/credit-cards/:code') ->label('sdk.method', 'getCreditCard') ->label('sdk.methodType', 'location') ->label('sdk.description', '/docs/references/avatars/get-credit-card.md') - ->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: ' . \implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))) . '.') + ->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: '.\implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))).'.') ->param('width', 100, new Range(0, 2000), 'Image width. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('height', 100, new Range(0, 2000), 'Image height. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('quality', 100, new Range(0, 100), 'Image quality. Pass an integer between 0 to 100. Defaults to 100.', true) @@ -145,10 +147,10 @@ App::get('/v1/avatars/image') $quality = 80; $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache - $key = \md5('/v2/avatars/images-' . $url . '-' . $width . '/' . $height . '/' . $quality); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache + $key = \md5('/v2/avatars/images-'.$url.'-'.$width.'/'.$height.'/'.$quality); $type = 'png'; - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 7 /* 1 week */); if ($data) { @@ -156,7 +158,8 @@ App::get('/v1/avatars/image') ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data); + ->send($data) + ; } if (!\extension_loaded('imagick')) { @@ -178,16 +181,17 @@ App::get('/v1/avatars/image') $resize->crop((int) $width, (int) $height); $output = (empty($output)) ? $type : $output; - + $data = $resize->output($output, $quality); - + $cache->save($key, $data); $response ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data);; + ->send($data); + ; unset($resize); }, ['response']); @@ -209,10 +213,10 @@ App::get('/v1/avatars/favicon') $height = 56; $quality = 80; $output = 'png'; - $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache - $key = \md5('/v2/avatars/favicon-' . $url); + $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache + $key = \md5('/v2/avatars/favicon-'.$url); $type = 'png'; - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); if ($data) { @@ -220,7 +224,8 @@ App::get('/v1/avatars/favicon') ->setContentType('image/png') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'hit') - ->send($data); + ->send($data) + ; } if (!\extension_loaded('imagick')) { @@ -234,8 +239,7 @@ App::get('/v1/avatars/favicon') CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, CURLOPT_URL => $url, - CURLOPT_USERAGENT => \sprintf( - APP_USERAGENT, + CURLOPT_USERAGENT => \sprintf(APP_USERAGENT, App::getEnv('_APP_VERSION', 'UNKNOWN'), App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) ), @@ -296,7 +300,7 @@ App::get('/v1/avatars/favicon') if (empty($outputHref) || empty($outputExt)) { $default = \parse_url($url); - $outputHref = $default['scheme'] . '://' . $default['host'] . '/favicon.ico'; + $outputHref = $default['scheme'].'://'.$default['host'].'/favicon.ico'; $outputExt = 'ico'; } @@ -313,7 +317,8 @@ App::get('/v1/avatars/favicon') ->setContentType('image/x-icon') ->addHeader('Expires', $date) ->addHeader('X-Appwrite-Cache', 'miss') - ->send($data); + ->send($data) + ; } $fetch = @\file_get_contents($outputHref, false); @@ -369,9 +374,10 @@ App::get('/v1/avatars/qr') } $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache ->setContentType('image/png') - ->send($qrcode->render($text)); + ->send($qrcode->render($text)) + ; }, ['response']); App::get('/v1/avatars/initials') @@ -405,7 +411,7 @@ App::get('/v1/avatars/initials') ['color' => '#610008', 'background' => '#f6d2d5'] // RED ]; - $rand = \rand(0, \count($themes) - 1); + $rand = \rand(0, \count($themes)-1); $name = (!empty($name)) ? $name : $user->getAttribute('name', $user->getAttribute('email', '')); $words = \explode(' ', \strtoupper($name)); @@ -422,23 +428,23 @@ App::get('/v1/avatars/initials') } $length = \count($words); - $rand = \substr($code, -1); - $background = (!empty($background)) ? '#' . $background : $themes[$rand]['background']; - $color = (!empty($color)) ? '#' . $color : $themes[$rand]['color']; + $rand = \substr($code,-1); + $background = (!empty($background)) ? '#'.$background : $themes[$rand]['background']; + $color = (!empty($color)) ? '#'.$color : $themes[$rand]['color']; $image = new \Imagick(); $draw = new \ImagickDraw(); $fontSize = \min($width, $height) / 2; - - $draw->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf"); - $image->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf"); + + $draw->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); + $image->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); $draw->setFillColor(new \ImagickPixel($color)); $draw->setFontSize($fontSize); - + $draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->annotation($width / 1.97, ($height / 2) + ($fontSize / 3), $initials); - + $image->newImage($width, $height, $background); $image->setImageFormat("png"); $image->drawImage($draw); @@ -446,7 +452,8 @@ App::get('/v1/avatars/initials') //$image->setImageCompressionQuality(9 - round(($quality / 100) * 9)); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache ->setContentType('image/png') - ->send($image->getImageBlob()); - }, ['response', 'user']); + ->send($image->getImageBlob()) + ; + }, ['response', 'user']); \ No newline at end of file From c102bee5ec9b86ebab8778ee78cde8e98336142d Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana <37348419+PedroCisnerosSantana@users.noreply.github.com> Date: Mon, 26 Oct 2020 22:08:47 +0100 Subject: [PATCH 5/5] Set output type to imagick --- app/controllers/api/avatars.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index 6c225d7112..49889f48d0 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -364,10 +364,10 @@ App::get('/v1/avatars/qr') $download = ($download === '1' || $download === 'true' || $download === 1 || $download === true); $qropts = new QROptions([ - 'quietzone' => $size + 'quietzone' => $size, + 'outputType' => QRCode::OUTPUT_IMAGICK ]); $qrcode = new QRCode($qropts); - $qrcode->render($text); if ($download) { $response->addHeader('Content-Disposition', 'attachment; filename="qr.png"'); @@ -456,4 +456,4 @@ App::get('/v1/avatars/initials') ->setContentType('image/png') ->send($image->getImageBlob()) ; - }, ['response', 'user']); \ No newline at end of file + }, ['response', 'user']);