Skip to content

Commit a46ab8f

Browse files
committed
Update intervention/image to v3
1 parent 1a63213 commit a46ab8f

File tree

5 files changed

+59
-35
lines changed

5 files changed

+59
-35
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.0|^7.1|^7.2|^7.3|^7.4|^8.0|^8.1|^8.2",
19+
"php": "^8.1",
2020
"ext-json": "*",
21-
"intervention/image": "^2.3",
21+
"intervention/image": "^3.11",
2222
"lasserafn/php-initials": "^3.0",
2323
"lasserafn/php-string-script-language": "^0.4",
2424
"meyfa/php-svg": "^0.9.0",

src/InitialAvatar.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace LasseRafn\InitialAvatarGenerator;
44

5+
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
6+
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
7+
use Intervention\Image\Geometry\Factories\CircleFactory;
58
use Intervention\Image\Image;
69
use Intervention\Image\ImageManager;
710
use LasseRafn\InitialAvatarGenerator\Translator\Base;
@@ -10,6 +13,7 @@
1013
use LasseRafn\InitialAvatarGenerator\Translator\ZhCN;
1114
use LasseRafn\Initials\Initials;
1215
use LasseRafn\StringScript;
16+
use RuntimeException;
1317
use SVG\Nodes\Shapes\SVGCircle;
1418
use SVG\Nodes\Shapes\SVGRect;
1519
use SVG\Nodes\Structures\SVGFont;
@@ -78,7 +82,15 @@ public function __construct()
7882
*/
7983
protected function setupImageManager()
8084
{
81-
$this->image = new ImageManager(['driver' => $this->getDriver()]);
85+
$driver = $this->getDriver();
86+
87+
$driverInstance = match ($driver) {
88+
'gd' => new GdDriver(),
89+
'imagick' => new ImagickDriver(),
90+
default => throw new RuntimeException("Unsupported driver."),
91+
};
92+
93+
$this->image = new ImageManager($driverInstance);
8294
}
8395

8496
/**
@@ -264,9 +276,9 @@ public function autoColor(bool $foreground = true, bool $background = true, int
264276
}
265277

266278
/**
267-
* Set the font file by path or int (1-5).
279+
* Set the font file by path.
268280
*
269-
* @param string|int $font
281+
* @param string $font
270282
*
271283
* @return $this
272284
*/
@@ -517,7 +529,7 @@ public function getFontSize()
517529
/**
518530
* Will return the font file parameter.
519531
*
520-
* @return string|int
532+
* @return string
521533
*/
522534
public function getFontFile()
523535
{
@@ -691,12 +703,21 @@ protected function makeAvatar($image)
691703
$height *= 5;
692704
}
693705

694-
$avatar = $image->canvas($width, $height, !$this->getRounded() ? $bgColor : null);
706+
$avatar = $image->create($width, $height);
707+
708+
if (!$this->getRounded()) {
709+
$avatar = $avatar->fill($bgColor);
710+
}
695711

696712
if ($this->getRounded()) {
697-
$avatar = $avatar->circle($width - 2, $width / 2, $height / 2, function ($draw) use ($bgColor) {
698-
return $draw->background($bgColor);
699-
});
713+
$avatar = $avatar->drawCircle(
714+
x: $width / 2,
715+
y: $height / 2,
716+
init: function (CircleFactory $circle) use ($width, $bgColor) {
717+
$circle->radius($width -2);
718+
$circle->background($bgColor);
719+
}
720+
);
700721
}
701722

702723
if ($this->getRounded() && $this->getSmooth()) {
@@ -706,7 +727,10 @@ protected function makeAvatar($image)
706727
}
707728

