<?php
/**
* @package PhpOpenDocumentReports
* @author Eric Letard
* @copyright GPL License 2009
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 0.3
*/
/**
* class ImageOdtFilter
*
* Allows you to insert pictures in your odt file.
*
* Insert an image in your odt template.
* You must use the name of the picture to define all the parameters.
*
* Be carefull images used in the template are keeped in final file, so don't use too big ones.
*
* You can define the properties of the image in a script called "ImageOdt" in your ODT template.
* <pre>
* image_name.argument=$val['image']
* image_name.callback=genImageArray
* image_name.resizeOdt=0
* </pre>
* be carefull with resizeOdt, it's a string=> bool conversion so (bool)"false"=>true.
*
* @package PhpOpenDocumentReports
* @author Eric Letard
* @copyright GPL License 2009
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 0.3
*/
class ImageOdtFilter{
const PIXEL_TO_CM = 0.026458333;
static protected $__images=array();
/**
* set Configuration for image
* possibles properties are :
* <pre>
* - callback : static method or function returning path of image to insert
* - argument : argument givent to the callback function
* - resizeOdt: keep values of size of the template for the picture (possible values are 0|1)
* </pre>
* @param string $key
* @param string $property
* @param mixed $value
*/
public static function setImageProperty($key,$property,$value)
{
if(!isset(self::$__images[$key]))
self::$__images[$key]=array();
self::$__images[$key][$property]=$value;
}
/**
* return configuration values for the image
* @param string $key
* @return array
*/
public static function getImageData($key)
{
return (isset(self::$__images[$key]))?self::$__images[$key]:null;
}
/**
* update the buffer,
* replaces all images present in $__images by it's own callback function
* @param string $buffer
* @return string
*/
public static function filter(PODRFileString $data)
{
$stream=$data->getContent();
while(preg_match("@(.*)<text:script script:language=\"ImageOdt\">(.+?)</text:script>(.*)@sm",$stream,$matches))
{
$stream=$matches[1].$matches[3];
$config=str_replace("'","\"", html_entity_decode($matches[2],ENT_QUOTES));
$config=explode("\n", $config);
foreach($config as $line)
{
if(trim($line)!=='')
{
$key=strtok($line,'.');
$line=substr($line,strlen($key)+1);
list($property,$value)=explode("=", $line);
self::setImageProperty($key,$property,$value);
}
}
}
$stream=preg_replace_callback("@(<draw:frame.+?</draw:frame>)@sm", array("self","replaceImageTag"), $stream);
$data->setContent($stream);
return $data;
}
/**
*
* @param array $data preg_replace_callback results
* @return string php script
*/
protected static function replaceImageTag($data)
{
$imagexml=$data[1];
preg_match("@draw:name=\"([^\"]*)\"@sm",$imagexml,$matches);
$key=$matches[1];
if(($data=self::getImageData($key))!==null)
{
self::setImageProperty($key, 'xml', $imagexml);
return "<?php ImageOdtFilter::getImageXML('$key',".$data['argument']."); ?>";
}
else
return $imagexml;
}
/**
* function called while fetching
* @param string $key
* @param mixed $argument
*/
public static function getImageXML($key,$argument)
{
$imagepath="";
if(($data=self::getImageData($key))!==null)
{
if(is_array($argument))
{
if($data['callback']!==null)
$imagepath=call_user_func_array($data['callback'], $argument);
}
else
{
if($data['callback']!==null)
$imagepath=call_user_func($data['callback'], $argument);
else
$imagepath=$argument;
}
}
if($imagepath=="")
echo $data['xml'];
else
{
$i=preg_replace("@(draw:image xlink:href=\")([^\"]*)(\")@sm","\\1".self::publishOdtImage($imagepath)."\\3", $data['xml']);
if(is_file($imagepath)&&(boolean)$data['resizeOdt']==true)
{
$size = @getimagesize($imagepath);
if ($size === false) {
throw new ImageOdtFilterException("Invalid image");
}
list ($width, $height) = $size;
$width *= self::PIXEL_TO_CM;
$height *= self::PIXEL_TO_CM;
$i=preg_replace("@(draw:frame.*svg:width=\")([^\"]*)(\")@sm","\${1}".$width."cm\$3", $i);
$i=preg_replace("@(draw:frame.*svg:height=\")([^\"]*)(\")@sm","\${1}".$height."cm\$3", $i);
}
echo $i;
}
}
/**
* adds the picture in the odt file
* @param string $imagepath
* @return string
*/
public static function publishOdtImage($imagepath)
{
$key="Pictures/".md5($imagepath).strrchr($imagepath,'.');
PODRODT::addFile($imagepath,$key);
return $key;
}
}
?>