Source Code for /public/04-includes-and-functions/cap-img.php

<?php
// ( this is the file cap-img.php )

/**
 * Renders a captioned image. Takes the following parameters:
 * - $url: The URL of the image to display.
 * - $alt: The alt text for the image.
 * - $caption: The caption to be displayed below the image.
 * - $max_width: The maximum width of the image in pixels;
 *               optional - defaults to 100 (pixels).
 */
function cap_img( $url, $alt, $caption, $max_width = 80 ) {
  ?>
  <figure>
    <a href="<?php echo $url; ?>">
      <img src="<?php echo $url; ?>"
          alt="<?php echo $alt; ?>"
        style="max-width: <?php echo $max_width; ?>px;
               height: auto;">
    </a>
    <figcaption><?php echo $caption; ?></figcaption>
  </figure>
  <?php
}