Image Alt Text Batch Helper
Finds images missing useful alt text and builds concise repair suggestions from image context.
Purpose
Finds images missing useful alt text and builds concise repair suggestions from image context.
Snippet details
ContextSeoLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Image Alt Text Batch Helper.
*
* Purpose:
* Finds images missing useful alt text and builds concise repair suggestions from image context.
*
* @param array $image_rows Image rows with src, alt, and context values.
* @return array Images needing alt repair.
*/
function ogSnippetImageAltTextBatchHelper(array $image_rows): array {
$repairs = array();
foreach ($image_rows as $image_row) {
$src = '';
if (isset($image_row['src'])) {
$src = trim((string) $image_row['src']);
}
$alt = '';
if (isset($image_row['alt'])) {
$alt = trim((string) $image_row['alt']);
}
$context = '';
if (isset($image_row['context'])) {
$context = trim((string) $image_row['context']);
}
if ($src === '') {
continue;
}
if ($alt === '' || strtolower($alt) === 'image' || strlen($alt) < 8) {
$suggestion = 'Describe '.$context;
if ($context === '') {
$suggestion = 'Describe the image purpose';
}
$repairs[] = array(
'src' => $src,
'current_alt' => $alt,
'suggested_alt' => $suggestion
);
}
}
return $repairs;
}
$image_rows = array(
array('src' => '/img/defiant.webp', 'alt' => 'image', 'context' => 'Defiant tactical console'),
array('src' => '/img/serenity.webp', 'alt' => 'Serenity ship in orbit', 'context' => 'Firefly hero image')
);
$alt_repairs = ogSnippetImageAltTextBatchHelper($image_rows);
echo 'Alt repairs: '.count($alt_repairs);