Remove Image Background

[remove_bg_upload]
'No image uploaded.']); } $file = $_FILES['image']['tmp_name']; $upload_dir = wp_upload_dir(); $output_file = $upload_dir['path'] . '/processed_' . basename($_FILES['image']['name']); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.remove.bg/v1.0/removebg'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-Api-Key: ' . REMOVE_BG_API_KEY ]); curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'image_file' => new CURLFile($file), 'size' => 'auto' ]); $response = curl_exec($ch); curl_close($ch); if (!$response) { wp_send_json_error(['message' => 'API request failed.']); } file_put_contents($output_file, $response); $image_url = $upload_dir['url'] . '/processed_' . basename($_FILES['image']['name']); wp_send_json_success(['image_url' => $image_url]); } add_action('wp_ajax_process_remove_bg', 'wp_process_remove_bg'); add_action('wp_ajax_nopriv_process_remove_bg', 'wp_process_remove_bg'); ?>
Scroll to Top