diff --git a/app/Models/Image.php b/app/Models/Image.php index 0bf18079..b6c1b80f 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -25,6 +25,8 @@ use Illuminate\Database\Eloquent\Model; use Config; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\File\UploadedFile; +use ColorThief\ColorThief; +use Helpers; /** * Poniverse\Ponyfm\Models\Image @@ -36,6 +38,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; * @property integer $size * @property string $hash * @property integer $uploaded_by + * @property string $palette * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Image whereId($value) @@ -191,4 +194,15 @@ class Image extends Model mkdir($destination, 0777, true); } } + + public function getPalette() { + if ($this->palette === null) { + $palette = ColorThief::getPalette($this->getFile(Image::NORMAL), 2); + $formatted_palette = array_map("Helpers::rgb2hex", $palette); + $this->palette = json_encode($formatted_palette); + $this->save(); + } + + return json_decode($this->palette); + } } diff --git a/app/Models/Track.php b/app/Models/Track.php index b17b8e7d..2e537824 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -500,6 +500,7 @@ class Track extends Model implements Searchable, Commentable, Favouritable 'normal' => $track->getCoverUrl(Image::NORMAL), 'original' => $track->getCoverUrl(Image::ORIGINAL) ], + 'cover_colors' => $track->cover->getPalette(), 'streams' => [ 'mp3' => $track->getStreamUrl('MP3'), 'aac' => (!Config::get('app.debug') || is_file($track->getFileFor('AAC'))) ? $track->getStreamUrl('AAC') : null, diff --git a/database/migrations/2017_05_19_124056_AddPaletteToImages.php b/database/migrations/2017_05_19_124056_AddPaletteToImages.php new file mode 100644 index 00000000..8879f031 --- /dev/null +++ b/database/migrations/2017_05_19_124056_AddPaletteToImages.php @@ -0,0 +1,32 @@ +json('palette')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('images', function (Blueprint $table) { + $table->dropColumn(['palette']); + }); + } +}