<?php
/**
 * Image Alt Text Automation
 * Generates SEO-friendly alt text for images
 * Place in: /helpers/image_alt_helper.php
 */

class ImageAltHelper {
    
    /**
     * Generate alt text from filename
     */
    public static function generateFromFilename($filename) {
        // Get filename without extension
        $name = pathinfo($filename, PATHINFO_FILENAME);
        // Replace hyphens and underscores with spaces
        $alt = str_replace(['-', '_'], ' ', $name);
        // Capitalize words
        $alt = ucwords($alt);
        return htmlspecialchars($alt, ENT_QUOTES, 'UTF-8');
    }
    
    /**
     * Generate product image alt text
     */
    public static function generateProductAlt($productName, $colorName = null, $imageType = 'product') {
        $alt = 'Product image';
        
        if ($productName) {
            $alt = htmlspecialchars($productName, ENT_QUOTES, 'UTF-8');
            
            if ($colorName) {
                $alt .= ' in ' . htmlspecialchars($colorName, ENT_QUOTES, 'UTF-8');
            }
            
            $alt .= ' - ' . $imageType;
        }
        
        return $alt;
    }
    
    /**
     * Generate category image alt text
     */
    public static function generateCategoryAlt($categoryName) {
        if (!$categoryName) {
            return 'Category image';
        }
        return htmlspecialchars($categoryName, ENT_QUOTES, 'UTF-8') . ' category';
    }
    
    /**
     * Generate blog image alt text
     */
    public static function generateBlogAlt($blogTitle, $imagePosition = 'featured') {
        if (!$blogTitle) {
            return 'Blog image';
        }
        
        $alt = htmlspecialchars($blogTitle, ENT_QUOTES, 'UTF-8');
        
        if ($imagePosition === 'featured') {
            $alt .= ' - featured image';
        } elseif ($imagePosition === 'thumbnail') {
            $alt .= ' - thumbnail';
        }
        
        return $alt;
    }
    
    /**
     * Get image dimensions for responsive optimization
     */
    public static function getImageDimensions($imagePath) {
        if (file_exists($imagePath)) {
            $size = getimagesize($imagePath);
            if ($size) {
                return ['width' => $size[0], 'height' => $size[1]];
            }
        }
        return false;
    }
    
    /**
     * Generate srcset for responsive images
     */
    public static function generateSrcset($imagePath, $sizes = []) {
        if (empty($sizes)) {
            $sizes = [320, 640, 1024, 1280];
        }
        
        $srcset = [];
        foreach ($sizes as $size) {
            $srcset[] = htmlspecialchars($imagePath, ENT_QUOTES) . " {$size}w";
        }
        
        return implode(', ', $srcset);
    }
}

/**
 * Quick helper function
 */
function getImageAlt($filename, $customAlt = null) {
    if ($customAlt) {
        return htmlspecialchars($customAlt, ENT_QUOTES, 'UTF-8');
    }
    return ImageAltHelper::generateFromFilename($filename);
}
?>