708729
return $avatar->text($name, $width / 2, $height / 2, function ($draw) use ($width, $color, $fontFile, $fontSize) {
709-
$draw->file($fontFile);
730+
if ($fontFile !== null) {
731+
$draw->filename($fontFile);
732+
}
733+
710734
$draw->size($width * $fontSize);
711735
$draw->color($color);
712736
$draw->align('center');
@@ -755,6 +779,10 @@ protected function makeSvgAvatar()
755779
return $image;
756780
}
757781

782+
783+
/**
784+
* @return string|null
785+
*/
758786
protected function findFontFile()
759787
{
760788
$fontFile = $this->getFontFile();
@@ -763,10 +791,6 @@ protected function findFontFile()
763791
$fontFile = $this->getFontByScript();
764792
}
765793

766-
if (is_int($fontFile) && \in_array($fontFile, [1, 2, 3, 4, 5], false)) {
767-
return $fontFile;
768-
}
769-
770794
$weightsToTry = ['Regular'];
771795

772796
if ($this->preferBold) {
@@ -791,7 +815,7 @@ protected function findFontFile()
791815
}
792816
}
793817

794-
return 1;
818+
return null;
795819
}
796820

797821
protected function getFontByScript()

tests/GenerateTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function returns_image_object_with_default_gd_wont()
5050
{
5151
$avatar = new InitialAvatar();
5252

53-
$image = $avatar->font(2)->gd()->generate('LR');
53+
$image = $avatar->gd()->generate('LR');
5454

5555
$this->assertEquals('Intervention\Image\Image', get_class($image));
56-
$this->assertTrue($image->stream()->isReadable());
56+
$this->assertNotEmpty($image->toJpeg()->size());
5757
}
5858

5959
/** @test */
@@ -64,7 +64,7 @@ public function can_use_imagick_driver()
6464
$image = $avatar->imagick()->generate('LR');
6565

6666
$this->assertEquals('Intervention\Image\Image', get_class($image));
67-
$this->assertTrue($image->stream()->isReadable());
67+
$this->assertNotEmpty($image->toJpeg()->size());
6868
}
6969

7070
/** @test */
@@ -75,7 +75,7 @@ public function can_use_gd_driver()
7575
$image = $avatar->gd()->generate('LR');
7676

7777
$this->assertEquals('Intervention\Image\Image', get_class($image));
78-
$this->assertTrue($image->stream()->isReadable());
78+
$this->assertNotEmpty($image->toJpeg()->size());
7979
}
8080

8181
/** @test */
@@ -103,7 +103,7 @@ public function stream_is_readable()
103103
{
104104
$avatar = new InitialAvatar();
105105

106-
$this->assertTrue($avatar->generate()->stream()->isReadable());
106+
$this->assertNotEmpty($avatar->generate()->toJpeg()->size());
107107
}
108108

109109
/** @test */

tests/ImageSizeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public function can_set_image_size_to_50_pixels()
1212

1313
$avatar->size(50);
1414

15-
$this->assertEquals(50, $avatar->generate()->getWidth());
16-
$this->assertEquals(50, $avatar->generate()->getHeight());
15+
$this->assertEquals(50, $avatar->generate()->width());
16+
$this->assertEquals(50, $avatar->generate()->height());
1717
}
1818

1919
/** @test */
@@ -23,7 +23,7 @@ public function can_set_image_size_to_100_pixels()
2323

2424
$avatar->size(100);
2525

26-
$this->assertEquals(100, $avatar->generate()->getWidth());
27-
$this->assertEquals(100, $avatar->generate()->getHeight());
26+
$this->assertEquals(100, $avatar->generate()->width());
27+
$this->assertEquals(100, $avatar->generate()->height());
2828
}
2929
}

tests/ScriptLanguageDetectionTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function can_detect_and_use_script_Arabic()
1313
$image = $avatar->autoFont()->generate('الحزمة');
1414

1515
$this->assertEquals('Intervention\Image\Image', get_class($image));
16-
$this->assertTrue($image->stream()->isReadable());
16+
$this->assertNotEmpty($image->toJpeg()->size());
1717
}
1818

