From 03277c8227324a213b9d736bd2716239a69c5671 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 00:48:01 -0400 Subject: [PATCH 1/9] Auto generate colors --- src/InitialAvatar.php | 133 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/src/InitialAvatar.php b/src/InitialAvatar.php index a3b931c..07bdd50 100644 --- a/src/InitialAvatar.php +++ b/src/InitialAvatar.php @@ -207,6 +207,27 @@ public function color( $color ) { return $this; } + /** + * Automatically set a font and/or background color based on the supplied name + * + * @param bool $foreground + * @param bool $background + * + * @return $this + */ + public function autoColor(bool $foreground = true, bool $background = true, int $saturation = 85, int $luminance = 60) + { + + $hue = (crc32($this->name) % 360) / 360; + $saturation /= 100; + $luminance /= 100; + + $this->bgColor = $this->convertHSLtoRGB($hue, $saturation, $luminance); + $this->fontColor = $this->getContrastColor($this->bgColor); + + return $this; + } + /** * Set the font file by path or int (1-5). * @@ -738,4 +759,116 @@ protected function getFontByScript() { return $this->getFontFile(); } + + /** + * Convert HSL color value produced by autoColor() to RGB value expected by image driver + */ + protected function convertHSLtoRGB($h, $s, $l, $toHex = true) + { + assert((0 <= $h) && ($h <= 1)); + + $red = $l; + $green = $l; + $blue = $l; + + $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s); + if ($v > 0) { + $m = $l + $l - $v; + $sv = ($v - $m) / $v; + $h *= 6.0; + $sextant = floor($h); + $fract = $h - $sextant; + $vsf = $v * $sv * $fract; + $mid1 = $m + $vsf; + $mid2 = $v - $vsf; + + switch ($sextant) { + case 0: + $red = $v; + $green = $mid1; + $blue = $m; + break; + case 1: + $red = $mid2; + $green = $v; + $blue = $m; + break; + case 2: + $red = $m; + $green = $v; + $blue = $mid1; + break; + case 3: + $red = $m; + $green = $mid2; + $blue = $v; + break; + case 4: + $red = $mid1; + $green = $m; + $blue = $v; + break; + case 5: + $red = $v; + $green = $m; + $blue = $mid2; + break; + } + } + + $red = round($red * 255, 0); + $green = round($green * 255, 0); + $blue = round($blue * 255, 0); + + if ($toHex) { + $red = ($red < 15) ? '0' . dechex($red) : dechex($red); + $green = ($green < 15) ? '0' . dechex($green) : dechex($green); + $blue = ($blue < 15) ? '0' . dechex($blue) : dechex($blue); + return "#{$red}{$green}{$blue}"; + } else { + return ['red' => $red, 'green' => $green, 'blue' => $blue]; + } + } + + /** + * Get contrasting foreground color for autoColor background + */ + protected function getContrastColor($hexColor) + { + + // hexColor RGB + $R1 = hexdec(substr($hexColor, 1, 2)); + $G1 = hexdec(substr($hexColor, 3, 2)); + $B1 = hexdec(substr($hexColor, 5, 2)); + + // Black RGB + $blackColor = "#000000"; + $R2BlackColor = hexdec(substr($blackColor, 1, 2)); + $G2BlackColor = hexdec(substr($blackColor, 3, 2)); + $B2BlackColor = hexdec(substr($blackColor, 5, 2)); + + // Calc contrast ratio + $L1 = 0.2126 * pow($R1 / 255, 2.2) + + 0.7152 * pow($G1 / 255, 2.2) + + 0.0722 * pow($B1 / 255, 2.2); + + $L2 = 0.2126 * pow($R2BlackColor / 255, 2.2) + + 0.7152 * pow($G2BlackColor / 255, 2.2) + + 0.0722 * pow($B2BlackColor / 255, 2.2); + + $contrastRatio = 0; + if ($L1 > $L2) { + $contrastRatio = (int)(($L1 + 0.05) / ($L2 + 0.05)); + } else { + $contrastRatio = (int)(($L2 + 0.05) / ($L1 + 0.05)); + } + + // If contrast is more than 5, return black color + if ($contrastRatio > 5) { + return '#000000'; + } else { + // if not, return white color. + return '#FFFFFF'; + } + } } From 5da309385a96ff51b0ac6e6b2126f71c24fc148e Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 00:52:21 -0400 Subject: [PATCH 2/9] Add example auto color --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 33b89ef..4a64fc5 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,12 @@ $image = $avatar->background('#ff0000')->generate(); $image = $avatar->color('#ff0000')->generate(); ```` +### Auto Color +````php +// Will choose a background color based on `name` and a contrasting font color. The color for a specific name will always be the same. +$image = $avatar->autoColor()->generate(); +```` + ### Font file - default: /fonts/OpenSans-Regular.ttf Two fonts with two variants are included: * /fonts/OpenSans-Regular.ttf From dab05d2312843dd09bd55c5ed4c9f50a86676b33 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 00:59:29 -0400 Subject: [PATCH 3/9] Add tests for autocolor --- tests/ParameterTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/ParameterTest.php b/tests/ParameterTest.php index 057ab03..ca6e01b 100644 --- a/tests/ParameterTest.php +++ b/tests/ParameterTest.php @@ -98,4 +98,17 @@ public function can_set_auto_font() $this->assertNotTrue($avatar->getAutoFont()); } + + public function can_set_background_color() + { + $avatar = new InitialAvatar(); + + $avatar->autoColor(); + + $this->assertEquals('#eaf042', $avatar->getBackgroundColor()); + + $this->assertEquals('#000000', $avatar->getColor()); + } + + } From fa73a31b0aa4ee59c4c1053a1556666488894df8 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 01:01:36 -0400 Subject: [PATCH 4/9] Fix test name Copy/paste mistake --- tests/ParameterTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/ParameterTest.php b/tests/ParameterTest.php index ca6e01b..0755469 100644 --- a/tests/ParameterTest.php +++ b/tests/ParameterTest.php @@ -99,7 +99,8 @@ public function can_set_auto_font() $this->assertNotTrue($avatar->getAutoFont()); } - public function can_set_background_color() + /** @test */ + public function can_set_auto_color() { $avatar = new InitialAvatar(); @@ -109,6 +110,4 @@ public function can_set_background_color() $this->assertEquals('#000000', $avatar->getColor()); } - - } From b6782107ae5173a3c52466bfebc2df7a57e69826 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 01:03:20 -0400 Subject: [PATCH 5/9] StyleCI fixes --- src/InitialAvatar.php | 1706 +++++++++++++++++++-------------------- tests/ParameterTest.php | 2 +- 2 files changed, 854 insertions(+), 854 deletions(-) diff --git a/src/InitialAvatar.php b/src/InitialAvatar.php index 07bdd50..1f0a4d4 100644 --- a/src/InitialAvatar.php +++ b/src/InitialAvatar.php @@ -18,857 +18,857 @@ class InitialAvatar { - /** @var ImageManager */ - protected $image; - - /** @var Initials */ - protected $initials_generator; - - protected $driver = 'gd'; // imagick or gd - protected $fontSize = 0.5; - protected $name = 'John Doe'; - protected $width = 48; - protected $height = 48; - protected $bgColor = '#f0e9e9'; - protected $fontColor = '#8b5d5d'; - protected $rounded = false; - protected $smooth = false; - protected $autofont = false; - protected $keepCase = false; - protected $allowSpecialCharacters = true; - protected $fontFile = '/fonts/OpenSans-Regular.ttf'; - protected $fontName = 'OpenSans, sans-serif'; - protected $generated_initials = 'JD'; - protected $preferBold = false; - - /** - * Language eg.en zh-CN - * - * @var string - */ - protected $language = 'en'; - - /** - * Role translator - * - * @var Base - */ - protected $translator; - - /** - * Language related to translator - * - * @var array - */ - protected $translatorMap = [ - 'en' => En::class, - 'zh-CN' => ZhCN::class, - ]; - - public function __construct() { - $this->setupImageManager(); - $this->initials_generator = new Initials(); - } - - /** - * Create a ImageManager instance - */ - protected function setupImageManager() { - $this->image = new ImageManager( [ 'driver' => $this->getDriver() ] ); - } - - /** - * Set the name used for generating initials. - * - * @param string $nameOrInitials - * - * @return $this - */ - public function name( $nameOrInitials ) { - $nameOrInitials = $this->translate( $nameOrInitials ); - $this->name = $nameOrInitials; - $this->initials_generator->name( $nameOrInitials ); - - return $this; - } - - /** - * Transforms a unicode string to the proper format - * - * @param string $char the code to be converted (e.g., f007 would mean the "user" symbol) - * - * @return $this - */ - public function glyph( $char ) { - $uChar = json_decode( sprintf( '"\u%s"', $char ), false ); - $this->name( $uChar ); - - return $this; - } - - /** - * Set the length of the generated initials. - * - * @param int $length - * - * @return $this - */ - public function length( $length = 2 ) { - $this->initials_generator->length( $length ); - - return $this; - } - - /** - * Set the avatar/image size in pixels. - * - * @param int $size - * - * @return $this - */ - public function size( $size ) { - $this->width = (int) $size; - $this->height = (int) $size; - - return $this; - } - - /** - * Set the avatar/image height in pixels. - * - * @param int $height - * - * @return $this - */ - public function height( $height ) { - $this->height = (int) $height; - - return $this; - } - - /** - * Set the avatar/image width in pixels. - * - * @param int $width - * - * @return $this - */ - public function width( $width ) { - $this->width = (int) $width; - - return $this; - } - - /** - * Prefer bold fonts (if possible) - * - * @return $this - */ - public function preferBold() { - $this->preferBold = true; - - return $this; - } - - /** - * Prefer regular fonts (if possible) - * - * @return $this - */ - public function preferRegular() { - $this->preferBold = false; - - return $this; - } - - /** - * Set the background color. - * - * @param string $background - * - * @return $this - */ - public function background( $background ) { - $this->bgColor = (string) $background; - - return $this; - } - - /** - * Set the font color. - * - * @param string $color - * - * @return $this - */ - public function color( $color ) { - $this->fontColor = (string) $color; - - return $this; - } - - /** - * Automatically set a font and/or background color based on the supplied name - * - * @param bool $foreground - * @param bool $background - * - * @return $this - */ - public function autoColor(bool $foreground = true, bool $background = true, int $saturation = 85, int $luminance = 60) - { - - $hue = (crc32($this->name) % 360) / 360; - $saturation /= 100; - $luminance /= 100; - - $this->bgColor = $this->convertHSLtoRGB($hue, $saturation, $luminance); - $this->fontColor = $this->getContrastColor($this->bgColor); - - return $this; - } - - /** - * Set the font file by path or int (1-5). - * - * @param string|int $font - * - * @return $this - */ - public function font( $font ) { - $this->fontFile = $font; - - return $this; - } - - /** - * Set the font name - * - * Example: "Open Sans" - * - * @param string $name - * - * @return $this - */ - public function fontName( $name ) { - $this->fontName = $name; - - return $this; - } - - /** - * Use imagick as the driver. - * - * @return $this - */ - public function imagick() { - $this->driver = 'imagick'; - - $this->setupImageManager(); - - return $this; - } - - /** - * Use GD as the driver. - * - * @return $this - */ - public function gd() { - $this->driver = 'gd'; - - $this->setupImageManager(); - - return $this; - } - - /** - * Set if should make a round image or not. - * - * @param bool $rounded - * - * @return $this - */ - public function rounded( $rounded = true ) { - $this->rounded = (bool) $rounded; - - return $this; - } - - /** - * Set if should detect character script - * and use a font that supports it. - * - * @param bool $autofont - * - * @return $this - */ - public function autoFont( $autofont = true ) { - $this->autofont = (bool) $autofont; - - return $this; - } - - /** - * Set if should make a rounding smoother with a resizing hack. - * - * @param bool $smooth - * - * @return $this - */ - public function smooth( $smooth = true ) { - $this->smooth = (bool) $smooth; - - return $this; - } - - /** - * Set if should skip uppercasing the name. - * - * @param bool $keepCase - * - * @return $this - */ - public function keepCase( $keepCase = true ) { - $this->keepCase = (bool) $keepCase; - - return $this; - } - - /** - * Set if should allow (or remove) special characters - * - * @param bool $allowSpecialCharacters - * - * @return $this - */ - public function allowSpecialCharacters( $allowSpecialCharacters = true ) { - $this->allowSpecialCharacters = (bool) $allowSpecialCharacters; - - return $this; - } - - /** - * Set the font size in percentage - * (0.1 = 10%). - * - * @param float $size - * - * @return $this - */ - public function fontSize( $size = 0.5 ) { - $this->fontSize = number_format( $size, 2 ); - - return $this; - } - - /** - * Generate the image. - * - * @param null|string $name - * - * @return Image - */ - public function generate( $name = null ) { - if ( $name !== null ) { - $this->name = $name; - $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->generate( $name ); - } - - return $this->makeAvatar( $this->image ); - } - - /** - * Generate the image. - * - * @param null|string $name - * - * @return SVG - */ - public function generateSvg( $name = null ) { - if ( $name !== null ) { - $this->name = $name; - $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->generate( $name ); - } - - return $this->makeSvgAvatar(); - } - - /** - * Will return the generated initials. - * - * @return string - */ - public function getInitials() { - return $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->name( $this->name ) - ->getInitials(); - } - - /** - * Will return the background color parameter. - * - * @return string - */ - public function getBackgroundColor() { - return $this->bgColor; - } - - /** - * Will return the set driver. - * - * @return string - */ - public function getDriver() { - return $this->driver; - } - - /** - * Will return the font color parameter. - * - * @return string - */ - public function getColor() { - return $this->fontColor; - } - - /** - * Will return the font size parameter. - * - * @return float - */ - public function getFontSize() { - return $this->fontSize; - } - - /** - * Will return the font file parameter. - * - * @return string|int - */ - public function getFontFile() { - return $this->fontFile; - } - - /** - * Will return the font name parameter for SVGs. - * - * @return string - */ - public function getFontName() { - return $this->fontName; - } - - /** - * Will return the round parameter. - * - * @return bool - */ - public function getRounded() { - return $this->rounded; - } - - /** - * Will return the smooth parameter. - * - * @return bool - */ - public function getSmooth() { - return $this->smooth; - } - - /** - * @deprecated for getWidth and getHeight - */ - public function getSize() { - return $this->getWidth(); - } - - /** - * Will return the width parameter. - * - * @return int - */ - public function getWidth() { - return $this->width; - } - - /** - * Will return the height parameter. - * - * @return int - */ - public function getHeight() { - return $this->height; - } - - /** - * Will return the keepCase parameter. - * - * @return boolean - */ - public function getKeepCase() { - return $this->keepCase; - } - - /** - * Will return the allowSpecialCharacters parameter. - * - * @return boolean - */ - public function getAllowSpecialCharacters() { - return $this->allowSpecialCharacters; - } - - /** - * Will return the autofont parameter. - * - * @return bool - */ - public function getAutoFont() { - return $this->autofont; - } - - /** - * Set language of name, pls use `language` before `name`, just like - * ```php - * $avatar->language('en')->name('Mr Green'); // Right - * $avatar->name('Mr Green')->language('en'); // Wrong - * ``` - * - * @param string $language - * - * @return $this - */ - public function language( $language ) { - $this->language = $language ?: 'en'; - - return $this; - } - - /** - * Add new translators designed by user - * - * @param array $translatorMap - * ```php - * $translatorMap = [ - * 'fr' => 'foo\bar\Fr', - * 'zh-TW' => 'foo\bar\ZhTW' - * ]; - * ``` - * - * @return $this - */ - public function addTranslators( $translatorMap ) { - $this->translatorMap = array_merge( $this->translatorMap, $translatorMap ); - - return $this; - } - - /** - * @inheritdoc - */ - protected function translate( $nameOrInitials ) { - return $this->getTranslator()->translate( $nameOrInitials ); - } - - /** - * Instance the translator by language - * - * @return Base - */ - protected function getTranslator() { - if ( $this->translator instanceof Base && $this->translator->getSourceLanguage() === $this->language ) { - return $this->translator; - } - - $translatorClass = array_key_exists( $this->language, $this->translatorMap ) ? $this->translatorMap[ $this->language ] : 'LasseRafn\\InitialAvatarGenerator\\Translator\\En'; - - return $this->translato = new $translatorClass(); - } - - /** - * @param ImageManager $image - * - * @return Image - */ - protected function makeAvatar( $image ) { - $width = $this->getWidth(); - $height = $this->getHeight(); - $bgColor = $this->getBackgroundColor(); - $name = $this->getInitials(); - $fontFile = $this->findFontFile(); - $color = $this->getColor(); - $fontSize = $this->getFontSize(); - - if ( $this->getRounded() && $this->getSmooth() ) { - $width *= 5; - $height *= 5; - } - - $avatar = $image->canvas( $width, $height, ! $this->getRounded() ? $bgColor : null ); - - if ( $this->getRounded() ) { - $avatar = $avatar->circle( $width - 2, $width / 2, $height / 2, function ( $draw ) use ( $bgColor ) { - return $draw->background( $bgColor ); - } ); - } - - if ( $this->getRounded() && $this->getSmooth() ) { - $width /= 5; - $height /= 5; - $avatar->resize( $width, $height ); - } - - return $avatar->text( $name, $width / 2, $height / 2, function ( AbstractFont $font ) use ( $width, $color, $fontFile, $fontSize ) { - $font->file( $fontFile ); - $font->size( $width * $fontSize ); - $font->color( $color ); - $font->align( 'center' ); - $font->valign( 'center' ); - } ); - } - - /** - * @return SVG - */ - protected function makeSvgAvatar() { - // Original document - $image = new SVG( $this->getWidth(), $this->getHeight() ); - $document = $image->getDocument(); - - // Background - if ( $this->getRounded() ) { - // Circle - $background = new SVGCircle( $this->getWidth() / 2, $this->getHeight() / 2, $this->getWidth() / 2 ); - } else { - // Rectangle - $background = new SVGRect( 0, 0, $this->getWidth(), $this->getHeight() ); - } - - $background->setStyle( 'fill', $this->getBackgroundColor() ); - $document->addChild( $background ); - - // Text - $text = new SVGText( $this->getInitials(), '50%', '50%' ); - $text->setFont( new SVGFont( $this->getFontName(), $this->findFontFile() ) ); - $text->setStyle( 'line-height', 1 ); - $text->setAttribute( 'dy', '.1em' ); - $text->setAttribute( 'fill', $this->getColor() ); - $text->setAttribute('font-size', $this->getFontSize() * $this->getWidth()); - $text->setAttribute( 'text-anchor', 'middle' ); - $text->setAttribute( 'dominant-baseline', 'middle' ); - - if ( $this->preferBold ) { - $text->setStyle( 'font-weight', 600 ); - } - - $document->addChild( $text ); - - return $image; - } - - protected function findFontFile() { - $fontFile = $this->getFontFile(); - - if ( $this->getAutoFont() ) { - $fontFile = $this->getFontByScript(); - } - - if ( is_int( $fontFile ) && \in_array( $fontFile, [ 1, 2, 3, 4, 5 ], false ) ) { - return $fontFile; - } - - $weightsToTry = [ 'Regular' ]; - - if ( $this->preferBold ) { - $weightsToTry = [ 'Bold', 'Semibold', 'Regular' ]; - } - - $originalFile = $fontFile; - - foreach ( $weightsToTry as $weight ) { - $fontFile = preg_replace( '/(\-(Bold|Semibold|Regular))/', "-{$weight}", $originalFile ); - - if ( file_exists( $fontFile ) ) { - return $fontFile; - } - - if ( file_exists( __DIR__ . $fontFile ) ) { - return __DIR__ . $fontFile; - } - - if ( file_exists( __DIR__ . '/' . $fontFile ) ) { - return __DIR__ . '/' . $fontFile; - } - } - - return 1; - } - - protected function getFontByScript() { - // Arabic - if ( StringScript::isArabic( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Arabic-Regular.ttf'; - } - - // Armenian - if ( StringScript::isArmenian( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Armenian-Regular.ttf'; - } - - // Bengali - if ( StringScript::isBengali( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Bengali-Regular.ttf'; - } - - // Georgian - if ( StringScript::isGeorgian( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Georgian-Regular.ttf'; - } - - // Hebrew - if ( StringScript::isHebrew( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Hebrew-Regular.ttf'; - } - - // Mongolian - if ( StringScript::isMongolian( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Mongolian-Regular.ttf'; - } - - // Thai - if ( StringScript::isThai( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Thai-Regular.ttf'; - } - - // Tibetan - if ( StringScript::isTibetan( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-Tibetan-Regular.ttf'; - } - - // Chinese & Japanese - if ( StringScript::isJapanese( $this->getInitials() ) || StringScript::isChinese( $this->getInitials() ) ) { - return __DIR__ . '/fonts/script/Noto-CJKJP-Regular.otf'; - } - - return $this->getFontFile(); - } - - /** - * Convert HSL color value produced by autoColor() to RGB value expected by image driver - */ - protected function convertHSLtoRGB($h, $s, $l, $toHex = true) - { - assert((0 <= $h) && ($h <= 1)); - - $red = $l; - $green = $l; - $blue = $l; - - $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s); - if ($v > 0) { - $m = $l + $l - $v; - $sv = ($v - $m) / $v; - $h *= 6.0; - $sextant = floor($h); - $fract = $h - $sextant; - $vsf = $v * $sv * $fract; - $mid1 = $m + $vsf; - $mid2 = $v - $vsf; - - switch ($sextant) { - case 0: - $red = $v; - $green = $mid1; - $blue = $m; - break; - case 1: - $red = $mid2; - $green = $v; - $blue = $m; - break; - case 2: - $red = $m; - $green = $v; - $blue = $mid1; - break; - case 3: - $red = $m; - $green = $mid2; - $blue = $v; - break; - case 4: - $red = $mid1; - $green = $m; - $blue = $v; - break; - case 5: - $red = $v; - $green = $m; - $blue = $mid2; - break; - } - } - - $red = round($red * 255, 0); - $green = round($green * 255, 0); - $blue = round($blue * 255, 0); - - if ($toHex) { - $red = ($red < 15) ? '0' . dechex($red) : dechex($red); - $green = ($green < 15) ? '0' . dechex($green) : dechex($green); - $blue = ($blue < 15) ? '0' . dechex($blue) : dechex($blue); - return "#{$red}{$green}{$blue}"; - } else { - return ['red' => $red, 'green' => $green, 'blue' => $blue]; - } - } - - /** - * Get contrasting foreground color for autoColor background - */ - protected function getContrastColor($hexColor) - { - - // hexColor RGB - $R1 = hexdec(substr($hexColor, 1, 2)); - $G1 = hexdec(substr($hexColor, 3, 2)); - $B1 = hexdec(substr($hexColor, 5, 2)); - - // Black RGB - $blackColor = "#000000"; - $R2BlackColor = hexdec(substr($blackColor, 1, 2)); - $G2BlackColor = hexdec(substr($blackColor, 3, 2)); - $B2BlackColor = hexdec(substr($blackColor, 5, 2)); - - // Calc contrast ratio - $L1 = 0.2126 * pow($R1 / 255, 2.2) + - 0.7152 * pow($G1 / 255, 2.2) + - 0.0722 * pow($B1 / 255, 2.2); - - $L2 = 0.2126 * pow($R2BlackColor / 255, 2.2) + - 0.7152 * pow($G2BlackColor / 255, 2.2) + - 0.0722 * pow($B2BlackColor / 255, 2.2); - - $contrastRatio = 0; - if ($L1 > $L2) { - $contrastRatio = (int)(($L1 + 0.05) / ($L2 + 0.05)); - } else { - $contrastRatio = (int)(($L2 + 0.05) / ($L1 + 0.05)); - } - - // If contrast is more than 5, return black color - if ($contrastRatio > 5) { - return '#000000'; - } else { - // if not, return white color. - return '#FFFFFF'; - } - } + /** @var ImageManager */ + protected $image; + + /** @var Initials */ + protected $initials_generator; + + protected $driver = 'gd'; // imagick or gd + protected $fontSize = 0.5; + protected $name = 'John Doe'; + protected $width = 48; + protected $height = 48; + protected $bgColor = '#f0e9e9'; + protected $fontColor = '#8b5d5d'; + protected $rounded = false; + protected $smooth = false; + protected $autofont = false; + protected $keepCase = false; + protected $allowSpecialCharacters = true; + protected $fontFile = '/fonts/OpenSans-Regular.ttf'; + protected $fontName = 'OpenSans, sans-serif'; + protected $generated_initials = 'JD'; + protected $preferBold = false; + + /** + * Language eg.en zh-CN + * + * @var string + */ + protected $language = 'en'; + + /** + * Role translator + * + * @var Base + */ + protected $translator; + + /** + * Language related to translator + * + * @var array + */ + protected $translatorMap = [ + 'en' => En::class, + 'zh-CN' => ZhCN::class, + ]; + + public function __construct() { + $this->setupImageManager(); + $this->initials_generator = new Initials(); + } + + /** + * Create a ImageManager instance + */ + protected function setupImageManager() { + $this->image = new ImageManager( [ 'driver' => $this->getDriver() ] ); + } + + /** + * Set the name used for generating initials. + * + * @param string $nameOrInitials + * + * @return $this + */ + public function name( $nameOrInitials ) { + $nameOrInitials = $this->translate( $nameOrInitials ); + $this->name = $nameOrInitials; + $this->initials_generator->name( $nameOrInitials ); + + return $this; + } + + /** + * Transforms a unicode string to the proper format + * + * @param string $char the code to be converted (e.g., f007 would mean the "user" symbol) + * + * @return $this + */ + public function glyph( $char ) { + $uChar = json_decode( sprintf( '"\u%s"', $char ), false ); + $this->name( $uChar ); + + return $this; + } + + /** + * Set the length of the generated initials. + * + * @param int $length + * + * @return $this + */ + public function length( $length = 2 ) { + $this->initials_generator->length( $length ); + + return $this; + } + + /** + * Set the avatar/image size in pixels. + * + * @param int $size + * + * @return $this + */ + public function size( $size ) { + $this->width = (int) $size; + $this->height = (int) $size; + + return $this; + } + + /** + * Set the avatar/image height in pixels. + * + * @param int $height + * + * @return $this + */ + public function height( $height ) { + $this->height = (int) $height; + + return $this; + } + + /** + * Set the avatar/image width in pixels. + * + * @param int $width + * + * @return $this + */ + public function width( $width ) { + $this->width = (int) $width; + + return $this; + } + + /** + * Prefer bold fonts (if possible) + * + * @return $this + */ + public function preferBold() { + $this->preferBold = true; + + return $this; + } + + /** + * Prefer regular fonts (if possible) + * + * @return $this + */ + public function preferRegular() { + $this->preferBold = false; + + return $this; + } + + /** + * Set the background color. + * + * @param string $background + * + * @return $this + */ + public function background( $background ) { + $this->bgColor = (string) $background; + + return $this; + } + + /** + * Set the font color. + * + * @param string $color + * + * @return $this + */ + public function color( $color ) { + $this->fontColor = (string) $color; + + return $this; + } + + /** + * Automatically set a font and/or background color based on the supplied name + * + * @param bool $foreground + * @param bool $background + * + * @return $this + */ + public function autoColor(bool $foreground = true, bool $background = true, int $saturation = 85, int $luminance = 60) + { + + $hue = (crc32($this->name) % 360) / 360; + $saturation /= 100; + $luminance /= 100; + + $this->bgColor = $this->convertHSLtoRGB($hue, $saturation, $luminance); + $this->fontColor = $this->getContrastColor($this->bgColor); + + return $this; + } + + /** + * Set the font file by path or int (1-5). + * + * @param string|int $font + * + * @return $this + */ + public function font( $font ) { + $this->fontFile = $font; + + return $this; + } + + /** + * Set the font name + * + * Example: "Open Sans" + * + * @param string $name + * + * @return $this + */ + public function fontName( $name ) { + $this->fontName = $name; + + return $this; + } + + /** + * Use imagick as the driver. + * + * @return $this + */ + public function imagick() { + $this->driver = 'imagick'; + + $this->setupImageManager(); + + return $this; + } + + /** + * Use GD as the driver. + * + * @return $this + */ + public function gd() { + $this->driver = 'gd'; + + $this->setupImageManager(); + + return $this; + } + + /** + * Set if should make a round image or not. + * + * @param bool $rounded + * + * @return $this + */ + public function rounded( $rounded = true ) { + $this->rounded = (bool) $rounded; + + return $this; + } + + /** + * Set if should detect character script + * and use a font that supports it. + * + * @param bool $autofont + * + * @return $this + */ + public function autoFont( $autofont = true ) { + $this->autofont = (bool) $autofont; + + return $this; + } + + /** + * Set if should make a rounding smoother with a resizing hack. + * + * @param bool $smooth + * + * @return $this + */ + public function smooth( $smooth = true ) { + $this->smooth = (bool) $smooth; + + return $this; + } + + /** + * Set if should skip uppercasing the name. + * + * @param bool $keepCase + * + * @return $this + */ + public function keepCase( $keepCase = true ) { + $this->keepCase = (bool) $keepCase; + + return $this; + } + + /** + * Set if should allow (or remove) special characters + * + * @param bool $allowSpecialCharacters + * + * @return $this + */ + public function allowSpecialCharacters( $allowSpecialCharacters = true ) { + $this->allowSpecialCharacters = (bool) $allowSpecialCharacters; + + return $this; + } + + /** + * Set the font size in percentage + * (0.1 = 10%). + * + * @param float $size + * + * @return $this + */ + public function fontSize( $size = 0.5 ) { + $this->fontSize = number_format( $size, 2 ); + + return $this; + } + + /** + * Generate the image. + * + * @param null|string $name + * + * @return Image + */ + public function generate( $name = null ) { + if ( $name !== null ) { + $this->name = $name; + $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) + ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) + ->generate( $name ); + } + + return $this->makeAvatar( $this->image ); + } + + /** + * Generate the image. + * + * @param null|string $name + * + * @return SVG + */ + public function generateSvg( $name = null ) { + if ( $name !== null ) { + $this->name = $name; + $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) + ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) + ->generate( $name ); + } + + return $this->makeSvgAvatar(); + } + + /** + * Will return the generated initials. + * + * @return string + */ + public function getInitials() { + return $this->initials_generator->keepCase( $this->getKeepCase() ) + ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) + ->name( $this->name ) + ->getInitials(); + } + + /** + * Will return the background color parameter. + * + * @return string + */ + public function getBackgroundColor() { + return $this->bgColor; + } + + /** + * Will return the set driver. + * + * @return string + */ + public function getDriver() { + return $this->driver; + } + + /** + * Will return the font color parameter. + * + * @return string + */ + public function getColor() { + return $this->fontColor; + } + + /** + * Will return the font size parameter. + * + * @return float + */ + public function getFontSize() { + return $this->fontSize; + } + + /** + * Will return the font file parameter. + * + * @return string|int + */ + public function getFontFile() { + return $this->fontFile; + } + + /** + * Will return the font name parameter for SVGs. + * + * @return string + */ + public function getFontName() { + return $this->fontName; + } + + /** + * Will return the round parameter. + * + * @return bool + */ + public function getRounded() { + return $this->rounded; + } + + /** + * Will return the smooth parameter. + * + * @return bool + */ + public function getSmooth() { + return $this->smooth; + } + + /** + * @deprecated for getWidth and getHeight + */ + public function getSize() { + return $this->getWidth(); + } + + /** + * Will return the width parameter. + * + * @return int + */ + public function getWidth() { + return $this->width; + } + + /** + * Will return the height parameter. + * + * @return int + */ + public function getHeight() { + return $this->height; + } + + /** + * Will return the keepCase parameter. + * + * @return boolean + */ + public function getKeepCase() { + return $this->keepCase; + } + + /** + * Will return the allowSpecialCharacters parameter. + * + * @return boolean + */ + public function getAllowSpecialCharacters() { + return $this->allowSpecialCharacters; + } + + /** + * Will return the autofont parameter. + * + * @return bool + */ + public function getAutoFont() { + return $this->autofont; + } + + /** + * Set language of name, pls use `language` before `name`, just like + * ```php + * $avatar->language('en')->name('Mr Green'); // Right + * $avatar->name('Mr Green')->language('en'); // Wrong + * ``` + * + * @param string $language + * + * @return $this + */ + public function language( $language ) { + $this->language = $language ?: 'en'; + + return $this; + } + + /** + * Add new translators designed by user + * + * @param array $translatorMap + * ```php + * $translatorMap = [ + * 'fr' => 'foo\bar\Fr', + * 'zh-TW' => 'foo\bar\ZhTW' + * ]; + * ``` + * + * @return $this + */ + public function addTranslators( $translatorMap ) { + $this->translatorMap = array_merge( $this->translatorMap, $translatorMap ); + + return $this; + } + + /** + * @inheritdoc + */ + protected function translate( $nameOrInitials ) { + return $this->getTranslator()->translate( $nameOrInitials ); + } + + /** + * Instance the translator by language + * + * @return Base + */ + protected function getTranslator() { + if ( $this->translator instanceof Base && $this->translator->getSourceLanguage() === $this->language ) { + return $this->translator; + } + + $translatorClass = array_key_exists( $this->language, $this->translatorMap ) ? $this->translatorMap[ $this->language ] : 'LasseRafn\\InitialAvatarGenerator\\Translator\\En'; + + return $this->translato = new $translatorClass(); + } + + /** + * @param ImageManager $image + * + * @return Image + */ + protected function makeAvatar( $image ) { + $width = $this->getWidth(); + $height = $this->getHeight(); + $bgColor = $this->getBackgroundColor(); + $name = $this->getInitials(); + $fontFile = $this->findFontFile(); + $color = $this->getColor(); + $fontSize = $this->getFontSize(); + + if ( $this->getRounded() && $this->getSmooth() ) { + $width *= 5; + $height *= 5; + } + + $avatar = $image->canvas( $width, $height, ! $this->getRounded() ? $bgColor : null ); + + if ( $this->getRounded() ) { + $avatar = $avatar->circle( $width - 2, $width / 2, $height / 2, function ( $draw ) use ( $bgColor ) { + return $draw->background( $bgColor ); + } ); + } + + if ( $this->getRounded() && $this->getSmooth() ) { + $width /= 5; + $height /= 5; + $avatar->resize( $width, $height ); + } + + return $avatar->text( $name, $width / 2, $height / 2, function ( AbstractFont $font ) use ( $width, $color, $fontFile, $fontSize ) { + $font->file( $fontFile ); + $font->size( $width * $fontSize ); + $font->color( $color ); + $font->align( 'center' ); + $font->valign( 'center' ); + } ); + } + + /** + * @return SVG + */ + protected function makeSvgAvatar() { + // Original document + $image = new SVG( $this->getWidth(), $this->getHeight() ); + $document = $image->getDocument(); + + // Background + if ( $this->getRounded() ) { + // Circle + $background = new SVGCircle( $this->getWidth() / 2, $this->getHeight() / 2, $this->getWidth() / 2 ); + } else { + // Rectangle + $background = new SVGRect( 0, 0, $this->getWidth(), $this->getHeight() ); + } + + $background->setStyle( 'fill', $this->getBackgroundColor() ); + $document->addChild( $background ); + + // Text + $text = new SVGText( $this->getInitials(), '50%', '50%' ); + $text->setFont( new SVGFont( $this->getFontName(), $this->findFontFile() ) ); + $text->setStyle( 'line-height', 1 ); + $text->setAttribute( 'dy', '.1em' ); + $text->setAttribute( 'fill', $this->getColor() ); + $text->setAttribute('font-size', $this->getFontSize() * $this->getWidth()); + $text->setAttribute( 'text-anchor', 'middle' ); + $text->setAttribute( 'dominant-baseline', 'middle' ); + + if ( $this->preferBold ) { + $text->setStyle( 'font-weight', 600 ); + } + + $document->addChild( $text ); + + return $image; + } + + protected function findFontFile() { + $fontFile = $this->getFontFile(); + + if ( $this->getAutoFont() ) { + $fontFile = $this->getFontByScript(); + } + + if ( is_int( $fontFile ) && \in_array( $fontFile, [ 1, 2, 3, 4, 5 ], false ) ) { + return $fontFile; + } + + $weightsToTry = [ 'Regular' ]; + + if ( $this->preferBold ) { + $weightsToTry = [ 'Bold', 'Semibold', 'Regular' ]; + } + + $originalFile = $fontFile; + + foreach ( $weightsToTry as $weight ) { + $fontFile = preg_replace( '/(\-(Bold|Semibold|Regular))/', "-{$weight}", $originalFile ); + + if ( file_exists( $fontFile ) ) { + return $fontFile; + } + + if ( file_exists( __DIR__ . $fontFile ) ) { + return __DIR__ . $fontFile; + } + + if ( file_exists( __DIR__ . '/' . $fontFile ) ) { + return __DIR__ . '/' . $fontFile; + } + } + + return 1; + } + + protected function getFontByScript() { + // Arabic + if ( StringScript::isArabic( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Arabic-Regular.ttf'; + } + + // Armenian + if ( StringScript::isArmenian( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Armenian-Regular.ttf'; + } + + // Bengali + if ( StringScript::isBengali( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Bengali-Regular.ttf'; + } + + // Georgian + if ( StringScript::isGeorgian( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Georgian-Regular.ttf'; + } + + // Hebrew + if ( StringScript::isHebrew( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Hebrew-Regular.ttf'; + } + + // Mongolian + if ( StringScript::isMongolian( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Mongolian-Regular.ttf'; + } + + // Thai + if ( StringScript::isThai( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Thai-Regular.ttf'; + } + + // Tibetan + if ( StringScript::isTibetan( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-Tibetan-Regular.ttf'; + } + + // Chinese & Japanese + if ( StringScript::isJapanese( $this->getInitials() ) || StringScript::isChinese( $this->getInitials() ) ) { + return __DIR__ . '/fonts/script/Noto-CJKJP-Regular.otf'; + } + + return $this->getFontFile(); + } + + /** + * Convert HSL color value produced by autoColor() to RGB value expected by image driver + */ + protected function convertHSLtoRGB($h, $s, $l, $toHex = true) + { + assert((0 <= $h) && ($h <= 1)); + + $red = $l; + $green = $l; + $blue = $l; + + $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s); + if ($v > 0) { + $m = $l + $l - $v; + $sv = ($v - $m) / $v; + $h *= 6.0; + $sextant = floor($h); + $fract = $h - $sextant; + $vsf = $v * $sv * $fract; + $mid1 = $m + $vsf; + $mid2 = $v - $vsf; + + switch ($sextant) { + case 0: + $red = $v; + $green = $mid1; + $blue = $m; + break; + case 1: + $red = $mid2; + $green = $v; + $blue = $m; + break; + case 2: + $red = $m; + $green = $v; + $blue = $mid1; + break; + case 3: + $red = $m; + $green = $mid2; + $blue = $v; + break; + case 4: + $red = $mid1; + $green = $m; + $blue = $v; + break; + case 5: + $red = $v; + $green = $m; + $blue = $mid2; + break; + } + } + + $red = round($red * 255, 0); + $green = round($green * 255, 0); + $blue = round($blue * 255, 0); + + if ($toHex) { + $red = ($red < 15) ? '0' . dechex($red) : dechex($red); + $green = ($green < 15) ? '0' . dechex($green) : dechex($green); + $blue = ($blue < 15) ? '0' . dechex($blue) : dechex($blue); + return "#{$red}{$green}{$blue}"; + } else { + return ['red' => $red, 'green' => $green, 'blue' => $blue]; + } + } + + /** + * Get contrasting foreground color for autoColor background + */ + protected function getContrastColor($hexColor) + { + + // hexColor RGB + $R1 = hexdec(substr($hexColor, 1, 2)); + $G1 = hexdec(substr($hexColor, 3, 2)); + $B1 = hexdec(substr($hexColor, 5, 2)); + + // Black RGB + $blackColor = "#000000"; + $R2BlackColor = hexdec(substr($blackColor, 1, 2)); + $G2BlackColor = hexdec(substr($blackColor, 3, 2)); + $B2BlackColor = hexdec(substr($blackColor, 5, 2)); + + // Calc contrast ratio + $L1 = 0.2126 * pow($R1 / 255, 2.2) + + 0.7152 * pow($G1 / 255, 2.2) + + 0.0722 * pow($B1 / 255, 2.2); + + $L2 = 0.2126 * pow($R2BlackColor / 255, 2.2) + + 0.7152 * pow($G2BlackColor / 255, 2.2) + + 0.0722 * pow($B2BlackColor / 255, 2.2); + + $contrastRatio = 0; + if ($L1 > $L2) { + $contrastRatio = (int)(($L1 + 0.05) / ($L2 + 0.05)); + } else { + $contrastRatio = (int)(($L2 + 0.05) / ($L1 + 0.05)); + } + + // If contrast is more than 5, return black color + if ($contrastRatio > 5) { + return '#000000'; + } else { + // if not, return white color. + return '#FFFFFF'; + } + } } diff --git a/tests/ParameterTest.php b/tests/ParameterTest.php index 0755469..2134b13 100644 --- a/tests/ParameterTest.php +++ b/tests/ParameterTest.php @@ -5,7 +5,7 @@ class ParameterTest extends TestCase { - /** @test */ + /** @test */ public function can_set_background_color() { $avatar = new InitialAvatar(); From 0239158f6c5e7d0ff589f1e81928badace5c55d6 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 01:12:43 -0400 Subject: [PATCH 6/9] Use the patch from StyleCI --- src/InitialAvatar.php | 325 ++++++++++++++++++++++++------------------ 1 file changed, 186 insertions(+), 139 deletions(-) diff --git a/src/InitialAvatar.php b/src/InitialAvatar.php index 1f0a4d4..4bcfbcc 100644 --- a/src/InitialAvatar.php +++ b/src/InitialAvatar.php @@ -24,22 +24,22 @@ class InitialAvatar /** @var Initials */ protected $initials_generator; - protected $driver = 'gd'; // imagick or gd - protected $fontSize = 0.5; - protected $name = 'John Doe'; - protected $width = 48; - protected $height = 48; - protected $bgColor = '#f0e9e9'; - protected $fontColor = '#8b5d5d'; - protected $rounded = false; - protected $smooth = false; - protected $autofont = false; - protected $keepCase = false; - protected $allowSpecialCharacters = true; - protected $fontFile = '/fonts/OpenSans-Regular.ttf'; - protected $fontName = 'OpenSans, sans-serif'; - protected $generated_initials = 'JD'; - protected $preferBold = false; + protected $driver = 'gd'; // imagick or gd + protected $fontSize = 0.5; + protected $name = 'John Doe'; + protected $width = 48; + protected $height = 48; + protected $bgColor = '#f0e9e9'; + protected $fontColor = '#8b5d5d'; + protected $rounded = false; + protected $smooth = false; + protected $autofont = false; + protected $keepCase = false; + protected $allowSpecialCharacters = true; + protected $fontFile = '/fonts/OpenSans-Regular.ttf'; + protected $fontName = 'OpenSans, sans-serif'; + protected $generated_initials = 'JD'; + protected $preferBold = false; /** * Language eg.en zh-CN @@ -65,7 +65,8 @@ class InitialAvatar 'zh-CN' => ZhCN::class, ]; - public function __construct() { + public function __construct() + { $this->setupImageManager(); $this->initials_generator = new Initials(); } @@ -73,8 +74,9 @@ public function __construct() { /** * Create a ImageManager instance */ - protected function setupImageManager() { - $this->image = new ImageManager( [ 'driver' => $this->getDriver() ] ); + protected function setupImageManager() + { + $this->image = new ImageManager(['driver' => $this->getDriver()]); } /** @@ -84,10 +86,11 @@ protected function setupImageManager() { * * @return $this */ - public function name( $nameOrInitials ) { - $nameOrInitials = $this->translate( $nameOrInitials ); + public function name($nameOrInitials) + { + $nameOrInitials = $this->translate($nameOrInitials); $this->name = $nameOrInitials; - $this->initials_generator->name( $nameOrInitials ); + $this->initials_generator->name($nameOrInitials); return $this; } @@ -99,9 +102,10 @@ public function name( $nameOrInitials ) { * * @return $this */ - public function glyph( $char ) { - $uChar = json_decode( sprintf( '"\u%s"', $char ), false ); - $this->name( $uChar ); + public function glyph($char) + { + $uChar = json_decode(sprintf('"\u%s"', $char), false); + $this->name($uChar); return $this; } @@ -113,8 +117,9 @@ public function glyph( $char ) { * * @return $this */ - public function length( $length = 2 ) { - $this->initials_generator->length( $length ); + public function length($length = 2) + { + $this->initials_generator->length($length); return $this; } @@ -126,7 +131,8 @@ public function length( $length = 2 ) { * * @return $this */ - public function size( $size ) { + public function size($size) + { $this->width = (int) $size; $this->height = (int) $size; @@ -140,7 +146,8 @@ public function size( $size ) { * * @return $this */ - public function height( $height ) { + public function height($height) + { $this->height = (int) $height; return $this; @@ -153,7 +160,8 @@ public function height( $height ) { * * @return $this */ - public function width( $width ) { + public function width($width) + { $this->width = (int) $width; return $this; @@ -164,7 +172,8 @@ public function width( $width ) { * * @return $this */ - public function preferBold() { + public function preferBold() + { $this->preferBold = true; return $this; @@ -175,7 +184,8 @@ public function preferBold() { * * @return $this */ - public function preferRegular() { + public function preferRegular() + { $this->preferBold = false; return $this; @@ -188,7 +198,8 @@ public function preferRegular() { * * @return $this */ - public function background( $background ) { + public function background($background) + { $this->bgColor = (string) $background; return $this; @@ -201,7 +212,8 @@ public function background( $background ) { * * @return $this */ - public function color( $color ) { + public function color($color) + { $this->fontColor = (string) $color; return $this; @@ -235,7 +247,8 @@ public function autoColor(bool $foreground = true, bool $background = true, int * * @return $this */ - public function font( $font ) { + public function font($font) + { $this->fontFile = $font; return $this; @@ -250,7 +263,8 @@ public function font( $font ) { * * @return $this */ - public function fontName( $name ) { + public function fontName($name) + { $this->fontName = $name; return $this; @@ -261,7 +275,8 @@ public function fontName( $name ) { * * @return $this */ - public function imagick() { + public function imagick() + { $this->driver = 'imagick'; $this->setupImageManager(); @@ -274,7 +289,8 @@ public function imagick() { * * @return $this */ - public function gd() { + public function gd() + { $this->driver = 'gd'; $this->setupImageManager(); @@ -289,7 +305,8 @@ public function gd() { * * @return $this */ - public function rounded( $rounded = true ) { + public function rounded($rounded = true) + { $this->rounded = (bool) $rounded; return $this; @@ -303,7 +320,8 @@ public function rounded( $rounded = true ) { * * @return $this */ - public function autoFont( $autofont = true ) { + public function autoFont($autofont = true) + { $this->autofont = (bool) $autofont; return $this; @@ -316,7 +334,8 @@ public function autoFont( $autofont = true ) { * * @return $this */ - public function smooth( $smooth = true ) { + public function smooth($smooth = true) + { $this->smooth = (bool) $smooth; return $this; @@ -329,7 +348,8 @@ public function smooth( $smooth = true ) { * * @return $this */ - public function keepCase( $keepCase = true ) { + public function keepCase($keepCase = true) + { $this->keepCase = (bool) $keepCase; return $this; @@ -342,7 +362,8 @@ public function keepCase( $keepCase = true ) { * * @return $this */ - public function allowSpecialCharacters( $allowSpecialCharacters = true ) { + public function allowSpecialCharacters($allowSpecialCharacters = true) + { $this->allowSpecialCharacters = (bool) $allowSpecialCharacters; return $this; @@ -356,8 +377,9 @@ public function allowSpecialCharacters( $allowSpecialCharacters = true ) { * * @return $this */ - public function fontSize( $size = 0.5 ) { - $this->fontSize = number_format( $size, 2 ); + public function fontSize($size = 0.5) + { + $this->fontSize = number_format($size, 2); return $this; } @@ -369,15 +391,16 @@ public function fontSize( $size = 0.5 ) { * * @return Image */ - public function generate( $name = null ) { - if ( $name !== null ) { + public function generate($name = null) + { + if ($name !== null) { $this->name = $name; - $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->generate( $name ); + $this->generated_initials = $this->initials_generator->keepCase($this->getKeepCase()) + ->allowSpecialCharacters($this->getAllowSpecialCharacters()) + ->generate($name); } - return $this->makeAvatar( $this->image ); + return $this->makeAvatar($this->image); } /** @@ -387,12 +410,13 @@ public function generate( $name = null ) { * * @return SVG */ - public function generateSvg( $name = null ) { - if ( $name !== null ) { + public function generateSvg($name = null) + { + if ($name !== null) { $this->name = $name; - $this->generated_initials = $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->generate( $name ); + $this->generated_initials = $this->initials_generator->keepCase($this->getKeepCase()) + ->allowSpecialCharacters($this->getAllowSpecialCharacters()) + ->generate($name); } return $this->makeSvgAvatar(); @@ -403,11 +427,12 @@ public function generateSvg( $name = null ) { * * @return string */ - public function getInitials() { - return $this->initials_generator->keepCase( $this->getKeepCase() ) - ->allowSpecialCharacters( $this->getAllowSpecialCharacters() ) - ->name( $this->name ) - ->getInitials(); + public function getInitials() + { + return $this->initials_generator->keepCase($this->getKeepCase()) + ->allowSpecialCharacters($this->getAllowSpecialCharacters()) + ->name($this->name) + ->getInitials(); } /** @@ -415,7 +440,8 @@ public function getInitials() { * * @return string */ - public function getBackgroundColor() { + public function getBackgroundColor() + { return $this->bgColor; } @@ -424,7 +450,8 @@ public function getBackgroundColor() { * * @return string */ - public function getDriver() { + public function getDriver() + { return $this->driver; } @@ -433,7 +460,8 @@ public function getDriver() { * * @return string */ - public function getColor() { + public function getColor() + { return $this->fontColor; } @@ -442,7 +470,8 @@ public function getColor() { * * @return float */ - public function getFontSize() { + public function getFontSize() + { return $this->fontSize; } @@ -451,7 +480,8 @@ public function getFontSize() { * * @return string|int */ - public function getFontFile() { + public function getFontFile() + { return $this->fontFile; } @@ -460,7 +490,8 @@ public function getFontFile() { * * @return string */ - public function getFontName() { + public function getFontName() + { return $this->fontName; } @@ -469,7 +500,8 @@ public function getFontName() { * * @return bool */ - public function getRounded() { + public function getRounded() + { return $this->rounded; } @@ -478,14 +510,16 @@ public function getRounded() { * * @return bool */ - public function getSmooth() { + public function getSmooth() + { return $this->smooth; } /** * @deprecated for getWidth and getHeight */ - public function getSize() { + public function getSize() + { return $this->getWidth(); } @@ -494,7 +528,8 @@ public function getSize() { * * @return int */ - public function getWidth() { + public function getWidth() + { return $this->width; } @@ -503,7 +538,8 @@ public function getWidth() { * * @return int */ - public function getHeight() { + public function getHeight() + { return $this->height; } @@ -512,7 +548,8 @@ public function getHeight() { * * @return boolean */ - public function getKeepCase() { + public function getKeepCase() + { return $this->keepCase; } @@ -521,7 +558,8 @@ public function getKeepCase() { * * @return boolean */ - public function getAllowSpecialCharacters() { + public function getAllowSpecialCharacters() + { return $this->allowSpecialCharacters; } @@ -530,7 +568,8 @@ public function getAllowSpecialCharacters() { * * @return bool */ - public function getAutoFont() { + public function getAutoFont() + { return $this->autofont; } @@ -545,7 +584,8 @@ public function getAutoFont() { * * @return $this */ - public function language( $language ) { + public function language($language) + { $this->language = $language ?: 'en'; return $this; @@ -564,8 +604,9 @@ public function language( $language ) { * * @return $this */ - public function addTranslators( $translatorMap ) { - $this->translatorMap = array_merge( $this->translatorMap, $translatorMap ); + public function addTranslators($translatorMap) + { + $this->translatorMap = array_merge($this->translatorMap, $translatorMap); return $this; } @@ -573,8 +614,9 @@ public function addTranslators( $translatorMap ) { /** * @inheritdoc */ - protected function translate( $nameOrInitials ) { - return $this->getTranslator()->translate( $nameOrInitials ); + protected function translate($nameOrInitials) + { + return $this->getTranslator()->translate($nameOrInitials); } /** @@ -582,12 +624,13 @@ protected function translate( $nameOrInitials ) { * * @return Base */ - protected function getTranslator() { - if ( $this->translator instanceof Base && $this->translator->getSourceLanguage() === $this->language ) { + protected function getTranslator() + { + if ($this->translator instanceof Base && $this->translator->getSourceLanguage() === $this->language) { return $this->translator; } - $translatorClass = array_key_exists( $this->language, $this->translatorMap ) ? $this->translatorMap[ $this->language ] : 'LasseRafn\\InitialAvatarGenerator\\Translator\\En'; + $translatorClass = array_key_exists($this->language, $this->translatorMap) ? $this->translatorMap[$this->language] : 'LasseRafn\\InitialAvatarGenerator\\Translator\\En'; return $this->translato = new $translatorClass(); } @@ -597,7 +640,8 @@ protected function getTranslator() { * * @return Image */ - protected function makeAvatar( $image ) { + protected function makeAvatar($image) + { $width = $this->getWidth(); $height = $this->getHeight(); $bgColor = $this->getBackgroundColor(); @@ -606,104 +650,106 @@ protected function makeAvatar( $image ) { $color = $this->getColor(); $fontSize = $this->getFontSize(); - if ( $this->getRounded() && $this->getSmooth() ) { + if ($this->getRounded() && $this->getSmooth()) { $width *= 5; $height *= 5; } - $avatar = $image->canvas( $width, $height, ! $this->getRounded() ? $bgColor : null ); + $avatar = $image->canvas($width, $height, !$this->getRounded() ? $bgColor : null); - if ( $this->getRounded() ) { - $avatar = $avatar->circle( $width - 2, $width / 2, $height / 2, function ( $draw ) use ( $bgColor ) { - return $draw->background( $bgColor ); - } ); + if ($this->getRounded()) { + $avatar = $avatar->circle($width - 2, $width / 2, $height / 2, function ($draw) use ($bgColor) { + return $draw->background($bgColor); + }); } - if ( $this->getRounded() && $this->getSmooth() ) { + if ($this->getRounded() && $this->getSmooth()) { $width /= 5; $height /= 5; - $avatar->resize( $width, $height ); + $avatar->resize($width, $height); } - return $avatar->text( $name, $width / 2, $height / 2, function ( AbstractFont $font ) use ( $width, $color, $fontFile, $fontSize ) { - $font->file( $fontFile ); - $font->size( $width * $fontSize ); - $font->color( $color ); - $font->align( 'center' ); - $font->valign( 'center' ); - } ); + return $avatar->text($name, $width / 2, $height / 2, function (AbstractFont $font) use ($width, $color, $fontFile, $fontSize) { + $font->file($fontFile); + $font->size($width * $fontSize); + $font->color($color); + $font->align('center'); + $font->valign('center'); + }); } /** * @return SVG */ - protected function makeSvgAvatar() { + protected function makeSvgAvatar() + { // Original document - $image = new SVG( $this->getWidth(), $this->getHeight() ); + $image = new SVG($this->getWidth(), $this->getHeight()); $document = $image->getDocument(); // Background - if ( $this->getRounded() ) { + if ($this->getRounded()) { // Circle - $background = new SVGCircle( $this->getWidth() / 2, $this->getHeight() / 2, $this->getWidth() / 2 ); + $background = new SVGCircle($this->getWidth() / 2, $this->getHeight() / 2, $this->getWidth() / 2); } else { // Rectangle - $background = new SVGRect( 0, 0, $this->getWidth(), $this->getHeight() ); + $background = new SVGRect(0, 0, $this->getWidth(), $this->getHeight()); } - $background->setStyle( 'fill', $this->getBackgroundColor() ); - $document->addChild( $background ); + $background->setStyle('fill', $this->getBackgroundColor()); + $document->addChild($background); // Text - $text = new SVGText( $this->getInitials(), '50%', '50%' ); - $text->setFont( new SVGFont( $this->getFontName(), $this->findFontFile() ) ); - $text->setStyle( 'line-height', 1 ); - $text->setAttribute( 'dy', '.1em' ); - $text->setAttribute( 'fill', $this->getColor() ); - $text->setAttribute('font-size', $this->getFontSize() * $this->getWidth()); - $text->setAttribute( 'text-anchor', 'middle' ); - $text->setAttribute( 'dominant-baseline', 'middle' ); - - if ( $this->preferBold ) { - $text->setStyle( 'font-weight', 600 ); + $text = new SVGText($this->getInitials(), '50%', '50%'); + $text->setFont(new SVGFont($this->getFontName(), $this->findFontFile())); + $text->setStyle('line-height', 1); + $text->setAttribute('dy', '.1em'); + $text->setAttribute('fill', $this->getColor()); + $text->setAttribute('font-size', $this->getFontSize() * $this->getWidth()); + $text->setAttribute('text-anchor', 'middle'); + $text->setAttribute('dominant-baseline', 'middle'); + + if ($this->preferBold) { + $text->setStyle('font-weight', 600); } - $document->addChild( $text ); + $document->addChild($text); return $image; } - protected function findFontFile() { + protected function findFontFile() + { $fontFile = $this->getFontFile(); - if ( $this->getAutoFont() ) { + if ($this->getAutoFont()) { $fontFile = $this->getFontByScript(); } - if ( is_int( $fontFile ) && \in_array( $fontFile, [ 1, 2, 3, 4, 5 ], false ) ) { + if (is_int($fontFile) && \in_array($fontFile, [1, 2, 3, 4, 5], false)) { return $fontFile; } - $weightsToTry = [ 'Regular' ]; + $weightsToTry = ['Regular']; - if ( $this->preferBold ) { - $weightsToTry = [ 'Bold', 'Semibold', 'Regular' ]; + if ($this->preferBold) { + $weightsToTry = ['Bold', 'Semibold', 'Regular']; } $originalFile = $fontFile; - foreach ( $weightsToTry as $weight ) { - $fontFile = preg_replace( '/(\-(Bold|Semibold|Regular))/', "-{$weight}", $originalFile ); + foreach ($weightsToTry as $weight) { + $fontFile = preg_replace('/(\-(Bold|Semibold|Regular))/', "-{$weight}", $originalFile); - if ( file_exists( $fontFile ) ) { + if (file_exists($fontFile)) { return $fontFile; } - if ( file_exists( __DIR__ . $fontFile ) ) { + if (file_exists(__DIR__ . $fontFile)) { return __DIR__ . $fontFile; } - if ( file_exists( __DIR__ . '/' . $fontFile ) ) { + if (file_exists(__DIR__ . '/' . $fontFile)) { return __DIR__ . '/' . $fontFile; } } @@ -711,49 +757,50 @@ protected function findFontFile() { return 1; } - protected function getFontByScript() { + protected function getFontByScript() + { // Arabic - if ( StringScript::isArabic( $this->getInitials() ) ) { + if (StringScript::isArabic($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Arabic-Regular.ttf'; } // Armenian - if ( StringScript::isArmenian( $this->getInitials() ) ) { + if (StringScript::isArmenian($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Armenian-Regular.ttf'; } // Bengali - if ( StringScript::isBengali( $this->getInitials() ) ) { + if (StringScript::isBengali($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Bengali-Regular.ttf'; } // Georgian - if ( StringScript::isGeorgian( $this->getInitials() ) ) { + if (StringScript::isGeorgian($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Georgian-Regular.ttf'; } // Hebrew - if ( StringScript::isHebrew( $this->getInitials() ) ) { + if (StringScript::isHebrew($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Hebrew-Regular.ttf'; } // Mongolian - if ( StringScript::isMongolian( $this->getInitials() ) ) { + if (StringScript::isMongolian($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Mongolian-Regular.ttf'; } // Thai - if ( StringScript::isThai( $this->getInitials() ) ) { + if (StringScript::isThai($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Thai-Regular.ttf'; } // Tibetan - if ( StringScript::isTibetan( $this->getInitials() ) ) { + if (StringScript::isTibetan($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-Tibetan-Regular.ttf'; } // Chinese & Japanese - if ( StringScript::isJapanese( $this->getInitials() ) || StringScript::isChinese( $this->getInitials() ) ) { + if (StringScript::isJapanese($this->getInitials()) || StringScript::isChinese($this->getInitials())) { return __DIR__ . '/fonts/script/Noto-CJKJP-Regular.otf'; } From 08187411e9beae212b6063c40f42ed02f97aa6ba Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 01:17:58 -0400 Subject: [PATCH 7/9] Add some test for more autoColors --- tests/AutoColorUtilsTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/AutoColorUtilsTest.php diff --git a/tests/AutoColorUtilsTest.php b/tests/AutoColorUtilsTest.php new file mode 100644 index 0000000..a82e013 --- /dev/null +++ b/tests/AutoColorUtilsTest.php @@ -0,0 +1,29 @@ +name('A')->autoColor(); + $this->assertEquals('#f0a742', $avatar->getBackgroundColor()); + $this->assertEquals('#000000', $avatar->getColor()); + + $avatar->name('B')->autoColor(); + $this->assertEquals('#42caf0', $avatar->getBackgroundColor()); + $this->assertEquals('#000000', $avatar->getColor()); + + $avatar->name('C')->autoColor(); + $this->assertEquals('#42f085', $avatar->getBackgroundColor()); + $this->assertEquals('#000000', $avatar->getColor()); + + $avatar->name('D')->autoColor(); + $this->assertEquals('#f04293', $avatar->getBackgroundColor()); + $this->assertEquals('#ffffff', $avatar->getColor()); + } +} From 3142ba49457b33cc4587ffea7a478ba50c42f295 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2020 01:28:24 -0400 Subject: [PATCH 8/9] Is this what the StyleCI wants? --- src/InitialAvatar.php | 113 +++++++++++++++++------------------ tests/AutoColorUtilsTest.php | 2 +- 2 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/InitialAvatar.php b/src/InitialAvatar.php index 4bcfbcc..910dc81 100644 --- a/src/InitialAvatar.php +++ b/src/InitialAvatar.php @@ -42,21 +42,21 @@ class InitialAvatar protected $preferBold = false; /** - * Language eg.en zh-CN + * Language eg.en zh-CN. * * @var string */ protected $language = 'en'; /** - * Role translator + * Role translator. * * @var Base */ protected $translator; /** - * Language related to translator + * Language related to translator. * * @var array */ @@ -72,7 +72,7 @@ public function __construct() } /** - * Create a ImageManager instance + * Create a ImageManager instance. */ protected function setupImageManager() { @@ -89,14 +89,14 @@ protected function setupImageManager() public function name($nameOrInitials) { $nameOrInitials = $this->translate($nameOrInitials); - $this->name = $nameOrInitials; + $this->name = $nameOrInitials; $this->initials_generator->name($nameOrInitials); return $this; } /** - * Transforms a unicode string to the proper format + * Transforms a unicode string to the proper format. * * @param string $char the code to be converted (e.g., f007 would mean the "user" symbol) * @@ -133,7 +133,7 @@ public function length($length = 2) */ public function size($size) { - $this->width = (int) $size; + $this->width = (int) $size; $this->height = (int) $size; return $this; @@ -168,7 +168,7 @@ public function width($width) } /** - * Prefer bold fonts (if possible) + * Prefer bold fonts (if possible). * * @return $this */ @@ -180,7 +180,7 @@ public function preferBold() } /** - * Prefer regular fonts (if possible) + * Prefer regular fonts (if possible). * * @return $this */ @@ -220,16 +220,15 @@ public function color($color) } /** - * Automatically set a font and/or background color based on the supplied name - * + * Automatically set a font and/or background color based on the supplied name. + * * @param bool $foreground * @param bool $background - * + * * @return $this */ public function autoColor(bool $foreground = true, bool $background = true, int $saturation = 85, int $luminance = 60) { - $hue = (crc32($this->name) % 360) / 360; $saturation /= 100; $luminance /= 100; @@ -255,7 +254,7 @@ public function font($font) } /** - * Set the font name + * Set the font name. * * Example: "Open Sans" * @@ -356,7 +355,7 @@ public function keepCase($keepCase = true) } /** - * Set if should allow (or remove) special characters + * Set if should allow (or remove) special characters. * * @param bool $allowSpecialCharacters * @@ -394,7 +393,7 @@ public function fontSize($size = 0.5) public function generate($name = null) { if ($name !== null) { - $this->name = $name; + $this->name = $name; $this->generated_initials = $this->initials_generator->keepCase($this->getKeepCase()) ->allowSpecialCharacters($this->getAllowSpecialCharacters()) ->generate($name); @@ -413,7 +412,7 @@ public function generate($name = null) public function generateSvg($name = null) { if ($name !== null) { - $this->name = $name; + $this->name = $name; $this->generated_initials = $this->initials_generator->keepCase($this->getKeepCase()) ->allowSpecialCharacters($this->getAllowSpecialCharacters()) ->generate($name); @@ -546,7 +545,7 @@ public function getHeight() /** * Will return the keepCase parameter. * - * @return boolean + * @return bool */ public function getKeepCase() { @@ -556,7 +555,7 @@ public function getKeepCase() /** * Will return the allowSpecialCharacters parameter. * - * @return boolean + * @return bool */ public function getAllowSpecialCharacters() { @@ -578,7 +577,7 @@ public function getAutoFont() * ```php * $avatar->language('en')->name('Mr Green'); // Right * $avatar->name('Mr Green')->language('en'); // Wrong - * ``` + * ```. * * @param string $language * @@ -592,15 +591,15 @@ public function language($language) } /** - * Add new translators designed by user + * Add new translators designed by user.. * * @param array $translatorMap - * ```php - * $translatorMap = [ - * 'fr' => 'foo\bar\Fr', - * 'zh-TW' => 'foo\bar\ZhTW' - * ]; - * ``` + * ```php + * $translatorMap = [ + * 'fr' => 'foo\bar\Fr', + * 'zh-TW' => 'foo\bar\ZhTW' + * ]; + * ``` * * @return $this */ @@ -611,16 +610,13 @@ public function addTranslators($translatorMap) return $this; } - /** - * @inheritdoc - */ protected function translate($nameOrInitials) { return $this->getTranslator()->translate($nameOrInitials); } /** - * Instance the translator by language + * Instance the translator by language. * * @return Base */ @@ -642,12 +638,12 @@ protected function getTranslator() */ protected function makeAvatar($image) { - $width = $this->getWidth(); - $height = $this->getHeight(); - $bgColor = $this->getBackgroundColor(); - $name = $this->getInitials(); + $width = $this->getWidth(); + $height = $this->getHeight(); + $bgColor = $this->getBackgroundColor(); + $name = $this->getInitials(); $fontFile = $this->findFontFile(); - $color = $this->getColor(); + $color = $this->getColor(); $fontSize = $this->getFontSize(); if ($this->getRounded() && $this->getSmooth()) { @@ -684,7 +680,7 @@ protected function makeAvatar($image) protected function makeSvgAvatar() { // Original document - $image = new SVG($this->getWidth(), $this->getHeight()); + $image = new SVG($this->getWidth(), $this->getHeight()); $document = $image->getDocument(); // Background @@ -745,12 +741,12 @@ protected function findFontFile() return $fontFile; } - if (file_exists(__DIR__ . $fontFile)) { - return __DIR__ . $fontFile; + if (file_exists(__DIR__.$fontFile)) { + return __DIR__.$fontFile; } - if (file_exists(__DIR__ . '/' . $fontFile)) { - return __DIR__ . '/' . $fontFile; + if (file_exists(__DIR__.'/'.$fontFile)) { + return __DIR__.'/'.$fontFile; } } @@ -761,54 +757,54 @@ protected function getFontByScript() { // Arabic if (StringScript::isArabic($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Arabic-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Arabic-Regular.ttf'; } // Armenian if (StringScript::isArmenian($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Armenian-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Armenian-Regular.ttf'; } // Bengali if (StringScript::isBengali($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Bengali-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Bengali-Regular.ttf'; } // Georgian if (StringScript::isGeorgian($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Georgian-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Georgian-Regular.ttf'; } // Hebrew if (StringScript::isHebrew($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Hebrew-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Hebrew-Regular.ttf'; } // Mongolian if (StringScript::isMongolian($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Mongolian-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Mongolian-Regular.ttf'; } // Thai if (StringScript::isThai($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Thai-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Thai-Regular.ttf'; } // Tibetan if (StringScript::isTibetan($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-Tibetan-Regular.ttf'; + return __DIR__.'/fonts/script/Noto-Tibetan-Regular.ttf'; } // Chinese & Japanese if (StringScript::isJapanese($this->getInitials()) || StringScript::isChinese($this->getInitials())) { - return __DIR__ . '/fonts/script/Noto-CJKJP-Regular.otf'; + return __DIR__.'/fonts/script/Noto-CJKJP-Regular.otf'; } return $this->getFontFile(); } /** - * Convert HSL color value produced by autoColor() to RGB value expected by image driver + * Convert HSL color value produced by autoColor() to RGB value expected by image driver. */ protected function convertHSLtoRGB($h, $s, $l, $toHex = true) { @@ -868,9 +864,10 @@ protected function convertHSLtoRGB($h, $s, $l, $toHex = true) $blue = round($blue * 255, 0); if ($toHex) { - $red = ($red < 15) ? '0' . dechex($red) : dechex($red); - $green = ($green < 15) ? '0' . dechex($green) : dechex($green); - $blue = ($blue < 15) ? '0' . dechex($blue) : dechex($blue); + $red = ($red < 15) ? '0'.dechex($red) : dechex($red); + $green = ($green < 15) ? '0'.dechex($green) : dechex($green); + $blue = ($blue < 15) ? '0'.dechex($blue) : dechex($blue); + return "#{$red}{$green}{$blue}"; } else { return ['red' => $red, 'green' => $green, 'blue' => $blue]; @@ -878,7 +875,7 @@ protected function convertHSLtoRGB($h, $s, $l, $toHex = true) } /** - * Get contrasting foreground color for autoColor background + * Get contrasting foreground color for autoColor background. */ protected function getContrastColor($hexColor) { @@ -889,7 +886,7 @@ protected function getContrastColor($hexColor) $B1 = hexdec(substr($hexColor, 5, 2)); // Black RGB - $blackColor = "#000000"; + $blackColor = '#000000'; $R2BlackColor = hexdec(substr($blackColor, 1, 2)); $G2BlackColor = hexdec(substr($blackColor, 3, 2)); $B2BlackColor = hexdec(substr($blackColor, 5, 2)); @@ -905,9 +902,9 @@ protected function getContrastColor($hexColor) $contrastRatio = 0; if ($L1 > $L2) { - $contrastRatio = (int)(($L1 + 0.05) / ($L2 + 0.05)); + $contrastRatio = (int) (($L1 + 0.05) / ($L2 + 0.05)); } else { - $contrastRatio = (int)(($L2 + 0.05) / ($L1 + 0.05)); + $contrastRatio = (int) (($L2 + 0.05) / ($L1 + 0.05)); } // If contrast is more than 5, return black color diff --git a/tests/AutoColorUtilsTest.php b/tests/AutoColorUtilsTest.php index a82e013..7b0004c 100644 --- a/tests/AutoColorUtilsTest.php +++ b/tests/AutoColorUtilsTest.php @@ -24,6 +24,6 @@ public function can_create_all_colors() $avatar->name('D')->autoColor(); $this->assertEquals('#f04293', $avatar->getBackgroundColor()); - $this->assertEquals('#ffffff', $avatar->getColor()); + $this->assertEquals('#FFFFFF', $avatar->getColor()); } } From 51294d17a95cb5b8c2ab7cd52fd0346764e57725 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 26 Jul 2023 13:37:29 +0000 Subject: [PATCH 9/9] Apply fixes from StyleCI --- src/InitialAvatar.php | 1 - src/Translator/Base.php | 7 +- src/Translator/En.php | 2 +- src/Translator/ZhCN.php | 6 +- tests/GenerateTest.php | 173 ++++++++++++++------------ tests/ImageSizeTest.php | 30 ++--- tests/InitialGenerationTest.php | 80 ++++++------ tests/LimitInitialLenghtTest.php | 123 +++++++++--------- tests/ScriptLanguageDetectionTest.php | 135 ++++++++++---------- 9 files changed, 297 insertions(+), 260 deletions(-) diff --git a/src/InitialAvatar.php b/src/InitialAvatar.php index 910dc81..83d59a5 100644 --- a/src/InitialAvatar.php +++ b/src/InitialAvatar.php @@ -879,7 +879,6 @@ protected function convertHSLtoRGB($h, $s, $l, $toHex = true) */ protected function getContrastColor($hexColor) { - // hexColor RGB $R1 = hexdec(substr($hexColor, 1, 2)); $G1 = hexdec(substr($hexColor, 3, 2)); diff --git a/src/Translator/Base.php b/src/Translator/Base.php index 67b1027..6897118 100644 --- a/src/Translator/Base.php +++ b/src/Translator/Base.php @@ -5,17 +5,18 @@ interface Base { /** - * Translate words to english + * Translate words to english. * * @param string $words + * * @return mixed */ public function translate($words); /** - * Get the source language of translator + * Get the source language of translator. * * @return string */ public function getSourceLanguage(); -} \ No newline at end of file +} diff --git a/src/Translator/En.php b/src/Translator/En.php index e65b5d3..6e43909 100644 --- a/src/Translator/En.php +++ b/src/Translator/En.php @@ -19,4 +19,4 @@ public function getSourceLanguage() { return 'en'; } -} \ No newline at end of file +} diff --git a/src/Translator/ZhCN.php b/src/Translator/ZhCN.php index 80571d4..9868aed 100644 --- a/src/Translator/ZhCN.php +++ b/src/Translator/ZhCN.php @@ -7,14 +7,14 @@ class ZhCN implements Base { /** - * Inherent instance of zh-CN translator + * Inherent instance of zh-CN translator. * * @var Pinyin */ protected $inherent; /** - * ZhCN constructor, set the instance of PinYin + * ZhCN constructor, set the instance of PinYin. */ public function __construct() { @@ -36,4 +36,4 @@ public function getSourceLanguage() { return 'zh-CN'; } -} \ No newline at end of file +} diff --git a/tests/GenerateTest.php b/tests/GenerateTest.php index 3939430..14722fc 100644 --- a/tests/GenerateTest.php +++ b/tests/GenerateTest.php @@ -5,121 +5,134 @@ class GenerateTest extends TestCase { - /** @test */ - public function CanGenerateInitialsWithoutNameParameter() { - $avatar = new InitialAvatar(); + /** @test */ + public function CanGenerateInitialsWithoutNameParameter() + { + $avatar = new InitialAvatar(); - $avatar->generate( 'Lasse Rafn' ); + $avatar->generate('Lasse Rafn'); - $this->assertEquals( 'LR', $avatar->getInitials() ); - } + $this->assertEquals('LR', $avatar->getInitials()); + } - /** @test */ - public function returns_image_object() { - $avatar = new InitialAvatar(); + /** @test */ + public function returns_image_object() + { + $avatar = new InitialAvatar(); - $image = $avatar->generate(); + $image = $avatar->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function returns_image_object_with_emoji() { - $avatar = new InitialAvatar(); + /** @test */ + public function returns_image_object_with_emoji() + { + $avatar = new InitialAvatar(); - $image = $avatar->generate( '😅' ); + $image = $avatar->generate('😅'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function returns_image_object_with_japanese_letters() { - $avatar = new InitialAvatar(); + /** @test */ + public function returns_image_object_with_japanese_letters() + { + $avatar = new InitialAvatar(); - $image = $avatar->font( __DIR__ . '/fonts/NotoSans-Regular.otf' )->generate( 'こんにちは' ); + $image = $avatar->font(__DIR__.'/fonts/NotoSans-Regular.otf')->generate('こんにちは'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function returns_image_object_with_default_gd_wont() { - $avatar = new InitialAvatar(); + /** @test */ + public function returns_image_object_with_default_gd_wont() + { + $avatar = new InitialAvatar(); - $image = $avatar->font( 2 )->gd()->generate( 'LR' ); + $image = $avatar->font(2)->gd()->generate('LR'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_use_imagick_driver() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_use_imagick_driver() + { + $avatar = new InitialAvatar(); - $image = $avatar->imagick()->generate( 'LR' ); + $image = $avatar->imagick()->generate('LR'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_use_gd_driver() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_use_gd_driver() + { + $avatar = new InitialAvatar(); - $image = $avatar->gd()->generate( 'LR' ); + $image = $avatar->gd()->generate('LR'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_make_rounded_images() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_make_rounded_images() + { + $avatar = new InitialAvatar(); - $image = $avatar->rounded()->generate(); + $image = $avatar->rounded()->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function can_make_a_smooth_rounded_image() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_make_a_smooth_rounded_image() + { + $avatar = new InitialAvatar(); - $image = $avatar->rounded()->smooth()->generate(); + $image = $avatar->rounded()->smooth()->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function stream_is_readable() { - $avatar = new InitialAvatar(); + /** @test */ + public function stream_is_readable() + { + $avatar = new InitialAvatar(); - $this->assertTrue( $avatar->generate()->stream()->isReadable() ); - } + $this->assertTrue($avatar->generate()->stream()->isReadable()); + } - /** @test */ - public function can_use_local_font() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_use_local_font() + { + $avatar = new InitialAvatar(); - $image = $avatar->font( __DIR__ . '/fonts/NotoSans-Regular.ttf' )->generate(); + $image = $avatar->font(__DIR__.'/fonts/NotoSans-Regular.ttf')->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function has_a_font_fallback() { - $avatar = new InitialAvatar(); + /** @test */ + public function has_a_font_fallback() + { + $avatar = new InitialAvatar(); - $image = $avatar->font( 'no-font' )->generate(); + $image = $avatar->font('no-font')->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } - /** @test */ - public function can_handle_fonts_without_slash_first() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_handle_fonts_without_slash_first() + { + $avatar = new InitialAvatar(); - $image = $avatar->font( 'fonts/NotoSans-Regular.ttf' )->generate(); + $image = $avatar->font('fonts/NotoSans-Regular.ttf')->generate(); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + } } diff --git a/tests/ImageSizeTest.php b/tests/ImageSizeTest.php index 57cd710..53cf926 100644 --- a/tests/ImageSizeTest.php +++ b/tests/ImageSizeTest.php @@ -5,23 +5,25 @@ class ImageSizeTest extends TestCase { - /** @test */ - public function can_set_image_size_to_50_pixels() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_set_image_size_to_50_pixels() + { + $avatar = new InitialAvatar(); - $avatar->size( 50 ); + $avatar->size(50); - $this->assertEquals( 50, $avatar->generate()->getWidth() ); - $this->assertEquals( 50, $avatar->generate()->getHeight() ); - } + $this->assertEquals(50, $avatar->generate()->getWidth()); + $this->assertEquals(50, $avatar->generate()->getHeight()); + } - /** @test */ - public function can_set_image_size_to_100_pixels() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_set_image_size_to_100_pixels() + { + $avatar = new InitialAvatar(); - $avatar->size( 100 ); + $avatar->size(100); - $this->assertEquals( 100, $avatar->generate()->getWidth() ); - $this->assertEquals( 100, $avatar->generate()->getHeight() ); - } + $this->assertEquals(100, $avatar->generate()->getWidth()); + $this->assertEquals(100, $avatar->generate()->getHeight()); + } } diff --git a/tests/InitialGenerationTest.php b/tests/InitialGenerationTest.php index 79723ec..4d3b4be 100644 --- a/tests/InitialGenerationTest.php +++ b/tests/InitialGenerationTest.php @@ -5,58 +5,64 @@ class InitialGenerationTest extends TestCase { - /** @test */ - public function initials_are_generated_from_full_name() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_from_full_name() + { + $avatar = new InitialAvatar(); - $avatar->name( 'John Doe' ); + $avatar->name('John Doe'); - $this->assertEquals( 'JD', $avatar->getInitials() ); - } + $this->assertEquals('JD', $avatar->getInitials()); + } - /** @test */ - public function initials_are_generated_from_single_name() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_from_single_name() + { + $avatar = new InitialAvatar(); - $avatar->name( 'John' ); + $avatar->name('John'); - $this->assertEquals( 'JO', $avatar->getInitials() ); - } + $this->assertEquals('JO', $avatar->getInitials()); + } - /** @test */ - public function initials_are_generated_from_initals() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_from_initals() + { + $avatar = new InitialAvatar(); - $avatar->name( 'MA' ); + $avatar->name('MA'); - $this->assertEquals( 'MA', $avatar->getInitials() ); - } + $this->assertEquals('MA', $avatar->getInitials()); + } - /** @test */ - public function initials_are_generated_from_three_names() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_from_three_names() + { + $avatar = new InitialAvatar(); - $avatar->name( 'John Doe Bergerson' ); + $avatar->name('John Doe Bergerson'); - $this->assertEquals( 'JB', $avatar->getInitials() ); - } + $this->assertEquals('JB', $avatar->getInitials()); + } - /** @test */ - public function initials_are_generated_with_dialect_specific_letters() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_with_dialect_specific_letters() + { + $avatar = new InitialAvatar(); - $avatar->name( 'Gustav Årgonson' ); + $avatar->name('Gustav Årgonson'); - $this->assertEquals( 'GÅ', $avatar->getInitials() ); - } + $this->assertEquals('GÅ', $avatar->getInitials()); + } - /** @test */ - public function initials_are_generated_from_name() { - $avatar = new InitialAvatar(); + /** @test */ + public function initials_are_generated_from_name() + { + $avatar = new InitialAvatar(); - $avatar->name( 'Chanel Butterman' ); + $avatar->name('Chanel Butterman'); - $this->assertNotEquals( 'AB', $avatar->getInitials() ); - $this->assertEquals( 'CB', $avatar->getInitials() ); - } + $this->assertNotEquals('AB', $avatar->getInitials()); + $this->assertEquals('CB', $avatar->getInitials()); + } } diff --git a/tests/LimitInitialLenghtTest.php b/tests/LimitInitialLenghtTest.php index 9528e3d..15326c3 100644 --- a/tests/LimitInitialLenghtTest.php +++ b/tests/LimitInitialLenghtTest.php @@ -5,85 +5,92 @@ class LimitInitialLenghtTest extends TestCase { + /** @test */ + public function can_limit_length_to_one_letter() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_one_letter() { - $avatar = new InitialAvatar(); + $avatar->name('John Doe')->length(1); - $avatar->name( 'John Doe' )->length( 1 ); + $this->assertEquals('J', $avatar->getInitials()); + $this->assertEquals(1, strlen($avatar->getInitials())); + } - $this->assertEquals( 'J', $avatar->getInitials() ); - $this->assertEquals( 1, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_two_letters() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_two_letters() { - $avatar = new InitialAvatar(); + $avatar->name('John Doe')->length(2); - $avatar->name( 'John Doe' )->length( 2 ); + $this->assertEquals('JD', $avatar->getInitials()); + $this->assertEquals(2, strlen($avatar->getInitials())); + } - $this->assertEquals( 'JD', $avatar->getInitials() ); - $this->assertEquals( 2, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_three_letters() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_three_letters() { - $avatar = new InitialAvatar(); + $avatar->name('John Doe Johnson')->length(3); - $avatar->name( 'John Doe Johnson' )->length( 3 ); + $this->assertEquals('JDJ', $avatar->getInitials()); + $this->assertEquals(3, strlen($avatar->getInitials())); + } - $this->assertEquals( 'JDJ', $avatar->getInitials() ); - $this->assertEquals( 3, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_three_letters_from_two_names() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_three_letters_from_two_names() { - $avatar = new InitialAvatar(); + $avatar->name('John Doe')->length(3); - $avatar->name( 'John Doe' )->length( 3 ); + $this->assertEquals('JDO', $avatar->getInitials()); + $this->assertEquals(3, strlen($avatar->getInitials())); + } - $this->assertEquals( 'JDO', $avatar->getInitials() ); - $this->assertEquals( 3, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_four_letters_from_two_names() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_four_letters_from_two_names() { - $avatar = new InitialAvatar(); + $avatar->name('John Doe')->length(4); - $avatar->name( 'John Doe' )->length( 4 ); + $this->assertEquals('JDOE', $avatar->getInitials()); + $this->assertEquals(4, strlen($avatar->getInitials())); + } - $this->assertEquals( 'JDOE', $avatar->getInitials() ); - $this->assertEquals( 4, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function cannot_limit_length_to_five_letters_with_only_one_name_of_4_letters() + { + // This is not possible, so it will end in 4 letters + $avatar = new InitialAvatar(); - /** @test */ - public function cannot_limit_length_to_five_letters_with_only_one_name_of_4_letters() { - // This is not possible, so it will end in 4 letters - $avatar = new InitialAvatar(); + $avatar->name('John')->length(5); - $avatar->name( 'John' )->length( 5 ); + $this->assertEquals('JOHN', $avatar->getInitials()); + $this->assertEquals(4, strlen($avatar->getInitials())); + } - $this->assertEquals( 'JOHN', $avatar->getInitials() ); - $this->assertEquals( 4, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_five_letters_with_one_name_of_5_letters() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_five_letters_with_one_name_of_5_letters() { - $avatar = new InitialAvatar(); + $avatar->name('Lasse')->length(5); - $avatar->name( 'Lasse' )->length( 5 ); + $this->assertEquals('LASSE', $avatar->getInitials()); + $this->assertEquals(5, strlen($avatar->getInitials())); + } - $this->assertEquals( 'LASSE', $avatar->getInitials() ); - $this->assertEquals( 5, strlen( $avatar->getInitials() ) ); - } + /** @test */ + public function can_limit_length_to_one_letter_from_one_letter_name() + { + $avatar = new InitialAvatar(); - /** @test */ - public function can_limit_length_to_one_letter_from_one_letter_name() { - $avatar = new InitialAvatar(); + $avatar->name('L')->length(1); - $avatar->name( 'L' )->length( 1 ); - - $this->assertEquals( 'L', $avatar->getInitials() ); - $this->assertEquals( 1, strlen( $avatar->getInitials() ) ); - } + $this->assertEquals('L', $avatar->getInitials()); + $this->assertEquals(1, strlen($avatar->getInitials())); + } } diff --git a/tests/ScriptLanguageDetectionTest.php b/tests/ScriptLanguageDetectionTest.php index 2cdf27a..0064842 100644 --- a/tests/ScriptLanguageDetectionTest.php +++ b/tests/ScriptLanguageDetectionTest.php @@ -5,93 +5,102 @@ class ScriptLanguageDetectionTest extends TestCase { - /** @test */ - public function can_detect_and_use_script_Arabic() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Arabic() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'الحزمة' ); + $image = $avatar->autoFont()->generate('الحزمة'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Armenian() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Armenian() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'բենգիմžē' ); + $image = $avatar->autoFont()->generate('բենգիմžē'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Bengali() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Bengali() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'ǰǰô জ' ); + $image = $avatar->autoFont()->generate('ǰǰô জ'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Georgian() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Georgian() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'გამარჯობა' ); + $image = $avatar->autoFont()->generate('გამარჯობა'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Hebrew() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Hebrew() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'ה ו ז ח ט' ); + $image = $avatar->autoFont()->generate('ה ו ז ח ט'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Mongolian() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Mongolian() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'ᠪᠣᠯᠠᠢ᠃' ); + $image = $avatar->autoFont()->generate('ᠪᠣᠯᠠᠢ᠃'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Thai() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Thai() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'สวัสดีชาวโลกและยินดีต้อนรับแพ็กเกจนี้' ); + $image = $avatar->autoFont()->generate('สวัสดีชาวโลกและยินดีต้อนรับแพ็กเกจนี้'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Tibetan() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Tibetan() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'ཀཁཆཇའ' ); + $image = $avatar->autoFont()->generate('ཀཁཆཇའ'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } - /** @test */ - public function can_detect_and_use_script_Uncommon() { - $avatar = new InitialAvatar(); + /** @test */ + public function can_detect_and_use_script_Uncommon() + { + $avatar = new InitialAvatar(); - $image = $avatar->autoFont()->generate( 'ψψ' ); + $image = $avatar->autoFont()->generate('ψψ'); - $this->assertEquals( 'Intervention\Image\Image', get_class( $image ) ); - $this->assertTrue( $image->stream()->isReadable() ); - } + $this->assertEquals('Intervention\Image\Image', get_class($image)); + $this->assertTrue($image->stream()->isReadable()); + } }