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 |