<?php

namespace App\Modifiers;

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

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

        if (isset($exif['deep']) && $exif['deep'] == 1) {
            $imagick->stripImage();
        } else {
            $profiles = $imagick->getImageProfiles("icc", true);
            $imagick->stripImage();
            if ($profiles) {
                $imagick->profileImage("icc", $profiles['icc']);
            }
        }

        $result = $imagick->writeImage($filePath);
        $imagick->clear();
        $imagick->destroy();
        
        if ($result) {
            $toolStats = new ToolStats();
            $toolStats->processNew('exif_cleaner');
            return [
                true,
                $fileName
            ];
        }

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

}
