How can alt text for images be automatically generated using the filename in PHP?

When images are uploaded to a website, it's important to provide alt text for accessibility purposes. One way to automatically generate alt text for images is to use the filename of the image itself. This can be done in PHP by extracting the filename, removing the file extension, and then formatting the text as needed.

// Get the filename of the image
$imagePath = "path/to/image.jpg";
$imageFilename = pathinfo($imagePath, PATHINFO_FILENAME);

// Remove any special characters or underscores
$imageAltText = str_replace('_', ' ', $imageFilename);

// Capitalize the first letter of each word
$imageAltText = ucwords($imageAltText);

echo $imageAltText; // Output: "Image Name"