settings = $settings; } /** * Whether the brand kit should be applied to a generation. * * An enabled but completely empty kit is not active. * * @return bool True when premium, enabled and at least one field is filled. */ public function is_active() { if ( !Providers::can_use_premium() ) { return false; } $kit = $this->get_kit(); if ( empty( $kit ) || empty( $kit['enabled'] ) ) { return false; } return $this->get_text( 'description' ) !== '' || $this->get_text( 'tone' ) !== '' || $this->get_text( 'avoid' ) !== '' || !empty( $this->get_colors() ) || (int) $this->get_value( 'reference_id', 0 ) > 0 || (int) $this->get_value( 'logo_id', 0 ) > 0; } /** * Build the brand fragment appended to the image prompt. * * @param bool $has_reference Whether a reference image is attached to the request. * @return string Fragment with a single leading space, or '' when inactive. */ public function get_prompt_fragment( $has_reference = false ) { if ( !$this->is_active() ) { return ''; } return ''; } /** * Read the reference image as an inline API payload. * * @return array|null array('mime_type' => string, 'data' => string) or null on any failure. */ public function get_reference_payload() { if ( $this->reference_resolved ) { return $this->reference_payload; } if ( !$this->is_active() ) { return null; } $this->reference_resolved = true; return null; } /** * Read an image attachment as a reference for an AI request, downscaled when it is * over the size limit. Used for the Brand Kit reference and for Product Images photos. * * @param int $attachment_id Attachment. * @param string $size Image size to read (full or large). * @return array|null array( 'mime_type', 'data' (base64), 'bytes' (raw), 'filename' ) or null. */ public function build_reference_payload( $attachment_id, $size = 'full' ) { $attachment_id = (int) $attachment_id; if ( $attachment_id <= 0 || !wp_attachment_is_image( $attachment_id ) ) { return null; } $path = $this->resolve_attachment_path( $attachment_id, $size ); if ( $path === '' ) { return null; } $mime = $this->detect_mime( $path ); if ( !$this->is_supported_mime( $mime ) ) { return null; } $temp_path = ''; $bytes_on_disk = @filesize( $path ); if ( $bytes_on_disk !== false && $bytes_on_disk > self::REFERENCE_MAX_BYTES ) { $downscaled = $this->downscale_reference( $path, $mime ); if ( is_array( $downscaled ) ) { $temp_path = $downscaled['path']; $path = $downscaled['path']; $mime = $downscaled['mime']; } } $bytes = @file_get_contents( $path ); if ( $temp_path !== '' && file_exists( $temp_path ) ) { wp_delete_file( $temp_path ); } if ( $bytes === false || $bytes === '' ) { return null; } $ext = array( 'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/webp' => 'webp', 'image/gif' => 'gif', ); return array( 'mime_type' => $mime, 'data' => base64_encode( $bytes ), 'bytes' => $bytes, 'filename' => 'product.' . (( isset( $ext[$mime] ) ? $ext[$mime] : 'png' )), ); } /** * Whether a logo is configured for overlay. * * @return bool */ public function has_logo() { if ( !$this->is_active() ) { return false; } return (int) $this->get_value( 'logo_id', 0 ) > 0; } /** * Overlay the brand logo onto an image file, in place. * * The file is a download_url() temp file with no extension, so the format is * detected from the bytes and preserved. Never throws, the caller keeps the * untouched image when this returns false. * * @param string $file_path Absolute path to the image to modify. * @return bool True when the file was rewritten with the logo. */ public function apply_logo_overlay( $file_path ) { if ( !$this->has_logo() ) { return false; } return false; } /** * Extra lines appended to the prompt-writing system prompt. * * @return string Newline-prefixed bullet lines, or '' when inactive. */ public function get_prompt_context_fragment() { if ( !$this->is_active() ) { return ''; } $fragment = ''; if ( $this->get_text( 'description' ) !== '' || !empty( $this->get_colors() ) ) { $fragment .= "\n- Brand colors and art style are applied separately. Do NOT mention any colors or art style in your prompt."; } $tone = $this->get_text( 'tone' ); if ( $tone !== '' ) { $fragment .= "\n- Choose a scene whose mood fits this brand tone: " . $tone; } $avoid = $this->normalize_avoid(); if ( $avoid !== '' ) { $fragment .= "\n- Never describe or include: " . $avoid; } return $fragment; } /** * Get the brand kit array, fetched once per request. * * @return array */ private function get_kit() { if ( $this->kit === null ) { $this->kit = (array) $this->settings->get_brand_kit(); } return $this->kit; } /** * Read a single brand kit value. * * @param string $key Key without the brand_kit_ prefix. * @param mixed $default Value returned when the key is absent. * @return mixed */ private function get_value( $key, $default = '' ) { $kit = $this->get_kit(); return ( isset( $kit[$key] ) ? $kit[$key] : $default ); } /** * Read a brand kit value as a trimmed string. * * @param string $key Key without the brand_kit_ prefix. * @return string */ private function get_text( $key ) { return trim( (string) $this->get_value( $key, '' ) ); } /** * Get the saved brand colors with empty entries removed. * * @return array List of hex codes. */ private function get_colors() { $colors = (array) $this->get_value( 'colors', array() ); $clean = array(); foreach ( $colors as $color ) { $color = trim( (string) $color ); if ( $color !== '' ) { $clean[] = $color; } } return $clean; } /** * Get the logo corner, falling back to the default. * * @return string One of top-left, top-right, bottom-left, bottom-right, center. */ private function get_position() { $position = (string) $this->get_value( 'logo_position', 'bottom-right' ); $allowed = array( 'top-left', 'top-right', 'bottom-left', 'bottom-right', 'center' ); return ( in_array( $position, $allowed, true ) ? $position : 'bottom-right' ); } /** * Normalize the avoid field into a de-duplicated comma-separated term list. * * @return string */ private function normalize_avoid() { $avoid = $this->get_text( 'avoid' ); if ( $avoid === '' ) { return ''; } $avoid = str_replace( array("\r\n", "\r", "\n"), ',', $avoid ); $terms = array(); foreach ( explode( ',', $avoid ) as $term ) { $term = trim( $term, " \t.-" ); if ( $term === '' ) { continue; } $key = strtolower( $term ); if ( !isset( $terms[$key] ) ) { $terms[$key] = $term; } } return implode( ', ', $terms ); } /** * Close a free-text value with a single full stop. * * @param string $text Text value. * @return string */ private function as_sentence( $text ) { return rtrim( $text, " .,;" ) . '.'; } /** * Whether a mime type can be handled by the image pipeline. * * @param string $mime Mime type. * @return bool */ private function is_supported_mime( $mime ) { return in_array( $mime, array('image/jpeg', 'image/png', 'image/webp'), true ); } /** * Detect an image mime type from its bytes, falling back to the filename. * * @param string $path Absolute file path. * @return string Mime type, or '' when undetectable. */ private function detect_mime( $path ) { $info = @getimagesize( $path ); if ( is_array( $info ) && !empty( $info['mime'] ) ) { return $info['mime']; } $type = wp_check_filetype( $path ); return ( !empty( $type['type'] ) ? $type['type'] : '' ); } /** * Resolve an attachment to a readable local path. * * Prefers an intermediate size so large originals are not read in full. * * @param int $attachment_id Attachment ID. * @param string $size Registered image size. * @return string Absolute path, or '' when nothing readable was found. */ /** * Width the logo should be scaled to for this image, in pixels. * * Derived from the shorter edge so the result is visually identical across every aspect ratio * and resolution the providers offer, then capped so it can never dominate either dimension. * * @param array $options Resolved overlay options (needs base_width, base_height, size). * @return int Target width in pixels, at least 1. */ private function target_logo_width( $options ) { $short_edge = ( isset( $options['short_edge'] ) ? (int) $options['short_edge'] : min( (int) $options['base_width'], (int) $options['base_height'] ) ); // Rounded at 15 significant digits first, like PHP before 8.4 and the settings preview (phpRound). // %.14e is used because %g follows the locale decimal separator. $box = (int) round( (float) sprintf( '%.14e', $short_edge * $options['size'] / 100 ) ); $max_box = (int) floor( min( $options['base_width'], $options['base_height'] ) * self::LOGO_MAX_EDGE_SHARE ); if ( $max_box > 0 && $box > $max_box ) { $box = $max_box; } return max( 1, $box ); } /** * Fit a logo's native dimensions inside the target box, preserving its aspect ratio. * * Scaling on width alone breaks non-square logos: a tall badge would end up taller than the * image (and get rejected outright), while a wide wordmark would collapse to a few pixels. * Bounding both edges keeps every logo shape usable on every aspect ratio. * * @param int $logo_width Native logo width. * @param int $logo_height Native logo height. * @param array $options Resolved overlay options. * @return array array('width' => int, 'height' => int), each at least 1. */ private function fit_logo_dimensions( $logo_width, $logo_height, $options ) { $logo_width = max( 1, (int) $logo_width ); $logo_height = max( 1, (int) $logo_height ); $box = $this->target_logo_width( $options ); $scale = min( $box / $logo_width, $box / $logo_height ); // A very elongated logo (a 25:1 wordmark, say) fits the box on its long edge while its // short edge collapses to a couple of pixels, and GD's resampler flattens that to fully // transparent, the overlay would report success while drawing nothing. Grow it back to a // legible minimum instead. $short_side = min( $logo_width, $logo_height ) * $scale; if ( $short_side > 0 && $short_side < self::LOGO_MIN_EDGE_PX ) { $scale *= self::LOGO_MIN_EDGE_PX / $short_side; } // Never upscale a logo past its native size, it would only look soft. if ( $scale > 1 ) { $scale = 1; } // Whatever the minimum did, the logo still may not overrun the image. $max_w = max( 1, (int) floor( $options['base_width'] * self::LOGO_MAX_EDGE_SHARE ) ); $max_h = max( 1, (int) floor( $options['base_height'] * self::LOGO_MAX_EDGE_SHARE ) ); $scale = min( $scale, $max_w / $logo_width, $max_h / $logo_height ); // Rounded at 15 significant digits first, like PHP before 8.4 and the settings preview (phpRound), // so 57.49999999999999 still gives 58 on PHP 8.4. %.14e is used because %g follows the locale. return array( 'width' => max( 1, (int) round( (float) sprintf( '%.14e', $logo_width * $scale ) ) ), 'height' => max( 1, (int) round( (float) sprintf( '%.14e', $logo_height * $scale ) ) ), ); } /** * Whether a loaded logo file has the same shape as the fitted size, within the * rounding a few-pixel-tall wordmark can carry. * * @param int $width Loaded file width. * @param int $height Loaded file height. * @param array $fit array('width' => int, 'height' => int). * @return bool */ private function same_logo_shape( $width, $height, $fit ) { $width = max( 1, (int) $width ); $height = max( 1, (int) $height ); $scale = $fit['width'] / $width; return abs( $height * $scale - $fit['height'] ) <= 1; } /** * Path to the smallest registered logo size that still covers the target width. * * Decoding the full-size original costs width * height * 4 bytes of GD memory, which on a * large portrait logo is enough to exhaust the PHP memory limit mid-generation, and a memory * fatal cannot be caught, so it would take the whole request down with it. * * @param int $target_width Width the logo will be scaled to, in pixels. * @return string Absolute path, or '' when the attachment is unusable. */ private function resolve_logo_path( $target_width ) { $attachment_id = (int) $this->get_value( 'logo_id', 0 ); if ( $attachment_id <= 0 ) { return ''; } $best = ''; $best_width = 0; // A hard-cropped size (a theme registering a square "medium", say) has a // different shape from the logo and would be stretched, so skip it. $full = wp_get_attachment_image_src( $attachment_id, 'full' ); $full_ratio = ( is_array( $full ) && !empty( $full[1] ) && !empty( $full[2] ) ? $full[1] / $full[2] : 0 ); foreach ( array('medium', 'medium_large', 'large') as $size ) { $src = wp_get_attachment_image_src( $attachment_id, $size ); if ( !is_array( $src ) || empty( $src[1] ) || $src[1] < $target_width ) { continue; } if ( $full_ratio > 0 && (empty( $src[2] ) || abs( $src[1] / $src[2] - $full_ratio ) / $full_ratio > 0.02) ) { continue; } if ( $best === '' || $src[1] < $best_width ) { $path = $this->resolve_attachment_path( $attachment_id, $size ); if ( $path !== '' ) { $best = $path; $best_width = (int) $src[1]; } } } if ( $best !== '' ) { return $best; } return $this->resolve_attachment_path( $attachment_id, 'full' ); } /** * Resolve an attachment size to a local file path. * * @param int $attachment_id Attachment ID. * @param string $size Registered image size. * @return string Absolute path, or '' when it cannot be resolved. */ private function resolve_attachment_path( $attachment_id, $size ) { $src = wp_get_attachment_image_src( $attachment_id, $size ); if ( is_array( $src ) && !empty( $src[0] ) ) { $uploads = wp_upload_dir(); if ( empty( $uploads['error'] ) && !empty( $uploads['baseurl'] ) && !empty( $uploads['basedir'] ) ) { // Compare protocol-relative: the stored URL and the current // request scheme disagree on mixed http/https installs. $baseurl = preg_replace( '#^https?:#', '', $uploads['baseurl'] ); $url = preg_replace( '#^https?:#', '', $src[0] ); if ( strpos( $url, $baseurl ) === 0 ) { $candidate = $uploads['basedir'] . substr( $url, strlen( $baseurl ) ); if ( file_exists( $candidate ) && is_readable( $candidate ) ) { return $candidate; } } } } $attached = get_attached_file( $attachment_id ); if ( $attached && file_exists( $attached ) && is_readable( $attached ) ) { return $attached; } return ''; } /** * Write a downscaled copy of a reference image to a temp file. * * @param string $path Absolute path to the source image. * @param string $mime Source mime type. * @return array|false array('path' => string, 'mime' => string) or false on failure. */ private function downscale_reference( $path, $mime ) { $editor = wp_get_image_editor( $path ); if ( is_wp_error( $editor ) ) { return false; } $resized = $editor->resize( self::REFERENCE_MAX_EDGE, self::REFERENCE_MAX_EDGE, false ); if ( is_wp_error( $resized ) ) { return false; } if ( !function_exists( 'wp_tempnam' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $temp_path = wp_tempnam( 'aifi-brand-reference.png' ); $saved = $editor->save( $temp_path, $mime ); if ( is_wp_error( $saved ) || empty( $saved['path'] ) || !file_exists( $saved['path'] ) ) { if ( file_exists( $temp_path ) ) { wp_delete_file( $temp_path ); } return false; } // The editor rewrites the extension from the mime type, so the bytes usually land on a // different path than the placeholder wp_tempnam() created, remove the orphan. if ( $saved['path'] !== $temp_path && file_exists( $temp_path ) ) { wp_delete_file( $temp_path ); } return array( 'path' => $saved['path'], 'mime' => ( !empty( $saved['mime-type'] ) ? $saved['mime-type'] : $mime ), ); } /** * Composite the logo with ImageMagick. * * @param string $file_path Absolute path to the base image. * @param string $logo_path Absolute path to the logo image. * @param array $options Resolved overlay options. * @return bool */ private function overlay_with_imagick( $file_path, $logo_path, $options ) { $base = null; $logo = null; try { $base = new \Imagick($file_path); $logo = new \Imagick($logo_path); $fit = ( isset( $options['logo_fit'] ) && $logo->getImageWidth() >= $options['logo_fit']['width'] && $this->same_logo_shape( $logo->getImageWidth(), $logo->getImageHeight(), $options['logo_fit'] ) ? $options['logo_fit'] : $this->fit_logo_dimensions( $logo->getImageWidth(), $logo->getImageHeight(), $options ) ); $logo->scaleImage( $fit['width'], $fit['height'] ); $logo_width = $logo->getImageWidth(); $logo_height = $logo->getImageHeight(); if ( $logo_width < 1 || $logo_height < 1 || $logo_width > $options['base_width'] || $logo_height > $options['base_height'] ) { $this->log( 'logo overlay skipped: scaled logo does not fit the image.' ); return false; } if ( $options['opacity'] < 100 ) { // A logo without an alpha channel ignores the multiply otherwise. if ( defined( 'Imagick::ALPHACHANNEL_ACTIVATE' ) ) { $logo->setImageAlphaChannel( constant( 'Imagick::ALPHACHANNEL_ACTIVATE' ) ); } elseif ( defined( 'Imagick::ALPHACHANNEL_ON' ) ) { $logo->setImageAlphaChannel( constant( 'Imagick::ALPHACHANNEL_ON' ) ); } $logo->evaluateImage( \Imagick::EVALUATE_MULTIPLY, $options['opacity'] / 100, \Imagick::CHANNEL_ALPHA ); } $offset = $this->get_offset( $options, $logo_width, $logo_height ); $base->compositeImage( $logo, \Imagick::COMPOSITE_OVER, $offset['x'], $offset['y'] ); $base->setImageFormat( $this->mime_to_imagick_format( $options['mime'] ) ); $blob = $base->getImageBlob(); if ( !is_string( $blob ) || $blob === '' ) { return false; } return file_put_contents( $file_path, $blob ) !== false; } catch ( \ImagickException $e ) { $this->log( 'ImageMagick logo overlay failed: ' . $e->getMessage() ); return false; } catch ( \Exception $e ) { $this->log( 'ImageMagick logo overlay failed: ' . $e->getMessage() ); return false; } finally { if ( $logo instanceof \Imagick ) { $logo->clear(); $logo->destroy(); } if ( $base instanceof \Imagick ) { $base->clear(); $base->destroy(); } } } /** * Composite the logo with GD. * * @param string $file_path Absolute path to the base image. * @param string $logo_path Absolute path to the logo image. * @param array $options Resolved overlay options. * @return bool */ private function overlay_with_gd( $file_path, $logo_path, $options ) { if ( !function_exists( 'imagecreatefromstring' ) || !function_exists( 'imagescale' ) ) { return false; } $base_bytes = @file_get_contents( $file_path ); $logo_bytes = @file_get_contents( $logo_path ); if ( $base_bytes === false || $logo_bytes === false ) { return false; } // A large generated image needs more memory than the default limit once decoded. if ( function_exists( 'wp_raise_memory_limit' ) ) { wp_raise_memory_limit( 'image' ); } $base = @imagecreatefromstring( $base_bytes ); if ( $base === false ) { return false; } $logo = @imagecreatefromstring( $logo_bytes ); if ( $logo === false ) { imagedestroy( $base ); return false; } // A palette logo (PNG-8, GIF, what image optimisers emit) carries its transparency in a // palette index, which imagescale() drops. Copy it onto a fully transparent truecolor // canvas instead: imagecopy() skips the transparent index and keeps per-entry alpha, so // the transparent area never composites as a solid black rectangle. if ( !imageistruecolor( $logo ) ) { $palette_width = imagesx( $logo ); $palette_height = imagesy( $logo ); $truecolor = imagecreatetruecolor( $palette_width, $palette_height ); if ( $truecolor === false ) { imagedestroy( $base ); imagedestroy( $logo ); return false; } imagealphablending( $truecolor, false ); imagesavealpha( $truecolor, true ); imagefill( $truecolor, 0, 0, imagecolorallocatealpha( $truecolor, 0, 0, 0, 127 ) ); imagecopy( $truecolor, $logo, 0, 0, 0, 0, $palette_width, $palette_height ); imagedestroy( $logo ); $logo = $truecolor; } $fit = ( isset( $options['logo_fit'] ) && imagesx( $logo ) >= $options['logo_fit']['width'] && $this->same_logo_shape( imagesx( $logo ), imagesy( $logo ), $options['logo_fit'] ) ? $options['logo_fit'] : $this->fit_logo_dimensions( imagesx( $logo ), imagesy( $logo ), $options ) ); $scaled = $this->scale_logo_gd( $logo, $fit['width'], $fit['height'], $options['opacity'] ); imagedestroy( $logo ); if ( $scaled === false ) { imagedestroy( $base ); return false; } $logo_width = imagesx( $scaled ); $logo_height = imagesy( $scaled ); if ( $logo_width > $options['base_width'] || $logo_height > $options['base_height'] ) { $this->log( 'logo overlay skipped: scaled logo does not fit the image.' ); imagedestroy( $base ); imagedestroy( $scaled ); return false; } if ( function_exists( 'imagepalettetotruecolor' ) ) { imagepalettetotruecolor( $base ); } $offset = $this->get_offset( $options, $logo_width, $logo_height ); imagealphablending( $base, true ); imagecopy( $base, $scaled, $offset['x'], $offset['y'], 0, 0, $logo_width, $logo_height ); imagedestroy( $scaled ); $temp_path = $file_path . '.aifi-logo'; $written = false; if ( $options['mime'] === 'image/webp' && function_exists( 'imagewebp' ) ) { imagesavealpha( $base, true ); $written = @imagewebp( $base, $temp_path, 90 ); } elseif ( $options['mime'] === 'image/png' ) { imagesavealpha( $base, true ); $written = @imagepng( $base, $temp_path ); } elseif ( $options['mime'] === 'image/jpeg' ) { $written = @imagejpeg( $base, $temp_path, 90 ); } imagedestroy( $base ); if ( $written && file_exists( $temp_path ) && filesize( $temp_path ) > 0 ) { $copied = @copy( $temp_path, $file_path ); wp_delete_file( $temp_path ); return $copied; } if ( file_exists( $temp_path ) ) { wp_delete_file( $temp_path ); } $this->log( 'GD logo overlay failed to encode ' . $options['mime'] . '.' ); return false; } /** * Scale a GD logo resource and apply opacity. * * imagecopymerge() is not usable here, it discards the PNG alpha channel, * so the opacity is applied per pixel instead. * * @param resource|\GdImage $logo Source logo image. * @param int $target_width Width in pixels. * @param int $target_height Height in pixels. * @param int $opacity Opacity percentage (10-100). * @return resource|\GdImage|false */ private function scale_logo_gd( $logo, $target_width, $target_height, $opacity ) { imagealphablending( $logo, false ); imagesavealpha( $logo, true ); $scaled = @imagescale( $logo, $target_width, $target_height ); if ( $scaled === false ) { return false; } imagealphablending( $scaled, false ); imagesavealpha( $scaled, true ); if ( $opacity >= 100 ) { return $scaled; } $width = imagesx( $scaled ); $height = imagesy( $scaled ); $faded = imagecreatetruecolor( $width, $height ); if ( $faded === false ) { return $scaled; } imagealphablending( $faded, false ); imagesavealpha( $faded, true ); imagefill( $faded, 0, 0, imagecolorallocatealpha( $faded, 0, 0, 0, 127 ) ); $factor = $opacity / 100; for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { // Both images are truecolor, so pack the pixel directly. imagecolorallocatealpha() // can return false when memory is tight, and imagesetpixel(false) silently leaves a // transparent pixel, which shows up as torn bands across the overlay. $color = imagecolorat( $scaled, $x, $y ); // GD alpha runs 0 (opaque) to 127 (transparent). $alpha = 127 - (int) round( (127 - ($color >> 24 & 0x7f)) * $factor ); $alpha = max( 0, min( 127, $alpha ) ); imagesetpixel( $faded, $x, $y, $alpha << 24 | $color & 0xffffff ); } } imagedestroy( $scaled ); return $faded; } /** * Work out the top-left corner of the logo inside the base image. * * @param array $options Resolved overlay options. * @param int $logo_width Scaled logo width. * @param int $logo_height Scaled logo height. * @return array array('x' => int, 'y' => int) */ private function get_offset( $options, $logo_width, $logo_height ) { $margin = ( isset( $options['margin_px'] ) ? (int) $options['margin_px'] : (int) $options['margin'] ); switch ( $options['position'] ) { case 'top-left': $x = $margin; $y = $margin; break; case 'top-right': $x = $options['base_width'] - $logo_width - $margin; $y = $margin; break; case 'bottom-left': $x = $margin; $y = $options['base_height'] - $logo_height - $margin; break; case 'center': $x = ($options['base_width'] - $logo_width) / 2; $y = ($options['base_height'] - $logo_height) / 2; break; default: $x = $options['base_width'] - $logo_width - $margin; $y = $options['base_height'] - $logo_height - $margin; break; } // Positive x moves right, positive y moves down, from wherever the position put the logo. $x += ( isset( $options['offset_x_px'] ) ? (int) $options['offset_x_px'] : 0 ); $y += ( isset( $options['offset_y_px'] ) ? (int) $options['offset_y_px'] : 0 ); // Clamp on both sides so an offset can nudge the logo but never push it off the image. return array( 'x' => max( 0, min( $options['base_width'] - $logo_width, (int) round( $x ) ) ), 'y' => max( 0, min( $options['base_height'] - $logo_height, (int) round( $y ) ) ), ); } /** * Map a mime type to its ImageMagick format name. * * @param string $mime Mime type. * @return string */ private function mime_to_imagick_format( $mime ) { $formats = array( 'image/jpeg' => 'JPEG', 'image/png' => 'PNG', 'image/webp' => 'WEBP', ); return ( isset( $formats[$mime] ) ? $formats[$mime] : 'PNG' ); } /** * Log a brand kit failure while debugging. * * @param string $message Message to log. */ private function log( $message ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( 'AIFI Brand Kit: ' . $message ); } } }