Example #1
0
 /**
  * Return a valid img tag for an image.
  *
  * @param File|Item $record
  * @param array $props
  * @param string $format
  * @return string
  */
 public function image_tag($record, $props, $format)
 {
     if (!$record) {
         return false;
     }
     if ($record instanceof File) {
         $filename = $record->getDerivativeFilename();
         $file = $record;
     } else {
         if ($record instanceof Item) {
             $item = $record;
             $file = get_db()->getTable('File')->getRandomFileWithImage($item->id);
             if (!$file) {
                 return false;
             }
             $filename = $file->getDerivativeFilename();
         } else {
             // throw some exception?
             return '';
         }
     }
     if ($file->hasThumbnail()) {
         $uri = html_escape($file->getWebPath($format));
     } else {
         $uri = img($this->_getFallbackImage($file));
     }
     /** 
      * Determine alt attribute for images
      * Should use the following in this order:
      * 1. alt option 
      * 2. file description
      * 3. file title 
      * 4. item title
      */
     $alt = '';
     if (isset($props['alt'])) {
         $alt = $props['alt'];
     } else {
         if ($fileTitle = metadata($file, 'display title')) {
             $alt = $fileTitle;
         }
     }
     $props['alt'] = $alt;
     $title = '';
     if (isset($props['title'])) {
         $title = $props['title'];
     } else {
         $title = $alt;
     }
     $props['title'] = $title;
     // Build the img tag
     $html = '<img src="' . $uri . '" ' . tag_attributes($props) . '/>' . "\n";
     return $html;
 }