PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.3
Kubio AI Page Builder v2.8.3
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 1 year ago KubioImageGenerate.php 1 year ago KubioImageManager.php 1 year ago KubioImagePrompts.php 2 years ago KubioImageSearch.php 1 year ago sd-endpoints.php 1 year ago
KubioImageManager.php
140 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 ) {
8 return KUBIO_ROOT_DIR . $this->folder . basename( $url );
9 }
10
11 public function get_folder_path() {
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 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
33 $filename = fopen( $this->get_image_path( $output_file ), 'wb' );
34
35 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
36 fwrite( $filename, $image_string );
37
38 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
39 fclose( $filename );
40
41 return kubio_url( $this->folder . $output_file );
42 }
43
44
45 public function base64_to_image( $base64_string, $output_file ) {
46
47 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
48 $filename = fopen(
49 $this->get_image_path( $output_file ),
50 'wb'
51 );
52
53 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
54 fwrite(
55 $filename,
56 base64_decode(
57 str_replace(
58 'data:image/png;base64,',
59 '',
60 $base64_string
61 )
62 )
63 );
64
65 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
66 fclose( $filename );
67
68 return kubio_url( $this->folder . $output_file );
69 }
70
71 public function get_file_contents( $url ) {
72 $path = $this->get_attachment_path( $url );
73
74 if ( $path === false ) {
75 return '';
76 }
77
78 return 'data:image/png;base64,' . base64_encode(
79 file_get_contents(
80 $path
81 )
82 );
83 }
84
85 public function save_image_from_url( $image_url, $output_file = '' ) {
86 if ( $output_file === '' ) {
87 $output_file = basename( $image_url );
88 }
89
90 $file_contents = file_get_contents( $image_url );
91
92 //
93 }
94
95
96 public function get_attachment_path( $url ) {
97 $path = false;
98
99 $dir = wp_upload_dir();
100
101 if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) {
102 $file = basename( $url );
103 }
104
105 $query_args = array(
106 'post_type' => 'attachment',
107 'post_status' => 'inherit',
108 'fields' => 'ids',
109 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
110 'meta_query' => array(
111 array(
112 'value' => $file,
113 'compare' => 'LIKE',
114 'key' => '_wp_attachment_metadata',
115 ),
116 ),
117 );
118
119 $query = new \WP_Query( $query_args );
120 $upload_dir = wp_upload_dir();
121
122 if ( $query->have_posts() ) {
123
124 foreach ( $query->posts as $post_id ) {
125
126 $meta = wp_get_attachment_metadata( $post_id );
127
128 $original_file = basename( $meta['file'] );
129 $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
130
131 if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
132 $path = $upload_dir['basedir'] . '/' . $meta['file'];
133 break;
134 }
135 }
136 }
137 return $path;
138 }
139 }
140