How can a counter value be padded with zeros in PHP for image output?

When outputting a counter value in PHP for image output, you may want to pad the value with zeros to ensure a consistent width. This can be achieved using the str_pad() function in PHP. By specifying the total width and the padding character (in this case, '0'), you can ensure that the counter value is displayed with leading zeros if needed.

$counter = 5; // Example counter value
$padded_counter = str_pad($counter, 3, '0', STR_PAD_LEFT); // Pad the counter value with zeros to a total width of 3
echo $padded_counter; // Output the padded counter value