Sådan benyttes komponenten Img klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Img.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Img::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Img($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Img klassen
Den fulde PHP kildekode for Img klassen
<?php/** * @package base * @see HTML_BASE_UTIL_PATH.'/Img.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');if (defined('HTML_BASIC_UTIL_PATH')) { require_once(HTML_BASIC_UTIL_PATH.'/Message.php');}if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * Returns a complete Image as HTML * Note: If you specify "\r\n" in the alt tag, then a break is automatically added * <code> * <img name="$name" src="$src" width="$width" height="$height" * alt="$alt" class="$class" border="$border" /> * Usage: * $img = new Img($src, $width, $height, $alt, $class, $border, $aux); * print $img->getHtml(); * Or * Img::display($src, $width, $height, $alt, $class, $border, $aux); * </code> * @package base */class Img extends Html { /** * @var String $src The path to the image */ protected $src = ''; /** * @var String $width The width of the image */ protected $width = ''; /** * @var String $height The height of the image */ protected $height = ''; /** * @var String $alt The alt text for the image */ protected $alt = ''; /** * @var String $class The css class name for the image */ protected $class = ''; /** * @var String $border The border for the image */ protected $border = ''; /** * @var String $aux The aux attributes for the complete image */ protected $aux = ''; /** * @var String $name The name for the image */ protected $name = ''; /** * Constructor * @param String $src The source path for the image i.e. /logo.gif * @param String $width The width or null or '' * @param String $height The height or null or '' * @param String $alt The alt text * @param String $class The css class for the image * @param String $border The border of an image * @param String $aux The new line indicator (LINK_LAYOUT_BR) */ function __construct($src='', $width='', $height='', $alt='', $class='', $border='', $aux='') { parent::__construct(); $this->src = $src; $this->width = $width; $this->height = $height; $this->class = $class; $this->border = $border; $this->aux = $aux; $this->alt = $alt; } /** * Calculate the width and height, if not specified * Skip the calculation if the src contains * 'http://' OR does not have a '/' in the src * OR the height or width have already been defined */ function calculateWidthHeight() { $strHttp = 'http:/'.'/'; if (strpos($this->src,'/')===false || strpos($this->src, $strHttp)!==false || strpos($this->src,'?')!==false || // Used in Thumb.php $this->width!=='' || $this->height!=='') { // Skip } else { // Ignore } } /** * Get the complete html for an image * @return String the html */ function getHtml() { $html = $this->html; $this->calculateWidthHeight(); if ($this->src != '') { if ($this->alt=='') { // xhtml 1.0 strict compliant if (!empty($GLOBALS['pageKeyword'])) {// $this->alt = $GLOBALS['pageKeyword']; $this->alt = basename($this->src); } else { $this->alt = basename($this->src); } } if (strpos($this->alt,"\r\n")!==false) { // Does only work in IE, not FireFox $this->alt = str_replace("\r\n",'
', $this->alt); // <br> } $html .= '<img'; $html .= $this->getAttribute('name'); $html .= $this->getAttribute('src'); $html .= $this->getAttribute('width'); $html .= $this->getAttribute('height'); $html .= $this->getAttribute('alt'); $html .= $this->getAttribute('class'); $html .= $this->getAttribute('border'); $html .= " />"; if ($this->aux != '') { if ($this->aux & (LINK_LAYOUT_LI | LINK_LAYOUT_BR)) { // Ok } else { $msg = $this->getClassName().'(), Illegal aux, expected [ LINK_LAYOUT_LI | LINK_LAYOUT_BR ], found='.$this->aux; if (defined('HTML_LOG_UTIL_PATH')) { Log::error($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } } else { // Ok } $html .= $this->aux & LINK_LAYOUT_BR ? "<br />\r\n" : ''; } else { $html .= "<!-- ".$this->getClassName()."->getHtml() src is empty -->\r\n"; } return $html; } /** * Display html * <code> * Usage: * Img::display($src, $width, $height, $alt, $class, $border, $aux); * </code> * @static * @param String $src The source path for the image * @param String $width The width or null or '' * @param String $height The height or null or '' * @param String $alt The alt text * @param String $class The css class for the image * @param String $border The border of an image * @param String $aux The new line indicator (nl) */ public static function display($src='', $width='', $height='', $alt='', $class='', $border='', $aux='') { $html = new Img($src, $width, $height, $alt, $class, $border, $aux); $html->addHtml(); }}?>
Den fulde HTML kildekode for Img klassen
<? <!-- DEBUG: Img --> <img src="http://finn-rasmussen.com/images/aniBee.gif" width="20" height="20" alt="Sample demo" /> ?>
Her er 'klasse metoderne' for Img klassen:
Her er 'objekt variable' for Img klassen: