All images now have colour palettes

This commit is contained in:
Josef Citrine 2017-05-19 14:10:09 +01:00
parent 4e2b2f90ec
commit 40f5d0721e
3 changed files with 47 additions and 0 deletions

View file

@ -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);
}
}

View file

@ -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,

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddPaletteToImages extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('images', function(Blueprint $table) {
$table->json('palette')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('images', function (Blueprint $table) {
$table->dropColumn(['palette']);
});
}
}