1919
/** @test */
@@ -24,7 +24,7 @@ public function can_detect_and_use_script_Armenian()
2424
$image = $avatar->autoFont()->generate('բենգիմžē');
2525

2626
$this->assertEquals('Intervention\Image\Image', get_class($image));
27-
$this->assertTrue($image->stream()->isReadable());
27+
$this->assertNotEmpty($image->toJpeg()->size());
2828
}
2929

3030
/** @test */
@@ -35,7 +35,7 @@ public function can_detect_and_use_script_Bengali()
3535
$image = $avatar->autoFont()->generate('ǰǰô জ');
3636

3737
$this->assertEquals('Intervention\Image\Image', get_class($image));
38-
$this->assertTrue($image->stream()->isReadable());
38+
$this->assertNotEmpty($image->toJpeg()->size());
3939
}
4040

4141
/** @test */
@@ -46,7 +46,7 @@ public function can_detect_and_use_script_Georgian()
4646
$image = $avatar->autoFont()->generate('გამარჯობა');
4747

4848
$this->assertEquals('Intervention\Image\Image', get_class($image));
49-
$this->assertTrue($image->stream()->isReadable());
49+
$this->assertNotEmpty($image->toJpeg()->size());
5050
}
5151

5252
/** @test */
@@ -57,7 +57,7 @@ public function can_detect_and_use_script_Hebrew()
5757
$image = $avatar->autoFont()->generate('ה ו ז ח ט');
5858

5959
$this->assertEquals('Intervention\Image\Image', get_class($image));
60-
$this->assertTrue($image->stream()->isReadable());
60+
$this->assertNotEmpty($image->toJpeg()->size());
6161
}
6262

6363
/** @test */
@@ -68,7 +68,7 @@ public function can_detect_and_use_script_Mongolian()
6868
$image = $avatar->autoFont()->generate('ᠪᠣᠯᠠᠢ᠃');
6969

7070
$this->assertEquals('Intervention\Image\Image', get_class($image));
71-
$this->assertTrue($image->stream()->isReadable());
71+
$this->assertNotEmpty($image->toJpeg()->size());
7272
}
7373

7474
/** @test */
@@ -79,7 +79,7 @@ public function can_detect_and_use_script_Thai()
7979
$image = $avatar->autoFont()->generate('สวัสดีชาวโลกและยินดีต้อนรับแพ็กเกจนี้');
8080

8181
$this->assertEquals('Intervention\Image\Image', get_class($image));
82-
$this->assertTrue($image->stream()->isReadable());
82+
$this->assertNotEmpty($image->toJpeg()->size());
8383
}
8484

8585
/** @test */
@@ -90,7 +90,7 @@ public function can_detect_and_use_script_Tibetan()
9090
$image = $avatar->autoFont()->generate('ཀཁཆཇའ');
9191

9292
$this->assertEquals('Intervention\Image\Image', get_class($image));
93-
$this->assertTrue($image->stream()->isReadable());
93+
$this->assertNotEmpty($image->toJpeg()->size());
9494
}
9595

9696
/** @test */
@@ -101,7 +101,7 @@ public function can_detect_and_use_script_Turkish()
101101
$image = $avatar->autoFont()->generate('şçğüöı');
102102

103103
$this->assertEquals('Intervention\Image\Image', get_class($image));
104-
$this->assertTrue($image->stream()->isReadable());
104+
$this->assertNotEmpty($image->toJpeg()->size());
105105
}
106106

107107
/** @test */
@@ -112,6 +112,6 @@ public function can_detect_and_use_script_Uncommon()
112112
$image = $avatar->autoFont()->generate('ψψ');
113113

114114
$this->assertEquals('Intervention\Image\Image', get_class($image));
115-
$this->assertTrue($image->stream()->isReadable());
115+
$this->assertNotEmpty($image->toJpeg()->size());
116116
}
117117
}

0 commit comments

Comments
 (0)