$rendered['data'], 'mime' => $rendered['mime'], 'bytes' => strlen( $rendered['data'] ), 'source' => $source . '+scaled', ); } } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read. $binary = @file_get_contents( $path ); if ( false === $binary ) { return new WP_Error( 'read_failed', __( 'Could not read the image file.', 'ai-seo-tools' ) ); } return array( 'data' => base64_encode( $binary ), 'mime' => $mime ?: 'image/jpeg', 'bytes' => strlen( $binary ), 'source' => $source, ); } /** * Convenience wrapper returning a data URI. * * @return array{uri:string,bytes:int,source:string}|WP_Error */ public static function data_uri( int $attachment_id, int $max_dimension = 768 ) { $prepared = self::prepare( $attachment_id, $max_dimension ); if ( is_wp_error( $prepared ) ) { return $prepared; } return array( 'uri' => 'data:' . $prepared['mime'] . ';base64,' . $prepared['data'], 'bytes' => $prepared['bytes'], 'source' => $prepared['source'], 'mime' => $prepared['mime'], 'base64' => $prepared['data'], ); } /** * Chooses the cheapest already-generated file that is big enough. * * @return array{0:string,1:string}|WP_Error Path and a label describing where it came from. */ private static function pick_source( int $attachment_id, int $max_dimension ) { $original = get_attached_file( $attachment_id ); if ( ! $original || ! file_exists( $original ) ) { return new WP_Error( 'no_file', __( 'Could not locate the image file.', 'ai-seo-tools' ) ); } $meta = wp_get_attachment_metadata( $attachment_id ); if ( empty( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) { return array( $original, 'original' ); } $dir = trailingslashit( dirname( $original ) ); $best = null; foreach ( $meta['sizes'] as $name => $size ) { if ( empty( $size['file'] ) ) { continue; } $longest = max( (int) ( $size['width'] ?? 0 ), (int) ( $size['height'] ?? 0 ) ); // Too small to describe reliably. if ( $longest < min( 384, $max_dimension ) ) { continue; } if ( null === $best || $longest < $best['longest'] ) { $candidate = $dir . $size['file']; if ( file_exists( $candidate ) ) { $best = array( 'path' => $candidate, 'longest' => $longest, 'name' => $name, ); } } } if ( $best ) { return array( $best['path'], 'size:' . $best['name'] ); } return array( $original, 'original' ); } /** * True when the file's longest edge is over the budget. */ private static function exceeds( string $path, int $max_dimension ): bool { $size = @getimagesize( $path ); if ( ! $size ) { return false; } return max( (int) $size[0], (int) $size[1] ) > $max_dimension; } /** * Resizes (and optionally transcodes) into memory. * * @return array{data:string,mime:string}|WP_Error */ private static function render_scaled( string $path, int $max_dimension, bool $force_jpeg ) { $editor = wp_get_image_editor( $path ); if ( is_wp_error( $editor ) ) { return $editor; } $editor->resize( $max_dimension, $max_dimension, false ); $editor->set_quality( (int) apply_filters( 'ai_seo_tools_image_quality', 82 ) ); $mime = $force_jpeg ? 'image/jpeg' : null; $tmp = wp_tempnam( 'ai-seo-vision' ); if ( ! $tmp ) { return new WP_Error( 'tmp_failed', __( 'Could not create a temporary file for resizing.', 'ai-seo-tools' ) ); } $saved = $editor->save( $tmp, $mime ); if ( is_wp_error( $saved ) ) { wp_delete_file( $tmp ); return $saved; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local temp file. $binary = @file_get_contents( $saved['path'] ); wp_delete_file( $saved['path'] ); if ( $saved['path'] !== $tmp && file_exists( $tmp ) ) { wp_delete_file( $tmp ); } if ( false === $binary ) { return new WP_Error( 'read_failed', __( 'Could not read the resized image.', 'ai-seo-tools' ) ); } return array( 'data' => base64_encode( $binary ), 'mime' => $saved['mime-type'] ?? 'image/jpeg', ); } } }