image.php
1 year ago
message-builder.php
11 months ago
model-environment.php
11 months ago
response-id-manager.php
1 year ago
session.php
11 months ago
usage-stats.php
11 months ago
image.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Services_Image { |
| 4 | private $core; |
| 5 | |
| 6 | public function __construct( $core ) { |
| 7 | $this->core = $core; |
| 8 | } |
| 9 | |
| 10 | public function is_image( $mimeType ) { |
| 11 | return strpos( $mimeType, 'image/' ) === 0; |
| 12 | } |
| 13 | |
| 14 | public function get_image_resolution( $imageData ) { |
| 15 | try { |
| 16 | $tempFile = tmpfile(); |
| 17 | $tempFilePath = stream_get_meta_data( $tempFile )['uri']; |
| 18 | fwrite( $tempFile, $imageData ); |
| 19 | $imageSize = getimagesize( $tempFilePath ); |
| 20 | fclose( $tempFile ); |
| 21 | if ( $imageSize !== false ) { |
| 22 | return $imageSize[0] . 'x' . $imageSize[1]; |
| 23 | } |
| 24 | } |
| 25 | catch ( Exception $e ) { |
| 26 | throw new Exception( 'Failed to get image resolution.' ); |
| 27 | } |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | public function get_mime_type( $file, $fileData = null ) { |
| 32 | $mimeType = null; |
| 33 | |
| 34 | // If we have file data, let's use it |
| 35 | if ( !empty( $fileData ) ) { |
| 36 | $f = finfo_open(); |
| 37 | $mimeType = finfo_buffer( $f, $fileData, FILEINFO_MIME_TYPE ); |
| 38 | } |
| 39 | |
| 40 | // Try to use mime_content_type for local files |
| 41 | if ( !$mimeType ) { |
| 42 | $isUrl = filter_var( $file, FILTER_VALIDATE_URL ); |
| 43 | if ( !$isUrl && function_exists( 'mime_content_type' ) && file_exists( $file ) ) { |
| 44 | $mimeType = mime_content_type( $file ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Otherwise, let's check the file extension (which can actually also be an URL) |
| 49 | if ( !$mimeType ) { |
| 50 | $extension = pathinfo( $file, PATHINFO_EXTENSION ); |
| 51 | $extension = strtolower( $extension ); |
| 52 | $mimeTypes = [ |
| 53 | 'jpg' => 'image/jpeg', |
| 54 | 'jpeg' => 'image/jpeg', |
| 55 | 'png' => 'image/png', |
| 56 | 'gif' => 'image/gif', |
| 57 | 'webp' => 'image/webp', |
| 58 | 'bmp' => 'image/bmp', |
| 59 | 'tiff' => 'image/tiff', |
| 60 | 'tif' => 'image/tiff', |
| 61 | 'svg' => 'image/svg+xml', |
| 62 | 'ico' => 'image/x-icon', |
| 63 | 'pdf' => 'application/pdf', |
| 64 | ]; |
| 65 | $mimeType = isset( $mimeTypes[$extension] ) ? $mimeTypes[$extension] : null; |
| 66 | } |
| 67 | |
| 68 | return $mimeType; |
| 69 | } |
| 70 | |
| 71 | public function download_image( $url ) { |
| 72 | $response = wp_safe_remote_get( $url, [ 'timeout' => 60 ] ); |
| 73 | if ( is_wp_error( $response ) ) { |
| 74 | throw new Exception( $response->get_error_message() ); |
| 75 | } |
| 76 | return wp_remote_retrieve_body( $response ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add an image from a URL to the Media Library. |
| 81 | * @param string $url The URL of the image to be downloaded. |
| 82 | * @param string $filename The filename of the image, if not set, it will be the basename of the URL. |
| 83 | * @param string $title The title of the image. |
| 84 | * @param string $description The description of the image. |
| 85 | * @param string $caption The caption of the image. |
| 86 | * @param string $alt The alt text of the image. |
| 87 | * @return int The attachment ID of the image. |
| 88 | */ |
| 89 | public function add_image_from_url( $url, $filename = null, $title = null, $description = null, $caption = null, $alt = null, $attachedPost = null ) { |
| 90 | $path_parts = pathinfo( parse_url( $url, PHP_URL_PATH ) ); |
| 91 | $url_filename = $path_parts['basename']; |
| 92 | $file_type = wp_check_filetype( $url_filename, null ); |
| 93 | $allowed_types = get_allowed_mime_types(); |
| 94 | if ( !$file_type || !in_array( $file_type['type'], $allowed_types ) ) { |
| 95 | throw new Exception( 'Invalid file type from URL.' ); |
| 96 | } |
| 97 | |
| 98 | // Initial extension from URL file name |
| 99 | $extension = $file_type['ext']; |
| 100 | |
| 101 | if ( !empty( $filename ) ) { |
| 102 | $custom_file_type = wp_check_filetype( $filename, null ); |
| 103 | if ( !$custom_file_type || !in_array( $custom_file_type['type'], $allowed_types ) ) { |
| 104 | throw new Exception( 'Invalid custom file type.' ); |
| 105 | } |
| 106 | // Use the extension from the custom filename if valid |
| 107 | $extension = $custom_file_type['ext']; |
| 108 | } |
| 109 | |
| 110 | $image_data = $this->download_image( $url ); |
| 111 | if ( !$image_data ) { |
| 112 | throw new Exception( 'Could not download the image.' ); |
| 113 | } |
| 114 | $upload_dir = wp_upload_dir(); |
| 115 | |
| 116 | // Filename handling including 'generated_' prefix scenario |
| 117 | if ( empty( $filename ) ) { |
| 118 | $filename = sanitize_file_name( $url_filename ); |
| 119 | if ( empty( $extension ) ) { // This condition might now be redundant |
| 120 | $extension = $file_type['ext']; |
| 121 | } |
| 122 | // Filename length check and prepend if conditions met |
| 123 | if ( strlen( $filename ) > 32 || strlen( $filename ) < 4 || strpos( $filename, 'generated_' ) === 0 ) { |
| 124 | $filename = uniqid( 'ai_', true ) . '.' . $extension; |
| 125 | } |
| 126 | if ( strpos( $filename, '.' ) === false ) { |
| 127 | $filename .= '.' . $extension; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Directory and file path handling |
| 132 | if ( wp_mkdir_p( $upload_dir['path'] ) ) { |
| 133 | $file = $upload_dir['path'] . '/' . $filename; |
| 134 | } |
| 135 | else { |
| 136 | $file = $upload_dir['basedir'] . '/' . $filename; |
| 137 | } |
| 138 | |
| 139 | // Ensure file name uniqueness in the directory |
| 140 | $i = 1; |
| 141 | $parts = pathinfo( $file ); |
| 142 | while ( file_exists( $file ) ) { |
| 143 | $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension']; |
| 144 | $i++; |
| 145 | } |
| 146 | |
| 147 | // Write file to filesystem |
| 148 | file_put_contents( $file, $image_data ); |
| 149 | |
| 150 | // Prepare and insert attachment |
| 151 | $wp_filetype = wp_check_filetype( basename( $file ), null ); |
| 152 | $attachment = [ |
| 153 | 'post_mime_type' => $wp_filetype['type'], |
| 154 | 'post_title' => !is_null( $title ) ? $title : preg_replace( '/\.[^.]+$/', '', basename( $file ) ), |
| 155 | 'post_content' => !is_null( $description ) ? $description : '', |
| 156 | 'post_status' => 'inherit', |
| 157 | 'post_excerpt' => !is_null( $caption ) ? $caption : '', |
| 158 | ]; |
| 159 | |
| 160 | $attach_id = wp_insert_attachment( $attachment, $file, $attachedPost ); |
| 161 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 162 | $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); |
| 163 | wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 164 | if ( !is_null( $alt ) ) { |
| 165 | update_post_meta( $attach_id, '_wp_attachment_image_alt', $alt ); |
| 166 | } |
| 167 | return $attach_id; |
| 168 | } |
| 169 | } |
| 170 |