PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / BackgroundRemovalService.php

BackgroundRemovalService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/BackgroundRemovalService.php

142 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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