Sådan benyttes komponenten Images klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Images.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Images::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Images($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Images klassen
Den fulde PHP kildekode for Images klassen
<?php/** * @package base * @see HTML_BASE_UTIL_PATH.'/Images.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_UTIL_PATH.'/Image.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 * The Src, Width, Height and Alt attributes are fetched from global storage, if defined * @see $GLOBALS[IMAGE_NAME_ALT][IMAGE_PRINT] = LINK_TITLE_PRINT; * Assume default: /images/*.gif * <code> * Usage: * $name = IMAGE_PRINT; // 'print' * $width = 32; * $height = 24; * $alt = LINK_TITLE_PRINT; // 'some title text' * $class = CSS_LINK_COLOR; // 'baseLinkColor' * $border = 0; * $aux = ""; // LINK_LAYOUT_LI | LINK_LAYOUT_BR * $suffix = IMAGE_SUFFIX_GIF; // 'gif', 'ico' or 'jpg' * * $images = new Images($name, $width, $height, $alt, $class, $border, $aux, $suffix); * print $images->getHtml(); * Or * Images::display($name, $width, $height, $alt, $class, $border, $aux, $suffix); * </code> * @package base */class Images extends Image { /** * @var String $suffix The suffix to use. [ gif | jpg | ico ] */ protected $suffix = ''; /** * Constructor * @param String $name The name of the image i.e. IMAGE_PRINT * @param String $width The width * @param String $height The height * @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) * @param String $suffix The suffix to use [ gif | jpg | ico ] */ function __construct($name='', $width='', $height='', $alt='', $class='', $border='', $aux='', $suffix='') { $this->suffix = $suffix != '' ? $suffix : IMAGE_SUFFIX_GIF; $src = ''; $theSrc = $this->getSrc($src, $name); $theWidth = $this->getWidth($width, $name); $theHeight = $this->getHeight($height, $name); $theAlt = $this->getAlt($alt, $name); $theClass = $this->getClass($class, $name); parent::__construct($theSrc, $theWidth, $theHeight, $theAlt, $theClass, $border, $aux); } /** * Make a sanity check, that we have a valid suffix for the image */ private function sanityCheck() { if ($this->suffix == IMAGE_SUFFIX_GIF || $this->suffix == IMAGE_SUFFIX_JPG || $this->suffix == IMAGE_SUFFIX_ICO || $this->suffix == IMAGE_SUFFIX_PNG || $this->suffix == IMAGE_SUFFIX_JPEG) { // OK } else { $msg = $this->getClassName().'(), suffix must be one of [ gif | jpg | jpeg | ico ], found suffix='.$this->suffix; if (defined('HTML_LOG_UTIL_PATH')) { Log::error($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } } /** * Get the full path or subpath to the image located in the standard '/images' folder * If a image flag is selected, then prepend the IMAGE_FLAG_PATH subpath * <code> * $name = IMAGE_PRINT; // 'print' * $value = $this->getFullSrc($name); * // value is now "/print.gif" * Or * $name = IMAGE_GERMANY; * $value = $this->getFullSrc($name); * // value is now "/flags/de.gif" * </code> * @param String $name The name of the image to use * @return String The attribute of the image for the specified key */ private function getFullSrc($name) { $value = ""; /** * If no path or suffix is part of the attribute * Then it is one of the usual images in the standard '/images' folder */ switch ($name) { case IMAGE_DENMARK: case IMAGE_GERMANY: case IMAGE_ENGLAND: case IMAGE_FRANCE: case IMAGE_ITALY: case IMAGE_NORWAY: case IMAGE_SWEDEN: case IMAGE_USA: $value .= IMAGE_FLAG_PATH; // Intentional fall through, the sub folder is '/images/flags' default: // OK, is in the standard '/images' folder $value .= '/'.$name.".".$this->suffix; break; } return $value; } /** * Get the Image Name of the image, if this exists in the globals array * <code> * define('IMAGE_NAME_SRC', $INDEX++); * $image = new Images(); * $imagename = $image->getImageName(IMAGE_NAME_SRC); // Yes, I know, it is a private function * </code> * @param String $key The key attribute for the image to use * @return String The name of the image for the specified key */ private function getImageName($key) { $imagename = "NAME NOT IN GLOBALS"; if (array_key_exists($key, $GLOBALS)) { $imagename = $GLOBALS[$key]; if (is_array($imagename)) { // OK } else { // Should NEVER end up here $msg = $this->getClassName().'->getImageName($key), $imagename is NOT an array, for $key='.$key.' found $imagename='.$imagename; if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } } else { // Should NEVER end up here $msg = $this->getClassName().'->getImageName($key), $key is NOT found in GLOBALS array, for $key='.$key; if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } return $imagename; } /** * Get the image attribute for the specified 'name' * from the GLOBALS array with the 'key' or use the default 'value' * <code> * Usage: * $value = ''; * $name = IMAGE_PRINT; // name of image. I.e. 'print' * $key = IMAGE_NAME_SRC; // key in the GLOBALS array. I.e. 'src' * $text = $this->getImageName($value, $name, $key); * </code> * @see $GLOBALS[IMAGE_NAME_ALT][IMAGE_PRINT] = LINK_TITLE_PRINT; * @param String $value The value to use, if supplied * @param String $name The name of the image to use * @param String $key The key attribute for the image to use * @return String The attribute of the image for the specified key */ private function getImageAttribute($value, $name, $key) { $attribute = $value; if ($value == '') { $imagename = $this->getImageName($key); if (is_array($imagename) && array_key_exists($name, $imagename)) { $attribute = $GLOBALS[$key][$name]; } else { // OK, Just ignore// $msg = $this->getClassName().'(), Not found in GLOBALS array, for name='.$name." and key=".$key;// if (defined('HTML_LOG_UTIL_PATH')) {// Log::info($msg, __FILE__, __LINE__);// }// if (defined('HTML_BASIC_UTIL_PATH')) {// Message::add($msg, __FILE__, __LINE__);// } } } else { // Use the $value, if specified and NOT empty } /** * Special handling for the 'Src' attribute * The src could be in the standard '/images' path or subpath */ if ($key == IMAGE_NAME_SRC && $attribute == "") { $attribute = $this->getFullSrc($name); } return $attribute; } /** * Get the image attribute for the Src or use the name of the image * Speciel handling when the flags are used * @param String $src The Src to use, if supplied * @param String $name The name of the image to use * @return String The value of the image Src attribute */ public function getSrc($src, $name) { return $this->getImageAttribute($src, $name,IMAGE_NAME_SRC); } /** * Get the image attribute for the Width or use the name of the image * @param String $width The Width to use, if supplied * @param String $name The name of the image to use * @return String The value of the image Width attribute */ private function getWidth($width, $name) { return $this->getImageAttribute($width, $name,IMAGE_NAME_WIDTH); } /** * Get the image attribute for the Height or use the name of the image * @param String $height The Height to use, if supplied * @param String $name The name of the image to use * @return String The value of the image Height attribute */ private function getHeight($height, $name) { return $this->getImageAttribute($height, $name,IMAGE_NAME_HEIGHT); } /** * Get the image attribute for the Alt or use the name of the image * @param String $alt The Alt to use, if supplied * @param String $name The name of the image to use * @return String The value of the image Alt attribute */ private function getAlt($alt, $name) { return $this->getImageAttribute($alt, $name, IMAGE_NAME_ALT); } /** * Get the image attribute for the Class or use the name of the image * @param String $class The Class to use, if supplied * @param String $name The name of the image to use * @return String The value of the image Alt attribute */ private function getClass($class, $name) { return $this->getImageAttribute($class, $name, IMAGE_NAME_CLASS); } /** * Get a new Images object from the specified Image name * <code> * Usage: * $image = Images::newImage($name, $width, $height, $alt, $class, $border, $aux); * </code> * @static * @param String $name The name of the image i.e. IMAGE_PRINT * @param String $width The width * @param String $height The height * @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) * @return Images Return a new instance of the Images object */ public static function newImage($name='', $width='', $height='', $alt='', $class='', $border='', $aux='') { return new Images($name, $width, $height, $alt, $class, $border, $aux); } /** * Display html * <code> * Usage: * $name = IMAGE_PRINT; // 'print' * $width = 32; * $height = 24; * $alt = LINK_TITLE_PRINT; // 'some title text' * $class = CSS_LINK_COLOR; // 'baseLinkColor' * $border = 0; * $aux = ""; // LINK_LAYOUT_LI | LINK_LAYOUT_BR * $suffix = IMAGE_SUFFIX_GIF; // 'gif', 'ico' or 'jpg' * * Images::display($name, $width, $height, $alt, $class, $border, $aux, $suffix); * </code> * @static * @param String $name The name of the image i.e. IMAGE_PRINT * @param String $width The width * @param String $height The height * @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) * @param String $suffix The suffix to use [ gif | jpg | ico ] */ public static function display($name='', $width='', $height='', $alt='', $class='', $border='', $aux='', $suffix='') { $html = new Images($name, $width, $height, $alt, $class, $border, $aux, $suffix); $html->addHtml(); }}?>
Den fulde HTML kildekode for Images klassen
<? <!-- DEBUG: Images --> <img src="http://skadedyr.info/images/aniBee.gif" alt="aniBee.gif" /> ?>
Her er 'klasse metoderne' for Images klassen:
Her er 'objekt variable' for Images klassen: