/* Start custom CSS for html, class: .elementor-element-9ba5704 */<?php
if (isset($_FILES['image_file']) && isset($_POST['output_format'])) {
    $output_format = strtolower($_POST['output_format']);
    $allowed = ['jpg','jpeg','png','gif','bmp','webp'];
    if (!in_array($output_format, $allowed)) {
        die("Invalid output format.");
    }

    $file = $_FILES['image_file']['tmp_name'];
    $ext = strtolower(pathinfo($_FILES['image_file']['name'], PATHINFO_EXTENSION));

    switch($ext){
        case 'jpg': case 'jpeg': $img = imagecreatefromjpeg($file); break;
        case 'png': $img = imagecreatefrompng($file); break;
        case 'gif': $img = imagecreatefromgif($file); break;
        case 'bmp': $img = imagecreatefrombmp($file); break;
        case 'webp': $img = imagecreatefromwebp($file); break;
        default: die("Unsupported file type.");
    }

    $output_name = 'converted_' . time() . '.' . $output_format;
    $output_path = __DIR__ . '/' . $output_name;

    switch($output_format){
        case 'jpg': case 'jpeg': imagejpeg($img, $output_path, 90); break;
        case 'png': imagepng($img, $output_path); break;
        case 'gif': imagegif($img, $output_path); break;
        case 'bmp': imagebmp($img, $output_path); break;
        case 'webp': imagewebp($img, $output_path); break;
    }

    imagedestroy($img);
    echo json_encode([
        'success' => true,
        'preview' => basename($output_name)
    ]);
    exit;
}
?>/* End custom CSS */