- <?php
-
-
- /**
- * Class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @version $Revision: 269 $ $Date: 2006-06-12 08:29:29 +0200 (Mo, 12 Jun 2006) $
- * @package Lithron.LImage
- * @subpackage WebControls
- */
-
- /**
- * This is a combination of an extended TImage and a wrapper for ImageMagick functions
- *
- * @package System.Web.UI.WebControls
- */
- class LImage extends TImage
- {
- private $SrcUrl;
- private $SrcRealpath;
- private $DestUrl;
- private $DestRealpath;
- private $OutputDir;
- private $SrcExt;
-
- private $BinPath;
- private $Info;
-
- public function setResize($value)
- {
- $this->setViewState('Resize', TPropertyValue :: ensureBoolean($value), '');
- }
- public function getResize()
- {
- return $this->getViewState('Resize', true);
- }
-
- public function setDestWidth($value)
- {
- $this->setViewState('DestWidth', TPropertyValue :: ensureInteger($value), '');
- }
- public function getDestWidth()
- {
- return $this->getViewState('DestWidth');
- }
-
- public function setDestHeight($value)
- {
- $this->setViewState('DestHeight', TPropertyValue :: ensureInteger($value), '');
- }
- public function getDestHeight()
- {
- return $this->getViewState('DestHeight');
- }
-
- public function setDestQuality($value)
- {
- $this->setViewState('DestQuality', TPropertyValue :: ensureInteger($value), '');
- }
- public function getDestQuality()
- {
- return $this->getViewState('DestQuality', "50");
- }
-
- public function setDestType($value)
- {
- $this->setViewState('DestType', TPropertyValue :: ensureString(strtoupper($value)), '');
- }
- public function getDestType()
- {
- return $this->getViewState('DestType');
- }
-
- public function setDestColorSpace($value)
- {
- $this->setViewState('DestColorSpace', TPropertyValue :: ensureString($value), '');
- }
- public function getDestColorSpace()
- {
- return $this->getViewState('DestColorSpace', "rgb");
- }
-
- public function setShowMimeTypeImage($value)
- {
- $this->setViewState('ShowMimeTypeImage', TPropertyValue :: ensureBoolean($value), '');
- }
- public function getShowMimeTypeImage()
- {
- return $this->getViewState('ShowMimeTypeImage', false);
- }
- public function setDestBackgroundColor($value)
- {
- $this->setViewState('BackgroundColor', TPropertyValue :: ensureString($value), '');
- }
- public function getDestBackgroundColor()
- {
- return $this->getViewState('BackgroundColor', null);
- }
-
-
- /**
- * @ignore
- */
- 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");
- }
-
- function __construct()
- {
- $this->findImageMagick();
- if (empty($this->BinPath)){
- Prado::log("ImageMagick not found",8,"Lithron.LImage");
- return;
- }
- }
-
- /**
- * @ignore
- */
- public function onPreRender($param)
- {
- if (empty($this->BinPath)){
- return;
- }
- if ($this->getResize() == true)
- {
- $this->generateImage();
- }
- parent :: onPreRender($param);
- }
-
- public function doIdentify($file)
- {
- return $this->identify($file);
- }
-
- /**
- * Searches ImageMagick in file system
- */
- private function findImageMagick()
- {
- if ($this->BinPath= $this->Application->getGlobalState("LImage:BinPath"))
- {
- putenv("PATH=" . $this->BinPath);
- return;
- }
-
- $param= $this->Application->getParameters();
- $paths= explode(";", $param->itemAt("ImagickSearchPaths"));
- foreach ($paths AS $path)
- {
- if (!is_dir($path)) continue;
- #echo $path;exit;
- exec($path . "/identify -version", $output);
- if (isset ($output[0]) && strstr($output[0], "ImageMagick"))
- {
- $this->BinPath= $path . "/";
- $this->Application->setGlobalState("LImage:BinPath", $this->BinPath);
- return;
- #break;
- }
- #$this->BinPath;
- }
-
- }
-
- /**
- * Invokes image generation
- */
- private function generateImage()
- {
- #Prado :: trace("Generating image ... ", "Lithron.LImage");
-
- $this->SrcUrl= $this->getImageUrl();
- $this->SrcRealpath= realpath($this->getImageUrl());
- $this->SrcExt= substr(strrchr($this->SrcUrl, "."), 1);
-
- // check if missing
- if (!is_file($this->SrcUrl))
- {
- Prado :: log("File '" . $this->SrcUrl . "' missing", 3, "Lithron.LImage");
- $this->setImageUrl($this->publishAsset("mimetypes/missing.png"));
- return;
- }
-
- // output folder
- $outputDir= realpath($this->OutputDir);
-
- // check cache
- $file= $this->generateHash(); #.".".$outputType;
- if (is_file($outputDir . "/" . $file))
- {
- #Prado :: trace("Using cached file: {$file} <= '{$this->SrcUrl}' ", "Lithron.LImage");
- $this->setImageUrl($this->OutputDir . "/" . $file);
- return;
- }
-
- // get file info
- $this->Info= $this->identify($this->SrcRealpath);
-
- if (is_writable($outputDir))
- {
- $outputRealpath= $outputDir . "/" . $file;
- $clientUrl= $this->OutputDir . "/" . $file;
- // generate image
- if ($this->getShowMimeTypeImage() === true)
- {
- $this->generateAlternativeImage($this->SrcRealpath, $outputRealpath);
- }
- else
- {
- $this->convert($this->SrcRealpath, $outputRealpath);
- if (!is_file($outputRealpath))
- {
- $this->generateAlternativeImage($this->SrcRealpath, $outputRealpath);
- }
- }
-
- $this->setImageUrl($clientUrl);
- }
- else
- {
- 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);
- }
- if (empty ($idString))
- {
- $idString= md5_file($this->SrcUrl);
- Prado :: trace("Creating new md5 for {$this->SrcUrl}", "Lithron.LImage");
- if ($cache)
- {
- $cache->set("LImage:" . $this->SrcUrl, $idString, 360);
- }
- }
- $_props= array (
- "DestWidth",
- "DestHeight",
- "DestType",
- "DestQuality",
- "DestColorSpace",
- "DestBackgroundColor",
- "ShowMimeTypeImage"
- );
-
- foreach ($_props AS $prop)
- {
- $_eval= "return(\$this->get" . $prop . "());";
- $value= eval ($_eval);
- $idString .= "|" . $value;
- }
- $hash= md5($idString);
- #Prado :: trace("$idString: $hash", "Lithron.LImage");
- return $hash;
- }
-
- /**
- * 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]";
- exec($cmd, $data);
- if (isset ($data[0]))
- {
- $dataArray= explode(";", $data[0]);
- foreach ($dataArray AS $pair)
- {
- $array= split(":", $pair);
- $return[$array[0]]= @ $array[1];
- }
- return $return;
- }
- else
- {
- Prado :: log("Could not identify '" . $file . "'", 3, "Lithron.LImage");
- }
-
- }
-
- /**
- * Converts image
- */
- private function convert($src, $dest)
- {
- $page= "0";
- $cmd= $this->BinPath . "convert ";
-
- switch ($this->Info["type"])
- {
- // change output type for special image formats
- case "PSD" :
- case "PDF" :
- case "PS" :
- $this->setDestType("PNG");
- $cmd .= " -density 144x144 \"{$src}[{$page}]\" ";
- #$cmd .= " -resample 72x72 ";
- break;
-
- default :
- $cmd .= " \"{$src}[{$page}]\" ";
- }
-
- if ($this->Info['transparent'])
- $cmd .= " -colorspace transparent ";
-
- if ($this->Info["colorspace"] == "CMYK")
- {
- echo "COLORSPACE CONVERSION<br>";
- $cmd .= "-profile \"" . dirname(__FILE__) . "/EuroscaleUncoated.icc\" ";
- $cmd .= "-profile \"" . dirname(__FILE__) . "/sRGB.icm\" ";
- $cmd .= "-colorspace RGB";
- }
- if ($this->getDestWidth() || $this->getDestHeight())
- {
- $cmd .= " -geometry {$this->getDestWidth()}x{$this->getDestHeight()} ";
- }
- if ($this->getDestBackgroundColor() !== null)
- {
- $cmd .= " -background {$this->getDestBackgroundColor()} ";
- }
-
- $cmd .= " +profile \"*\" -quality {$this->getDestQuality()} ";
-
- if ($this->getDestType())
- {
- $cmd .= " {$this->getDestType()}:";
- }
- $cmd .= "\"{$dest}\" ";
-
- Prado :: trace("executing $cmd", "Lithron.Limage");
-
- exec($cmd, $output);
- #var_dump($output);
- }
-
- /**
- * Tries to create an image from file extension and mimetype images
- */
- private function generateAlternativeImage($src, $dest)
- {
- $mimeIcon= "mimetypes/" . $this->SrcExt . ".png";
- if (is_file(dirname(__FILE__) . "/" . $mimeIcon))
- {
- $altImg= $this->publishAsset($mimeIcon);
- }
- else
- {
- $altImg= $this->publishAsset("mimetypes/mime-empty.png");
- }
- $this->convert(realpath($altImg), $dest);
- $this->setImageUrl($dest);
- return;
-
- $cmd= $this->BinPath . "convert ";
- $cmd .= "-background white -fill gray ";
- #$cmd .= "-font Arial ";
- $cmd .= "-pointsize 24 label:{$this->SrcExt} ";
- $cmd .= "{$dest} ";
- Prado :: trace("executing $cmd", "Lithron.Limage");
-
- exec($cmd, $output);
- }
- }