PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.1.1
Kubio AI Page Builder v2.1.1
2.9.1 2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / AI / SDApi / KubioImageManager.php
kubio / lib / AI / SDApi Last commit date
KubioCloudSDApi.php 2 years ago KubioImageGenerate.php 2 years ago KubioImageManager.php 2 years ago KubioImagePrompts.php 2 years ago KubioImageSearch.php 2 years ago sd-endpoints.php 2 years ago
KubioImageManager.php
133 lines
1 <?php
2 namespace Kubio\Ai;
3
4 class KubioImageManager {
5 private $folder = '/static/ai-assets/generated/';
6
7 public function get_file_path_from_url( $url ) : string {
8 return KUBIO_ROOT_DIR . $this->folder . basename( $url );
9 }
10
11 public function get_folder_path() : string {
12 return $this->folder;
13 }
14
15 public function get_image_path( $output_file ) {
16 return KUBIO_ROOT_DIR . $this->folder . $output_file;
17 }
18
19 public function save_from_base64( $base64_string, $filename = '' ) {
20 if ( $filename === '' ) {
21 $filename = wp_generate_uuid4() . '.png';
22 }
23
24 return $this->base64_to_image( $base64_string, $filename );
25 }
26
27 public function save_image_from_string( $image_string, $output_file = '' ) {
28 if ( $output_file === '' ) {
29 $output_file = wp_generate_uuid4() . '.png';
30 }
31
32 $filename = fopen( $this->get_image_path( $output_file ), 'wb' );
33
34 fwrite( $filename, $image_string );
35 fclose( $filename );
36
37 return kubio_url( $this->folder . $output_file );
38 }
39
40
41 public function base64_to_image( $base64_string, $output_file ) {
42 $filename = fopen(
43 $this->get_image_path( $output_file ),
44 'wb'
45 );
46 fwrite(
47 $filename,
48 base64_decode(
49 str_replace(
50 'data:image/png;base64,',
51 '',
52 $base64_string
53 )
54 )
55 );
56 fclose( $filename );
57
58 return kubio_url( $this->folder . $output_file );
59 }
60
61 public function get_file_contents( $url ) {
62 $path = $this->get_attachment_path( $url );
63
64 if ( $path === false ) {
65 return '';
66 }
67
68 return 'data:image/png;base64,' . base64_encode(
69 file_get_contents(
70 $path
71 )
72 );
73 }
74
75 public function save_image_from_url( $image_url, $output_file = '' ) {
76 if ( $output_file === '' ) {
77 $output_file = basename( $image_url );
78 }
79
80 $file_contents = file_get_contents( $image_url );
81
82 //
83 }
84
85
86 public function get_attachment_path( $url ) {
87 $path = false;
88
89 $dir = wp_upload_dir();
90
91 if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) {
92 $file = basename( $url );
93 }
94
95 $query_args = array(
96 'post_type' => 'attachment',
97 'post_status' => 'inherit',
98 'fields' => 'ids',
99 'meta_query' => array(
100 array(
101 'value' => $file,
102 'compare' => 'LIKE',
103 'key' => '_wp_attachment_metadata',
104 ),
105 ),
106 );
107
108 $query = new \WP_Query( $query_args );
109 $upload_dir = wp_upload_dir();
110
111 if ( $query->have_posts() ) {
112
113 foreach ( $query->posts as $post_id ) {
114
115 $meta = wp_get_attachment_metadata( $post_id );
116
117 $original_file = basename( $meta['file'] );
118 $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
119
120 if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
121 $path = $upload_dir['basedir'] . '/' . $meta['file'];
122 break;
123 }
124 }
125 }
126 return $path;
127
128 }
129
130
131
132 }
133