You are here

function theme_flickr_photo in Flickr 6

Same name and namespace in other branches
  1. 5 flickr.module \theme_flickr_photo()
  2. 7 flickr.module \theme_flickr_photo()
3 theme calls to theme_flickr_photo()
theme_flickr_block_photo in block/flickr_block.module
theme_flickr_filter_photo in filter/flickr_filter.module
theme_flickr_photoset in ./flickr.module

File

./flickr.module, line 216

Code

function theme_flickr_photo($photo, $size = NULL, $format = NULL, $attributes = NULL) {

  // Get 'class' and 'rel' link attributes to trigger an overlay browser.
  $class = variable_get('flickr_class', '');
  $rel = variable_get('flickr_rel', '');

  // URL of the image to open to enlarge.
  $url = flickr_photo_img($photo, variable_get('flickr_opening_size', ''), $format);

  // The image as HTML to render.
  $img = flickr_img($photo, $size, $attributes);

  // URL of the image to display.
  $img_url = flickr_photo_img($photo, $size);

  // If the image is a square we know the width (avoids to make a request).
  switch ($size) {
    case 's':
      $width = '75';
      break;
    case 'q':
      $width = '150';
      break;
  }

  // If it is not a square.
  if (!isset($width)) {

    // Get the real width of the image.
    list($width) = getimagesize($img_url);
  }

  // URL of the photo page on Flickr.
  $photo_url = flickr_photo_page_url($photo['owner'], $photo['id']);

  // Get the Flickr image title for set data or the photo itself. Sanatize.
  $title = is_array($photo['title']) ? str_replace('"', "'", strip_tags($photo['title']['_content'])) : $photo['title'];
  $info = flickr_photo_get_info($photo['id']);

  // Use title if description does not exist, else sanitize the description.
  $description = !empty($info['description']['_content']) ? str_replace('"', "'", htmlspecialchars_decode(strip_tags($info['description']['_content']))) : $title;

  // Real name if it exists or go with the username. Link to Flickr user page.
  $username = !empty($info['owner']['realname']) ? l($info['owner']['realname'], 'https://www.flickr.com/photos/' . $info['owner']['nsid'], array(
    'attributes' => array(
      'title' => t('View user on Flickr.'),
      'target' => '_blank',
    ),
  )) : l($info['owner']['username'], 'https://www.flickr.com/photos/' . $info['owner']['nsid'], array(
    'attributes' => array(
      'title' => t('View user on Flickr.'),
      'target' => '_blank',
    ),
  ));

  // The date an image was taken formatted as 'time ago'.
  $taken = isset($info['dates']['taken']) ? format_interval(time() - strtotime($info['dates']['taken']), 1) . ' ' . t('ago') : '';

  // A bunch of geo data.
  $neighbourhood = isset($info['location']['neighbourhood']['_content']) ? strip_tags($info['location']['neighbourhood']['_content']) . ', ' : '';
  $locality = isset($info['location']['locality']['_content']) ? strip_tags($info['location']['locality']['_content']) . ', ' : '';
  $region = isset($info['location']['region']['_content']) ? strip_tags($info['location']['region']['_content']) . ', ' : '';
  $country = isset($info['location']['country']['_content']) ? strip_tags($info['location']['country']['_content']) : '';
  $location = !empty($country) ? ' ' . t('at') . ' ' . $neighbourhood . $locality . $region . $country : '';

  // Compose extra Flickr info as HTML to render.
  $metadata = '<br />' . $taken . $location . ' ' . t('by') . ' ' . $username;

  // Image width < 100 px is too small for most titles. Can be set differently.
  $credit = $width < variable_get('flickr_title_suppress_on_small', '100') ? t('Flickr') : $title;

  // Image width < 150 px is too small for extra info. Can be set differently.
  $metadatacaption = $width < variable_get('flickr_metadata_suppress_on_small', '150') ? '' : $metadata;

  // Determine what info goes with the enlarged version of the image.
  $overlay = variable_get('flickr_info_overlay', array(
    'title' => 'title',
    'metadata' => 'metadata',
    'description' => 'description',
  ));
  $overlaytitle = gettype($overlay['title']) == 'integer' ? '' : $title . ' - ';
  $metadata = gettype($overlay['metadata']) == 'integer' ? '' : $metadata . ' - ';
  $description = gettype($overlay['description']) == 'integer' || $overlaytitle == $description . ' - ' ? '' : $description;
  $overlayhtml = $overlaytitle . $metadata . $description;

  // If 'class' or 'rel' attribute are defined (overlay browser), use a caption.
  if (variable_get('flickr_class', '') == NULL && variable_get('flickr_rel', '') == NULL) {

    // Final step when NO overlay browser is used that generates the image that
    // links to the Flickr page to comply with the TOS of Flickr.
    return l($img, $photo_url, array(
      'attributes' => array(
        'title' => $title,
      ),
      'absolute' => TRUE,
      'html' => TRUE,
    ));
  }
  else {

    // Final step when an overlay browser is used that generates the image with
    // a link to the bigger version and a link to the Flickr page under it to
    // comply with the TOS of Flickr.
    return '<span class="flickr-wrap">' . l($img, $url, array(
      'attributes' => array(
        'title' => $overlayhtml,
        'class' => $class,
        'rel' => $rel,
      ),
      'html' => TRUE,
    )) . '<span class="flickr-credit" style="width: ' . ($width - variable_get('flickr_caption_padding', '0')) . 'px;">' . l($credit, $photo_url, array(
      'attributes' => array(
        'title' => t('View on Flickr. To enlarge click image.'),
        'target' => '_blank',
      ),
      'html' => TRUE,
    )) . $metadatacaption . '</span></span>';
  }
}