Skip to content

Commit 1e94483

Browse files
committed
Font size option added
1 parent 737d2a8 commit 1e94483

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ You can simply use ->cache() and it will set cache to 60 minutes, but you can al
8787
$image = $avatar->name('John Doe Johnson')->length(3)->generate(); // 3 letters = JDJ
8888
````
8989

90+
### Font Size - default: 0.5
91+
````php
92+
$image = $avatar->fontSize(0.25)->generate(); // Font will be 25% of image size.
93+
````
94+
If the Image size is 50px and fontSize is 0.5, the font size will be 25px.
95+
9096
## Chaining it all together
9197
We will not use the ->font() method in this example; as I like the regular one.
9298

9399
````php
94100
return $avatar->name('Lasse Rafn')
95101
->length(2)
102+
->fontSize(0.5)
96103
->size(96) // 48 * 2
97104
->background('#8BC34A')
98105
->color('#fff')

src/InitialAvatar.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class InitialAvatar
1111

1212
private $parameter_cacheTime = 0;
1313
private $parameter_length = 2;
14+
private $parameter_fontSize = 0.5;
1415
private $parameter_initials = 'JD';
1516
private $parameter_name = 'John Doe';
1617
private $parameter_size = 48;
@@ -74,6 +75,13 @@ public function cache( int $minutes = 60 ): self
7475
return $this;
7576
}
7677

78+
public function fontSize( float $size = 0.5 ): self
79+
{
80+
$this->parameter_fontSize = (float) $size;
81+
82+
return $this;
83+
}
84+
7785
/**
7886
* Generate the image
7987
*
@@ -88,18 +96,19 @@ public function generate( $name = null ): Image
8896
$this->parameter_initials = $this->generateInitials( $this->parameter_name );
8997
}
9098

91-
$fontFile = $this->parameter_fontFile;
9299
$size = $this->parameter_size;
93-
$color = $this->parameter_fontColor;
94100
$bgColor = $this->parameter_bgColor;
95101
$name = $this->parameter_initials;
102+
$fontFile = $this->parameter_fontFile;
103+
$color = $this->parameter_fontColor;
104+
$fontSize = $this->parameter_fontSize;
96105

97-
$img = $this->image->cache( function ( ImageCache $image ) use ( $size, $bgColor, $color, $fontFile, $name )
106+
$img = $this->image->cache( function ( ImageCache $image ) use ( $size, $bgColor, $color, $fontFile, $name, $fontSize )
98107
{
99-
$image->canvas( $size, $size, $bgColor )->text( $name, $size / 2, $size / 2, function ( $font ) use ( $size, $color, $fontFile )
108+
$image->canvas( $size, $size, $bgColor )->text( $name, $size / 2, $size / 2, function ( $font ) use ( $size, $color, $fontFile, $fontSize )
100109
{
101110
$font->file( __DIR__ . $fontFile );
102-
$font->size( $size / 2 );
111+
$font->size( $size * $fontSize );
103112
$font->color( $color );
104113
$font->align( 'center' );
105114
$font->valign( 'center' );

0 commit comments

Comments
 (0)