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

fix: fix resize with parameter #7

Merged
merged 2 commits into from
Jan 11, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.releaserc export-ignore
.styleci.yml export-ignore
CHANGELOG.md export-ignore
phpstan.neon export-ignore
Expand Down
File renamed without changes.
65 changes: 61 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
# laravel-adorable
Generate an Adorable Avatar for Laravel
Adorable Avatar for Laravel
============================

## Inspiration
This is mainly inspired by https://github.com/itsthatguy/avatars-api-middleware
LaravelAdorable is an library to generate nice avatars on Laravel.

[![Latest Version](https://img.shields.io/packagist/v/asbiin/laravel-adorable.svg?style=flat-square)](https://github.com/asbiin/laravel-adorable/releases)
[![Downloads](https://img.shields.io/packagist/dt/asbiin/laravel-adorable.svg?style=flat-square)](https://packagist.org/packages/asbiin/laravel-adorable)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/asbiin/laravel-adorable/Unit%20tests?style=flat-square)](https://github.com/asbiin/laravel-adorable/actions?query=branch%3Amain)
[![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/asbiin_laravel-adorable?server=https%3A%2F%2Fsonarcloud.io&style=flat-square)](https://sonarcloud.io/dashboard?id=asbiin_laravel-adorable)
[![Coverage Status](https://img.shields.io/sonar/https/sonarcloud.io/asbiin_laravel-adorable/coverage.svg?style=flat-square)](https://sonarcloud.io/dashboard?id=asbiin_laravel-adorable)


# Installation

You may use Composer to install this package into your Laravel project:

``` bash
composer require asbiin/laravel-adorable
```

You don't need to add this package to your service providers.

## Support

This package supports Laravel 8 and newer, and has been tested with php 7.4 and newer versions.


## Configuration

You can publish the LaravelAdorable configuration in a file named `config/adorable.php`.
Just run this artisan command:

```sh
php artisan vendor:publish --tag="laraveladorable-config"
```


# Usage

Use `LaravelAdorable` facade to generate avatar:

```php
use Illuminate\Support\Str;
use LaravelAdorable\Facades\LaravelAdorable;

...
$size = 200;
$hash = Str::uuid();
$dataUrl = LaravelAdorable::get($size, $hash);
```

# License

Author: [Alexis Saettler](https://github.com/asbiin)

Copyright © 2022.

Licensed under the MIT License. [View license](/LICENSE).

# Inspiration

This work is mainly inspired by [itsthatguy/avatars-api-middleware](https://github.com/itsthatguy/avatars-api-middleware) (MIT License).
16 changes: 0 additions & 16 deletions config/adorable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@

'shape' => 'square',

/*
|--------------------------------------------------------------------------
| Image width
|--------------------------------------------------------------------------
*/

'width' => 200,

/*
|--------------------------------------------------------------------------
| Image height
|--------------------------------------------------------------------------
*/

'height' => 200,

/*
|--------------------------------------------------------------------------
| Background colors
Expand Down
58 changes: 44 additions & 14 deletions src/Adorable/LaravelAdorable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,31 @@

class LaravelAdorable
{
const DEFAULT_SIZE = 400;
/**
* Default avatar size.
*
* @var int
*/
public const DEFAULT_SIZE = 400;

/**
* @var HashCollection
*/
private $colors;

/**
* @var HashCollection
*/
private $eyes;

/**
* @var HashCollection
*/
private $noses;

/**
* @var HashCollection
*/
private $mouths;

/**
Expand All @@ -37,12 +57,25 @@ public function __construct(Config $config, Cache $cache)
$this->mouths = new HashCollection(static::files('mouths'));
}

protected static function files(string $type)
/**
* Get image files.
*
* @param string $type
* @return array
*/
protected static function files(string $type): array
{
return array_slice(scandir(__DIR__."/../../resources/img/$type"), 2);
}

public function get(string $size, string $uuid)
/**
* Get base64 data-url of avatar.
*
* @param string $size
* @param string $uuid
* @return string
*/
public function get(string $size, string $uuid): string
{
$values = $this->hash($uuid);

Expand All @@ -59,7 +92,7 @@ public function get(string $size, string $uuid)
});
}

private function cacheKey(string $size, string $uuid, array $values): string
private function cacheKey(string $size, string $uuid, array &$values): string
{
$values['size'] = $size;
$values['uuid'] = $uuid;
Expand All @@ -80,11 +113,8 @@ private function hash(string $uuid): array
];
}

private function buildAvatar(array $values)
private function buildAvatar(array $values): Image
{
$width = $this->config->get('adorable.width');
$height = $this->config->get('adorable.height');

/** @var \Intervention\Image\ImageManager $manager */
$manager = ImageFacade::configure([
'driver' => $this->config->get('adorable.driver'),
Expand All @@ -95,12 +125,12 @@ private function buildAvatar(array $values)
$this->insert($image, $values, 'eyes');
$this->insert($image, $values, 'noses');
$this->insert($image, $values, 'mouths');
$image->resize($width, $height);
$image->resize($values['size'], $values['size']);

return $image;
}

private function createShape(Image $image, array $values)
private function createShape(Image $image, array $values): void
{
$shape = $this->config->get('adorable.shape');
switch ($shape) {
Expand All @@ -117,7 +147,7 @@ private function createShape(Image $image, array $values)
}
}

private function createCircleShape(Image $image, array $values)
private function createCircleShape(Image $image, array $values): void
{
$circleDiameter = self::DEFAULT_SIZE - $this->config->get('adorable.border.size');
$x = $y = self::DEFAULT_SIZE / 2;
Expand All @@ -133,7 +163,7 @@ function (AbstractShape $draw) use ($values) {
);
}

private function createSquareShape(Image $image, array $values)
private function createSquareShape(Image $image, array $values): void
{
$edge = (int) ceil($this->config->get('adorable.border.size') / 2);
$x = $y = $edge;
Expand All @@ -151,7 +181,7 @@ function (AbstractShape $draw) use ($values) {
);
}

private function getBorderColor(array $values)
private function getBorderColor(array $values): string
{
switch ($color = $this->config->get('adorable.border.color')) {
case 'white':
Expand All @@ -165,7 +195,7 @@ private function getBorderColor(array $values)
}
}

private function insert(Image $image, array $values, string $type)
private function insert(Image $image, array $values, string $type): void
{
$filename = $values[$type];

Expand Down
2 changes: 2 additions & 0 deletions src/Facades/LaravelAdorable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static string get(string $size, string $uuid)
*
* @see \LaravelAdorable\Service\LaravelAdorable
*/
class LaravelAdorable extends Facade
Expand Down