<?php

use App\Fortuneteller;
use App\Helpers\Helper;
use Illuminate\Database\Seeder;

class GenerateFortuneThumb extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $list = Fortuneteller::all();
        $_imagePath = public_path('uploads/image/fortune/');
        foreach ($list as $item) {
            if ($item->avatar != null && $item->avatar != "" && file_exists($_imagePath . $item->avatar)) {
                try {
                    $imgCross = ImageCross::make($_imagePath . $item->avatar)->orientate();
                    if (!file_exists($_imagePath . "thumb_460x360/" . $item->avatar)) $imgCross->fit(460, 360, function ($constraint) { $constraint->upsize(); })->save($_imagePath . "thumb_460x360/" . $item->avatar);
                    if (!file_exists($_imagePath . "thumb_260x356/" . $item->avatar)) $imgCross->fit(260, 356, function ($constraint) { $constraint->upsize(); })->save($_imagePath . "thumb_260x356/" . $item->avatar);
                    if (!file_exists($_imagePath . "thumb_205x205/" . $item->avatar)) $imgCross->fit(205, 205, function ($constraint) { $constraint->upsize(); })->save($_imagePath . "thumb_205x205/" . $item->avatar);
                    if (!file_exists($_imagePath . "thumb_100x60/" . $item->avatar)) $imgCross->fit(100, 60, function ($constraint) { $constraint->upsize(); })->save($_imagePath . "thumb_100x60/" . $item->avatar);
                    if (!file_exists($_imagePath . "thumb_60x60/" . $item->avatar)) $imgCross->fit(60, 60, function ($constraint) { $constraint->upsize(); })->save($_imagePath . "thumb_60x60/" . $item->avatar);
                    print "Generated: " . $item->avatar . "\r\n";
                } catch (Intervention\Image\Exception\NotSupportedException $exception) {
                    logger("[Thumb] Image error: " . $exception->getMessage());
                    continue;
                }
            }
        }
    }
}
