diff --git a/app/Http/Controllers/Api/Web/ArtistsController.php b/app/Http/Controllers/Api/Web/ArtistsController.php index 652ff0eb..11feafa8 100644 --- a/app/Http/Controllers/Api/Web/ArtistsController.php +++ b/app/Http/Controllers/Api/Web/ArtistsController.php @@ -31,6 +31,8 @@ use Poniverse\Ponyfm\Models\User; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Response; +use ColorThief\ColorThief; +use Helpers; class ArtistsController extends ApiControllerBase { @@ -157,6 +159,9 @@ class ArtistsController extends ApiControllerBase ]; } + $palette = ColorThief::getPalette($user->getAvatarUrl(Image::SMALL), 2); + $formatted_palette = array_map("Helpers::rgb2hex", $palette); + return Response::json([ 'artist' => [ 'id' => $user->id, @@ -167,6 +172,7 @@ class ArtistsController extends ApiControllerBase 'small' => $user->getAvatarUrl(Image::SMALL), 'normal' => $user->getAvatarUrl(Image::NORMAL) ], + 'avatar_colors' => $formatted_palette, 'created_at' => $user->created_at, 'followers' => [], 'following' => [], diff --git a/app/Library/Helpers.php b/app/Library/Helpers.php index b7124a4a..d9b004c0 100644 --- a/app/Library/Helpers.php +++ b/app/Library/Helpers.php @@ -75,4 +75,19 @@ class Helpers return '' . $content . ''; } + + /** + * Converts an RGB array to a hex string + * + * @param array[int] $rgb RGB values in an array + * @return string + */ + public static function rgb2hex($rgb) { + $hex = "#"; + $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT); + $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT); + $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT); + + return $hex; + } } diff --git a/composer.json b/composer.json index 3ac3ffbd..79145682 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "pda/pheanstalk": "~3.0", "cviebrock/laravel-elasticsearch": "^1.0", "barryvdh/laravel-debugbar": "^2.1", - "predis/predis": "^1.0" + "predis/predis": "^1.0", + "ksubileau/color-thief-php": "^1.3" }, "require-dev": { "fzaninotto/faker": "~1.4", diff --git a/composer.lock b/composer.lock index e4f495c5..4c1fc94b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a12fe5f5687e21aac441868ccc882fa2", - "content-hash": "95039400d11d1851dabe46e4c0456965", + "hash": "34abdcf2e916a26919b81059ba38fa47", + "content-hash": "afae7bc6603035b2573af4b56c91c0dd", "packages": [ { "name": "barryvdh/laravel-debugbar", @@ -1385,6 +1385,59 @@ ], "time": "2015-12-05 17:17:57" }, + { + "name": "ksubileau/color-thief-php", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/ksubileau/color-thief-php.git", + "reference": "f0e1bc5da3dd86abd3e8c95a553c3e8f25e0fe7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ksubileau/color-thief-php/zipball/f0e1bc5da3dd86abd3e8c95a553c3e8f25e0fe7e", + "reference": "f0e1bc5da3dd86abd3e8c95a553c3e8f25e0fe7e", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.*", + "squizlabs/php_codesniffer": "2.*" + }, + "suggest": { + "ext-gd": "to use the GD image adapter.", + "ext-gmagick": "to use the Gmagick image adapter.", + "ext-imagick": "to use the Imagick image adapter." + }, + "type": "library", + "autoload": { + "psr-0": { + "ColorThief": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "CC-BY-2.5" + ], + "authors": [ + { + "name": "Kevin Subileau", + "homepage": "http://www.kevinsubileau.fr" + } + ], + "description": "Grabs the dominant color or a representative color palette from an image.", + "homepage": "http://www.kevinsubileau.fr/projets/color-thief-php", + "keywords": [ + "color", + "dominant", + "palette", + "php", + "thief" + ], + "time": "2016-01-17 18:55:16" + }, { "name": "laravel/framework", "version": "v5.1.28", diff --git a/public/templates/artists/_show_layout.html b/public/templates/artists/_show_layout.html index a580e778..e58942cf 100644 --- a/public/templates/artists/_show_layout.html +++ b/public/templates/artists/_show_layout.html @@ -1,23 +1,24 @@