<?php

function createImage($provider_translit, $translit, $img) {
    $isLocal = getenv('LOCAL_ENV') === 'true';

    $directory = $isLocal ? "/Users/serhiipozynich" : "/var/www";
    $image_path = $directory . '/gamble-free.com/images/' . $provider_translit . '/' . $translit . '.webp';
    $image_temp_path = $directory . '/gamble-free.com/images/' . $provider_translit . '/' . $translit . '_temp.jpg';

    file_put_contents($image_temp_path, file_get_contents($img));

    if ($isLocal) {
        // Use sips for macOS
        $cmd_resize = 'sips -z 270 480 ' . $image_temp_path . ' -s format jpeg --out ' . $image_path;
        exec($cmd_resize);
    } else {
        // Use convert for Ubuntu
        $cmd_resize = 'convert ' . $image_temp_path . ' -resize 480x270\! ' . $image_path;
        exec($cmd_resize);
    }

    $cmd_convert = 'cwebp -q 80 ' . $image_path . ' -o ' . $image_path . ' > /dev/null 2>&1';
    exec($cmd_convert);

    unlink($image_temp_path);

    echo $image_path . "\n";
}

?>
