| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Depicter; |
| 6 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 7 |
|
| 8 |
class BackgroundRemovalService |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Upload media to process for background removal |
| 12 |
* @param $assetIDorURL |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public function upload( $assetIDorURL ): array |
| 17 |
{ |
| 18 |
|
| 19 |
$type = ''; |
| 20 |
if ( is_numeric( $assetIDorURL ) ) { |
| 21 |
try { |
| 22 |
$type = get_post_mime_type( $assetIDorURL ); |
| 23 |
if ( $type !== 'image/jpeg' && $type !== 'image/png' ) { |
| 24 |
return [ |
| 25 |
'succeed' => false, |
| 26 |
'error' => [ __( 'Invalid Media Type', 'depicter' ) ] |
| 27 |
]; |
| 28 |
} |
| 29 |
$assetIDorURL = \Depicter::media()->getSourceUrl($assetIDorURL); |
| 30 |
} catch ( GuzzleException|\Exception $e ){ |
| 31 |
return [ |
| 32 |
'succeed' => false, |
| 33 |
'errors' => [ $e->getMessage() ] |
| 34 |
]; |
| 35 |
} |
| 36 |
} elseif ( strpos( $assetIDorURL, '@' ) != 0 ) { |
| 37 |
return [ |
| 38 |
'succeed' => false, |
| 39 |
'errors' => [ __('Media ID is not valid.', 'depicter' ) ] |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
try { |
| 44 |
$response = \Depicter::remote()->post( 'v1/uploadcare/image/upload', [ |
| 45 |
'form_params' => [ |
| 46 |
'process_input' => $assetIDorURL, |
| 47 |
'type' => $type |
| 48 |
] |
| 49 |
] ); |
| 50 |
|
| 51 |
$response = JSON::decode( $response->getBody()->getContents(), true ); |
| 52 |
|
| 53 |
if ( ! empty( $response['errors'] ) ) { |
| 54 |
return [ |
| 55 |
'succeed' => false, |
| 56 |
'errors' => $response['errors'] |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
return [ |
| 61 |
'succeed' => true, |
| 62 |
'hits' => $response['hits'] |
| 63 |
]; |
| 64 |
} catch ( GuzzleException $e ){ |
| 65 |
return [ |
| 66 |
'succeed' => false, |
| 67 |
'errors' => $e->getMessage() |
| 68 |
]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get removed background image |
| 74 |
* @param $processID |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function getRemovedBackgroundImage( $processID ): array |
| 79 |
{ |
| 80 |
if ( file_exists( \Depicter::storage()->uploads()->getPath() . '/' . $processID . '.png' ) ) { |
| 81 |
$url = \Depicter::storage()->uploads()->getUrl() . '/' . $processID . '.png'; |
| 82 |
$attachmentID = attachment_url_to_postid( $url ); |
| 83 |
if ( $attachmentID ) { |
| 84 |
return [ |
| 85 |
'status' => 'done', |
| 86 |
'hits' => [ |
| 87 |
'attachmentID' => $attachmentID, |
| 88 |
'attachmentURL' => $url |
| 89 |
] |
| 90 |
]; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
try { |
| 95 |
$response = \Depicter::remote()->post( 'v1/uploadcare/image/removebg', [ |
| 96 |
'form_params' => [ |
| 97 |
'process' => $processID |
| 98 |
] |
| 99 |
] ); |
| 100 |
|
| 101 |
if ( $response->getHeader('Content-Type')[0] == 'application/json' ) { |
| 102 |
$response = JSON::decode( $response->getBody()->getContents(), true ); |
| 103 |
if ( ! empty( $response['errors'] ) ) { |
| 104 |
return [ |
| 105 |
'errors' => $response['errors'] |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
return [ |
| 110 |
'status' => $response['status'], |
| 111 |
'hits' => [ |
| 112 |
'attachmentID' => "", |
| 113 |
'attachmentURL' => "" |
| 114 |
] |
| 115 |
]; |
| 116 |
} else { |
| 117 |
$body = $response->getBody()->getContents(); |
| 118 |
$path = \Depicter::storage()->uploads()->getPath() . '/' . $processID . '.png'; |
| 119 |
\Depicter::storage()->filesystem()->write( $path, $body ); |
| 120 |
$attachmentID = \Depicter::media()->library()->insertAttachment( $processID, $path, 'image/png', $processID ); |
| 121 |
if ( ! empty( $attachmentID ) ) { |
| 122 |
return [ |
| 123 |
'status' => 'done', |
| 124 |
'hits' => [ |
| 125 |
'attachmentID' => $attachmentID, |
| 126 |
'attachmentURL' => Depicter::media()->getSourceUrl( $attachmentID ) |
| 127 |
] |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
return [ |
| 132 |
'errors' => [ __( 'Error while importing the new background removed image', 'depicter' ) ] |
| 133 |
]; |
| 134 |
} |
| 135 |
} catch ( GuzzleException|\Exception $e ){ |
| 136 |
return [ |
| 137 |
'errors' => [ $e->getMessage() ] |
| 138 |
]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|