| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Services; |
| 4 |
|
| 5 |
use AATXT\App\AIProviders\Contracts\SupportsImageValidation; |
| 6 |
|
| 7 |
/** |
| 8 |
* Service for validating images before alt text generation. |
| 9 |
* |
| 10 |
* This service centralizes image validation logic, checking MIME types |
| 11 |
* and other image properties before attempting to generate alt text. |
| 12 |
*/ |
| 13 |
final class ImageValidator |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Validate that a MIME type is in the allowed list. |
| 17 |
* |
| 18 |
* @param string $mimeType The MIME type to validate |
| 19 |
* @param array<string> $allowedTypes List of allowed MIME types |
| 20 |
* @return bool True if valid, false otherwise |
| 21 |
*/ |
| 22 |
public function validateMimeType(string $mimeType, array $allowedTypes): bool |
| 23 |
{ |
| 24 |
return in_array($mimeType, $allowedTypes, true); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the MIME type of a WordPress attachment. |
| 29 |
* |
| 30 |
* @param int $attachmentId The attachment post ID |
| 31 |
* @return string The MIME type, or empty string if not found |
| 32 |
*/ |
| 33 |
public function getImageMimeType(int $attachmentId): string |
| 34 |
{ |
| 35 |
$mimeType = get_post_mime_type($attachmentId); |
| 36 |
return $mimeType !== false ? $mimeType : ''; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Check if an attachment is an image. |
| 41 |
* |
| 42 |
* @param int $attachmentId The attachment post ID |
| 43 |
* @return bool True if the attachment is an image |
| 44 |
*/ |
| 45 |
public function isImage(int $attachmentId): bool |
| 46 |
{ |
| 47 |
return wp_attachment_is_image($attachmentId); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Validate an image against a provider's supported MIME types. |
| 52 |
* |
| 53 |
* @param int $attachmentId The attachment post ID |
| 54 |
* @param SupportsImageValidation $provider The provider to validate against |
| 55 |
* @return bool True if the image is supported by the provider |
| 56 |
*/ |
| 57 |
public function validateForProvider(int $attachmentId, SupportsImageValidation $provider): bool |
| 58 |
{ |
| 59 |
if (!$this->isImage($attachmentId)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$mimeType = $this->getImageMimeType($attachmentId); |
| 64 |
|
| 65 |
if (empty($mimeType)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
return $provider->supportsImage($mimeType); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get a human-readable list of supported formats. |
| 74 |
* |
| 75 |
* @param array<string> $mimeTypes List of MIME types |
| 76 |
* @return string Comma-separated list of formats (e.g., "png, jpeg, gif") |
| 77 |
*/ |
| 78 |
public function formatMimeTypeList(array $mimeTypes): string |
| 79 |
{ |
| 80 |
return str_replace('image/', '', implode(', ', $mimeTypes)); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get the image URL for a WordPress attachment. |
| 85 |
* |
| 86 |
* @param int $attachmentId The attachment post ID |
| 87 |
* @return string The image URL, or empty string if not found |
| 88 |
*/ |
| 89 |
public function getImageUrl(int $attachmentId): string |
| 90 |
{ |
| 91 |
$url = wp_get_attachment_url($attachmentId); |
| 92 |
return $url !== false ? $url : ''; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get image dimensions. |
| 97 |
* |
| 98 |
* @param int $attachmentId The attachment post ID |
| 99 |
* @return array{width: int, height: int}|null Width and height, or null if not available |
| 100 |
*/ |
| 101 |
public function getImageDimensions(int $attachmentId): ?array |
| 102 |
{ |
| 103 |
$metadata = wp_get_attachment_metadata($attachmentId); |
| 104 |
|
| 105 |
if (!$metadata || !isset($metadata['width'], $metadata['height'])) { |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
return [ |
| 110 |
'width' => (int) $metadata['width'], |
| 111 |
'height' => (int) $metadata['height'], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the file size of an attachment in bytes. |
| 117 |
* |
| 118 |
* @param int $attachmentId The attachment post ID |
| 119 |
* @return int|null File size in bytes, or null if not available |
| 120 |
*/ |
| 121 |
public function getFileSize(int $attachmentId): ?int |
| 122 |
{ |
| 123 |
$filePath = get_attached_file($attachmentId); |
| 124 |
|
| 125 |
if (!$filePath || !file_exists($filePath)) { |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
$size = filesize($filePath); |
| 130 |
return $size !== false ? $size : null; |
| 131 |
} |
| 132 |
} |
| 133 |
|