<?php

namespace App\Console\Commands;

use App\Models\RoomNeedMore;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //

        $items = RoomNeedMore::all();

        foreach ($items as $item) {
            $image = 'https://itro.vn/files/' . $item->compress_image;
            $filename = pathinfo(parse_url($image, PHP_URL_PATH), PATHINFO_FILENAME);
            $new = \Image::make($image)->encode('webp')->save(public_path('files/' . $filename . '.webp'));
            $item->compress_image =  $filename . '.webp';
            $item->save();
        }

//        for ($i = 1; $i <= 25; $i++) {
//            $image = 'https://itro.vn/random/pic' . $i . '.jpg';
//            $filename = pathinfo(parse_url($image, PHP_URL_PATH), PATHINFO_FILENAME);
//
//            $new = \Image::make($image)->encode('webp')->save(public_path('random/' . $filename . '.webp'));
//        }
    }
}
