Source for file Image.php
Documentation is available at Image.php
// +---------------------------------------------------------------------------+
// | PEAR :: XML :: Transformer :: Image Namespace Handler |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
// +---------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License, |
// | that is available at http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +---------------------------------------------------------------------------+
require_once 'XML/Transformer/Namespace.php';
require_once 'XML/Util.php';
define('PEAR_XML_TRANSFORMER_IMAGE_FONTPATH', '/usr/X11R6/lib/X11/fonts/truetype');
define('PEAR_XML_TRANSFORMER_IMAGE_cacheDir', '/cache/gtext');
* Handler for the Image Namespace.
* require_once 'XML/Transformer_OutputBuffer.php';
* require_once 'XML/Transformer/Namespace/Image.php';
* $t = new XML_Transformer_OutputBuffer;
* $t->overloadNamespace('img', new XML_Transformer_Namespace_Image);
* <!-- Height and Width attributes are autogenerated -->
* <img:img src="somepng.png" alt="A sample image" />
* <!-- Set default for all subsequent <img:gtext /> -->
* <img:gtextdefault bgcolor="888888" fgcolor="#000000"
* font="arial.ttf" fontsize="33"
* split="" cacheable="yes" />
* <!-- Render Text as PNG image -->
* <img alt="A sample image" height="33" src="somepng.png" width="133" />
* src="/cache/gtext/8b91aee0403c5cdccc1dd96bd4f49fbb.png" /></span>
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
* @package XML_Transformer
var $_imgAttributes = array ();
var $_gtextAttributes = array ();
var $_gtextDefaultAttributes = array ();
// {{{ function start_img($attributes)
$this->_imgAttributes = $attributes;
// {{{ function end_img($cdata)
$src = $this->_truePath ($this->_imgAttributes['src']);
$this->_imgAttributes['height'] = $h;
$this->_imgAttributes['width'] = $w;
XML_Util ::attributesToString ($this->_imgAttributes)
// {{{ start_gtext($attributes)
foreach ($this->_gtextDefaultAttributes as $k => $v) {
if (! isset ($attributes[$k])) {
$this->_gtextAttributes = $attributes;
// {{{ function end_gtext($cdata)
if(!is_file($this->_gtextAttributes['font']))
$this->_gtextAttributes['font'] .
switch ($this->_gtextAttributes['split']) {
foreach ($text as $index => $word) {
$text[$index] = " $word";
foreach ($text as $index => $word) {
if ($word == ' ' || $word == '') {
$text[$index] = chr(160 );
$text = array (0 => $cdata);
foreach ($text as $index => $word) {
$baseline = $this->_baseline (
$this->_gtextAttributes['font'],
$this->_gtextAttributes['fontsize']
$src = $this->_createImage ($word,$baseline);
$alt = $this->_createAlt ($word);
'<%simg src="%s" alt="%s" />',
($this->_prefix[0 ] != '&MAIN') ? $this->_prefix[0 ] . ':' : '',
return " <span>$r</span>";
// {{{ function start_gtextdefault($attributes)
$this->_gtextDefaultAttributes = $attributes;
// {{{ function end_gtextdefault()
// {{{ function _baseline($font, $size)
function _baseline ($font, $size) {
// {{{ function _colorString($color)
function _colorString ($color) {
if (substr($color, 0 , 1 ) == '#') {
return array ($r, $g, $b);
// {{{ function _createAlt($word)
function _createAlt ($word) {
if (isset ($this->_gtextAttributes['alt'])) {
return $this->_gtextAttributes['alt'];
// {{{ function _createImage($word, $baseline)
function _createImage ($word, $baseline) {
$font = isset ($this->_gtextAttributes['font']) ? $this->_gtextAttributes['font'] : 'arial.ttf';
$fh = isset ($this->_gtextAttributes['fontsize']) ? $this->_gtextAttributes['fontsize'] : 12;
$bgcolor = isset ($this->_gtextAttributes['bgcolor']) ? $this->_gtextAttributes['bgcolor'] : '#ffffff';
$fgcolor = isset ($this->_gtextAttributes['fgcolor']) ? $this->_gtextAttributes['fgcolor'] : '#ffffff';
$antialias = isset ($this->_gtextAttributes['antialias']) ? $this->_gtextAttributes['antialias'] : 'yes';
$transparency = isset ($this->_gtextAttributes['transparency']) ? $this->_gtextAttributes['transparency'] : 'yes';
$cacheable = isset ($this->_gtextAttributes['cacheable']) ? $this->_gtextAttributes['cacheable'] : 'yes';
$spacing = isset ($this->_gtextAttributes['spacing']) ? $this->_gtextAttributes['spacing'] : 2;
$border = isset ($this->_gtextAttributes['border']) ? $this->_gtextAttributes['border'] : 0;
$bordercolor = isset ($this->_gtextAttributes['bordercolor']) ? $this->_gtextAttributes['bordercolor'] : '#ff0000';
/* The cache name is derived from all attributes and cdata.
* This is very conserative and may create to many cachefiles,
* but better to err on the safe side.
$cachefile = md5(XML_Util ::attributesToString ($this->_gtextAttributes) . ':' . $word) . '.png';
$cacheDir = $_SERVER['DOCUMENT_ROOT']
$cacheName = " $cacheDir/$cachefile";
/* Don't do the same work twice. */
$w = max(1/10* $fh, abs($r[2 ] - $r[0 ]));
$h = max(1 , abs($r[7 ] - $r[1 ]));
$www = $w + 2* ($spacing+ $border);
$hhh = $fh + 2* ($spacing+ $border);
$im = ImageCreate ($www, $hhh);
list ($r, $g, $b) = $this->_colorString ($bgcolor);
$bg = ImageColorAllocate ($im, $r, $g, $b);
if ($transparency != 'no') {
ImageColorTransparent ($im, $bg);
list ($r, $g, $b) = $this->_colorString ($fgcolor);
$fg = ImageColorAllocate ($im, $r, $g, $b);
if ($antialias == 'no') {
list ($r, $g, $b) = $this->_colorString ($bordercolor);
$bo = ImageColorAllocate ($im, $r, $g, $b);
ImageFilledRectangle ($im, 0 , 0 , $www, $hhh, $bg);
for ($i= $border; $i>=0; $i-- ) {
ImageRectangle ($im, $x1, $y1, $x2, $y2, $bo);
$hhh- (2+ $y+ $spacing+ $border),
ImagePNG ($im, $cacheName);
// {{{ function _truePath($path)
function _truePath ($path) {
$uri = apache_lookup_uri ($path);
return $_SERVER['DOCUMENT_ROOT'] . '/' . $path;
* vim600: et sw=2 ts=2 fdm=marker
Documentation generated on Mon, 11 Mar 2019 15:48:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|