<?php

namespace App\Modifiers;

use Imagick;
use Illuminate\Support\Facades\Storage;
use App\Models\ToolStats;

class Resizer
{
    public function make($fileName, array $size)
    {
        $filePath = Storage::disk('temp')
            ->path($fileName);
        $imagick = new Imagick($filePath);

        $sizeArr = [
            'width' => $size['width'] ?? $imagick->getImageWidth(),
            'height' => $size['height'] ?? $imagick->getImageHeight(),
        ];

        $imagick->resizeImage(
            $sizeArr['width'], 
            $sizeArr['height'], 
            Imagick::FILTER_LANCZOS, 
            1
        );

        $result = $imagick->writeImage($filePath);
        $imagick->clear();
        $imagick->destroy();

        if ($result) {
            $toolStats = new ToolStats();
            $toolStats->processNew('resize');
            return [
                true,
                $fileName
            ];
        }

        return [
            false,
            __('lang.file_resize_error')
       ];
    }

}