Skip to content

Instantly share code, notes, and snippets.

@ivanmrchk
Last active April 29, 2021 12:17
Show Gist options
  • Save ivanmrchk/9bfbbebe3e90407c3d65154fff29053c to your computer and use it in GitHub Desktop.
Save ivanmrchk/9bfbbebe3e90407c3d65154fff29053c to your computer and use it in GitHub Desktop.
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
}
if (isset($_POST['imagePpUrl'])) {
//for testing
$imageRemoveBackgroundUrl = 'https://new.realty-cards.com/wp-content/themes/porto-child/api/remove.bg/downloads/no-bg.png';
json_encode(array('success' => true, 'image' => $imageRemoveBackgroundUrl));
//for api
// echo json_encode(array('success' => true, 'image' => removeBackground ($imagePpUrl = $_POST['imagePpUrl'])));
}
function removeBackground ($imagePpUrl) {
require_once("guzzle/vendor/autoload.php");
$client = new GuzzleHttp\Client();
$res = $client->post('https://api.remove.bg/v1.0/removebg', [
'multipart' => [
[
'name' => 'image_url',
'contents' => $imagePpUrl,
],
[
'name' => 'size',
'contents' => 'auto',
]
],
'headers' => [
'X-Api-Key' => 'DypiAXfgeFeFsXpv2WjiZNe2',
]
]);
$filename = 'downloads/no-bg_' .time(). '.png';
$fp = fopen($filename, "wb");
fwrite($fp, $res->getBody());
fclose($fp);
return $imageRemoveBackgroundUrl = 'https://new.realty-cards.com/wp-content/themes/porto-child/api/remove.bg/'.$filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment