Skip to content

Commit

Permalink
wip: initial migration to intervention/image 3
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Apr 5, 2024
1 parent e01bead commit e87e646
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-20.04]
php: [ 8.2, 8.1, 8.0]
php: [ 8.3, 8.2, 8.1]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
- name: Checkout code
Expand Down
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
}
],
"require": {
"php": ">=8.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"illuminate/cache": "^8.0|^9.0|^10.0|^11.0",
"intervention/image": "^2.7"
"php": ">=8.1",
"illuminate/support": "^10.0|^11.0",
"illuminate/cache": "^10.0|^11.0",
"intervention/image": "^3.4"
},
"suggest": {
"ext-gd": "Neede to support image manipulation",
"ext-imagick": "Neede to support image manipulation, better than GD"
},

"require-dev": {
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "~9.0",
"phpunit/phpunit": "^10.5",
"mockery/mockery": "~1.3",
"php-coveralls/php-coveralls": "^2.1"
},
Expand Down
16 changes: 8 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
<testsuites>
<testsuite name="Avatar Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/ServiceProvider.php</file>
<file>src/Facade.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Avatar Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</source>
</phpunit>
17 changes: 17 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/ServiceProvider.php</file>
<file>src/Facade.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Avatar Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
110 changes: 56 additions & 54 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Illuminate\Contracts\Cache\Repository;
use Intervention\Image\AbstractFont;
use Intervention\Image\AbstractShape;
use Intervention\Image\Gd\Color;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Geometry\Factories\RectangleFactory;
use Intervention\Image\ImageManager;
use Intervention\Image\Typography\FontFactory;
use Laravolt\Avatar\Concerns\AttributeGetter;
use Laravolt\Avatar\Concerns\AttributeSetter;
use Laravolt\Avatar\Generator\DefaultGenerator;
Expand Down Expand Up @@ -50,10 +53,7 @@ class Avatar

protected $rtl = false;

/**
* @var \Intervention\Image\Image
*/
protected $image;
protected \Intervention\Image\Image $image;

protected $font = null;

Expand Down Expand Up @@ -93,8 +93,8 @@ public function __construct(array $config = [], Repository $cache = null)

// Add any additional themes for further use
$themes = $this->resolveTheme('*', $config['themes'] ?? []);
foreach ($themes as $name => $config) {
$this->addTheme($name, $config);
foreach ($themes as $name => $conf) {
$this->addTheme($name, $conf);
}

$this->initTheme();
Expand Down Expand Up @@ -187,7 +187,7 @@ public function toBase64()

$this->buildAvatar();

$base64 = (string)$this->image->encode('data-url');
$base64 = $this->image->toPng()->toDataUri();

$this->cache->forever($key, $base64);

Expand Down Expand Up @@ -301,10 +301,10 @@ protected function getRandomFont()

protected function getBorderColor()
{
if ($this->borderColor == 'foreground') {
if ($this->borderColor === 'foreground') {
return $this->foreground;
}
if ($this->borderColor == 'background') {
if ($this->borderColor === 'background') {
return $this->background;
}

Expand All @@ -318,8 +318,9 @@ public function buildAvatar()
$x = $this->width / 2;
$y = $this->height / 2;

$manager = new ImageManager(['driver' => $this->driver]);
$this->image = $manager->canvas($this->width, $this->height);
$driver = $this->driver === 'gd' ? new Driver() : new ImagickDriver();
$manager = new ImageManager($driver);
$this->image = $manager->create($this->width, $this->height);

$this->createShape();

Expand All @@ -331,7 +332,7 @@ public function buildAvatar()
$this->initials,
(int) $x,
(int) $y,
function (AbstractFont $font) {
function (FontFactory $font) {
$font->file($this->font);
$font->size($this->fontSize);
$font->color($this->foreground);
Expand Down Expand Up @@ -361,43 +362,45 @@ protected function createCircleShape()

if ($this->driver === 'gd') {
// parse background color
$background = new Color($this->background);

if ($this->borderSize) {
// slightly smaller ellipse to keep 1px bordered edges clean
imagefilledellipse(
$this->image->getCore(),
$x,
$y,
$this->width - 1,
$this->height - 1,
$background->getInt()
);

$border_color = new Color($this->getBorderColor());
imagesetthickness($this->image->getCore(), $this->borderSize);

// gd's imageellipse doesn't respect imagesetthickness so i use imagearc with 359.9 degrees here
imagearc(
$this->image->getCore(),
$x,
$y,
$circleDiameter,
$circleDiameter,
0,
(int) 359.99,
$border_color->getInt()
);
} else {
imagefilledellipse(
$this->image->getCore(),
$x,
$y,
$circleDiameter,
$circleDiameter,
$background->getInt()
);
$background = hexdec(ltrim($this->background, '#'));
foreach ($this->image as $frame) {
if ($this->borderSize) {
// slightly smaller ellipse to keep 1px bordered edges clean
imagefilledellipse(
$frame->native(),
$x,
$y,
$this->width - 1,
$this->height - 1,
$background
);

$border_color = hexdec(ltrim($this->getBorderColor(), '#'));
imagesetthickness($frame->native(), $this->borderSize);

// gd's imageellipse doesn't respect imagesetthickness so i use imagearc with 359.9 degrees here
imagearc(
$frame->native(),
$x,
$y,
$circleDiameter,
$circleDiameter,
0,
(int) 359.99,
$border_color
);
} else {
imagefilledellipse(
$frame->native(),
$x,
$y,
$circleDiameter,
$circleDiameter,
$background
);
}
}

} else {
$this->image->circle(
$circleDiameter,
Expand All @@ -418,14 +421,13 @@ protected function createSquareShape()
$width = $this->width - $edge;
$height = $this->height - $edge;

$this->image->rectangle(
$this->image->drawRectangle(
$x,
$y,
$width,
$height,
function (AbstractShape $draw) {
function (RectangleFactory $draw) use ($width, $height) {
$draw->size($width, $height);
$draw->background($this->background);
$draw->border($this->borderSize, $this->getBorderColor());
$draw->border($this->getBorderColor(), $this->borderSize);
}
);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/AvatarLaravelTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Laravolt\Avatar\Test;

use Mockery;

class AvatarLaravelTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down
6 changes: 6 additions & 0 deletions tests/AvatarPhpTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

namespace Laravolt\Avatar\Test;

use InvalidArgumentException;
use Mockery;
use stdClass;

class AvatarPhpTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down
7 changes: 5 additions & 2 deletions tests/InitialsGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

namespace Laravolt\Avatar\Test;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Laravolt\Avatar\Generator\DefaultGenerator;

class InitialGeneratorTest extends TestCase
class InitialsGeneratorTest extends TestCase
{
protected $generator;

Expand Down Expand Up @@ -35,7 +38,7 @@ public function it_cannot_accept_array()
public function it_cannot_accept_object_without_to_string_function()
{
$this->expectException(InvalidArgumentException::class);
$this->generator->make(new DefaultGenerator(new stdClass()));
$this->generator->make(new DefaultGenerator());
}

/**
Expand Down

0 comments on commit e87e646

Please sign in to comment.