<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;

class Thumbnail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'thumbnail:make';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Make thumbnails of all image files';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
    $dir = public_path().'/'.config('app.image.dir').'/backup';
    $thumb_dir = public_path().'/'.config('app.image.thumb_dir').'/thumb';
    $w = config('app.image.thumb_width');
    $h = config('app.image.thumb_height');

	$output = new \Symfony\Component\Console\Output\ConsoleOutput();
	$output->writeln("<info>Starting make thumb</info>");


	$files = \File::allFiles($dir);
	foreach ($files as $file) {
		$thumb_file = str_replace('backup','thumb',$file);

		$output->write($file);
		$output->writeln("<info> [OK]</info>");
		
		$image = Image::make($file)->resize($w, $h);
		\File::makeDirectory(dirname($thumb_file), $mode = 0775, true, true);
		$image->save($thumb_file);

		$output->write("<info>[NEW] </info>");
		$output->write($thumb_file);
		$output->writeln("<info> [OK]</info>");


        }
    }
}
