Source for file LImage.php
Documentation is available at LImage.php
* @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
* @copyright 2005, diemeisterei GmbH. All rights reserved.
* @author $Author: schmunk $
* @version $Revision: 625 $ $Date: 2007-02-25 02:05:09 +0100 (Sun, 25 Feb 2007) $
* @package Phundament.Components
* @subpackage WebControls
* This is a combination of an extended TImage and a wrapper for ImageMagick functions
* @package System.Web.UI.WebControls
* @todo define max dimensions for image rendering!!! PERFORMANCE & MEMORY
* @return string the URL of the image file
return TPropertyValue :: ensureString($this->getViewState('ImageUrl', ''));
* @param string the URL of the image file
$this->setViewState('ImageUrl', $value, '');
$this->setViewState('Resize', TPropertyValue :: ensureBoolean($value), '');
return $this->getViewState('Resize', true);
$this->setViewState('DestWidth', TPropertyValue :: ensureInteger($value), '');
return $this->getViewState('DestWidth');
$this->setViewState('DestHeight', TPropertyValue :: ensureInteger($value), '');
return $this->getViewState('DestHeight');
$this->setViewState('DestQuality', TPropertyValue :: ensureInteger($value), '');
return $this->getViewState('DestQuality', "50");
$this->setViewState('DestType', TPropertyValue :: ensureString(strtoupper($value)), '');
return $this->getViewState('DestType', 'jpg');
$this->setViewState('DestColorSpace', TPropertyValue :: ensureString($value), '');
return $this->getViewState('DestColorSpace', "rgb");
$this->setViewState('ShowMimeTypeImage', TPropertyValue :: ensureBoolean($value), '');
return $this->getViewState('ShowMimeTypeImage', false);
$this->setViewState('BackgroundColor', TPropertyValue :: ensureString($value), '');
return $this->getViewState('BackgroundColor', null);
$this->setViewState('PageNumber', TPropertyValue :: ensureInteger($value), 0);
return $this->getViewState('PageNumber', 0);
$this->setViewState('IsRendered', TPropertyValue :: ensureBoolean($value), false);
return $this->getViewState('IsRendered', false);
$this->setViewState('PxEmFactor', $value);
return $this->getViewState('PxEmFactor', false);
if ($t= $this->Application->Parameters['AbortRemainingExecutionTime'])
public function onInit($param)
#Prado :: trace("LImage onInit starting ... ", "Lithron.LImage");
$param= $this->Application->getParameters();
$this->OutputDir= $param->itemAt("OutputDir");
parent :: onInit($param);
#Prado :: trace("LImage onInit complete", "Lithron.LImage");
$this->findImageMagick();
if (empty ($this->BinPath))
Prado :: log("ImageMagick not found", 32, "Lithron.LImage");
public function onLoad($param)
parent :: onLoad($param);
parent :: onPreRender($param);
// check if we should abort rendering
$settings= $this->Application->getModule("php_settings");
if ($settings instanceof TModule)
$t= $settings->getRemainingExecutionTime();
$this->Controls[]= "image not yet rendered";
$this->ImageUrl= "_IMAGE_NOT_YET_RENDERED_";
Prado :: log("Rendering aborted, only $t sec(s) time left", TLogger :: INFO, "Lithron.LImage");
// assume rendered image when url points to tmp path, otherwise postback problems
if (!strstr($this->ImageUrl, $this->OutputDir))
$info= getimagesize($this->Application->BasePath . "/../" . $this->ImageUrl);
$this->Width= ($info[0] / 10) . "em";
$this->Height= ($info[1] / 10) . "em";
Prado :: log($e->getMessage(), 2, "Lithron.LImage");
if (empty ($this->BinPath))
$publishedUrl= $this->publishFilePath($this->getImageUrl());
Prado :: log($e->getMessage(), 8);
return $this->identify($file);
* Searches ImageMagick in file system
private function findImageMagick()
if ($this->BinPath= $this->Application->getGlobalState("LImage:BinPath"))
putenv("PATH=" . $this->BinPath);
$param= $this->Application->getParameters();
$paths= explode(";", $param->itemAt("ImagickSearchPaths"));
foreach ($paths AS $path)
exec($path . "/identify -version", $output);
if (isset ($output[0]) && strstr($output[0], "ImageMagick"))
$this->BinPath= $path . "/";
$this->Application->setGlobalState("LImage:BinPath", $this->BinPath);
* Invokes image generation
private function generateImage()
#Prado :: trace("Generating image ... ", "Lithron.LImage");
Prado :: log("File '" . $this->SrcUrl . "' missing", 8, "Lithron.LImage");
$this->setImageUrl($this->publishAsset("LImage/mimetypes/missing.png"));
#$file= $this->generateHash(); #.".".$outputType;
if (is_file($outputDir . "/" . $file))
#Prado :: trace("Using cached file: {$file} <= '{$this->SrcUrl}' ", "Lithron.LImage");
$this->Info= $this->identify($this->SrcRealpath);
$outputRealpath= $outputDir . "/" . $file;
$clientUrl= $this->OutputDir . "/" . $file;
$this->generateAlternativeImage($this->SrcRealpath, $outputRealpath);
$this->convert($this->SrcRealpath, $outputRealpath);
$this->generateAlternativeImage($this->SrcRealpath, $outputRealpath);
Prado :: log("OutputDir '" . $outputDir . "' is not writable", 3, "Lithron.LImage");
* Creates a unique fingerprint of the destination file
private function generateHash()
if ($cache= $this->Application->Cache)
#$idString= $cache->get("LImage:" . $this->SrcUrl);
Prado :: trace("Creating new md5 for {$this->SrcUrl} ... ", "Lithron.LImage");
// useless performance debug ? wrong microtimes !?
if ($this->ShowMimeTypeImage === false)
$file_contents= file_get_contents($this->SrcUrl);
#$file_contents = $this->SrcUrl;
$idString= md5($file_contents);
$idString= strrchr($this->SrcUrl, ".");
#Prado :: trace("Md5 for {$this->SrcUrl} created", "Lithron.LImage");
$cache->set("LImage:" . $this->SrcUrl, $idString, 360);
foreach ($_props AS $prop)
$_eval= "return(\$this->get" . $prop . "());";
$idString .= "|" . $value;
#Prado :: trace("$idString: $hash", "Lithron.LImage");
* Returns file information
private function identify($file)
$f= "size_short:%b;width:%w;height:%h;type:%m;colorspace:%r;transparent:;resolution_x:%x;resolution_y:%y;colors:%k;quantum:%q;image_depth:%z;";
$cmd= $this->BinPath . "identify -format \"" . $f . "\" ' $file' [0 ]";
#Prado :: log("Executing $cmd", TLogger :: INFO, "Lithron.Limage");
$dataArray= explode(";", $data[0]);
foreach ($dataArray AS $pair)
$array= split(":", $pair);
$return[$array[0]]= @ $array[1];
Prado :: log("Could not identify '" . $file . "'", 4, "Lithron.LImage");
private function convert($src, $dest)
if (isset ($this->Info['Error']))
Prado :: log("ImageMagick error: " . $this->Info['Error'], 16, "Lithron.LImage");
$cmd= $this->BinPath . "convert ";
switch ($this->Info["type"])
// change output type for special image formats
$cmd .= " - density 144 x144 \"{ $src}[{ $page}]\" ";
#$cmd .= " -resample 72x72 ";
$cmd .= " \"{ $src}[{ $page}]\" ";
if ($this->Info['transparent'])
$cmd .= " -colorspace transparent ";
if (strstr($this->Info["colorspace"], "CMYK") && $this->DestColorspace != "CMYK")
#echo "COLORSPACE CONVERSION<br>";
$cmd .= "-profile \"" . dirname(__FILE__) . "/EuroscaleUncoated.icc\" ";
$cmd .= "-profile \"" . dirname(__FILE__) . "/sRGB.icm\" ";
$cmd .= "-colorspace RGB";
Prado :: log("Executing $cmd", TLogger :: INFO, "Lithron.Limage");
* Tries to create an image from file extension and mimetype images
private function generateAlternativeImage($src, $dest)
$mimeIcon= "LImage/mimetypes/" . $this->SrcExt . ".png";
$altImg= $this->publishAsset($mimeIcon);
$altImg= $this->publishAsset("LImage/mimetypes/mime-empty.png");
$this->convert(realpath($altImg), $dest);
$cmd= $this->BinPath . "convert ";
$cmd .= "-background white -fill gray ";
$cmd .= "- pointsize 24 label:{ $this->SrcExt} ";
Prado :: trace("executing $cmd", "Lithron.Limage");
|