Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to php 8 and intervention/image v3 #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
}
],
"require": {
"php" : ">=7.0.0",
"php" : ">=8.1.0",
"ext-mbstring" : "*",
"intervention/image": "^2.3"
"intervention/image": "^3.3"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand Down
18 changes: 11 additions & 7 deletions src/LetterAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace YoHang88\LetterAvatar;

use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Gd\Font;
use Intervention\Image\Gd\Shapes\CircleShape;
use Intervention\Image\ImageManager;
Expand Down Expand Up @@ -65,7 +66,7 @@ class LetterAvatar
public function __construct(string $name, string $shape = 'circle', int $size = 48)
{
$this->setName($name);
$this->setImageManager(new ImageManager());
$this->setImageManager(new ImageManager(new Driver()));
$this->setShape($shape);
$this->setSize($size);
}
Expand Down Expand Up @@ -129,17 +130,20 @@ private function generate(): \Intervention\Image\Image
$this->backgroundColor = $this->backgroundColor ?: $this->stringToColor($this->name);
$this->foregroundColor = $this->foregroundColor ?: '#fafafa';

$canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $this->backgroundColor);
$canvas = $this->imageManager->create(480, 480);

if ($isCircle) {
$canvas->circle(480, 240, 240, function (CircleShape $draw) {
$canvas->drawCircle(240, 240, function ($draw) {
$draw->diameter(480);
$draw->background($this->backgroundColor);
});

} else {
$canvas->fill($this->backgroundColor);
}

$canvas->text($this->nameInitials, 240, 240, function (Font $font) {
$font->file(__DIR__ . '/fonts/arial-bold.ttf');
$canvas->text($this->nameInitials, 240, 240, function ($font) {
$font->filename(__DIR__ . '/fonts/arial-bold.ttf');
$font->size(220);
$font->color($this->foregroundColor);
$font->valign('middle');
Expand Down Expand Up @@ -192,7 +196,7 @@ public function encode($mimetype = self::MIME_TYPE_PNG, $quality = 90): string
if(!in_array($mimetype, $allowedMimeTypes, true)) {
throw new InvalidArgumentException('Invalid mimetype');
}
return $this->generate()->encode($mimetype, $quality);
return $this->generate()->encodeByMediaType($mimetype, $quality);
}

/**
Expand All @@ -217,7 +221,7 @@ public function saveAs($path, $mimetype = self::MIME_TYPE_PNG, $quality = 90): b
*/
public function __toString(): string
{
return (string)$this->generate()->encode('data-url');
return (string)$this->generate()->toPng()->toDataUri();
}

/**
Expand Down