add amendments metric
[contractdashboard.git] / lib / jpgraph / jpgraph_imgtrans.php
blob:a/lib/jpgraph/jpgraph_imgtrans.php -> blob:b/lib/jpgraph/jpgraph_imgtrans.php
<?php <?php
//======================================================================= //=======================================================================
// File: JPGRAPH_IMGTRANS.PHP // File: JPGRAPH_IMGTRANS.PHP
// Description: Extension for JpGraph to do some simple img transformations // Description: Extension for JpGraph to do some simple img transformations
// Created: 2003-09-06 // Created: 2003-09-06
// Ver: $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $ // Ver: $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $
// //
// Copyright (c) Aditus Consulting. All rights reserved. // Copyright (c) Aditus Consulting. All rights reserved.
//======================================================================== //========================================================================
   
//------------------------------------------------------------------------ //------------------------------------------------------------------------
// Class ImgTrans // Class ImgTrans
// Perform some simple image transformations. // Perform some simple image transformations.
//------------------------------------------------------------------------ //------------------------------------------------------------------------
class ImgTrans { class ImgTrans {
private $gdImg=null; private $gdImg=null;
   
function __construct($aGdImg) { function __construct($aGdImg) {
// Constructor // Constructor
$this->gdImg = $aGdImg; $this->gdImg = $aGdImg;
} }
   
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// _TransVert3D() and _TransHor3D() are helper methods to // _TransVert3D() and _TransHor3D() are helper methods to
// Skew3D(). // Skew3D().
// -------------------------------------------------------------------- // --------------------------------------------------------------------
function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) { function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
   
   
// Parameter check // Parameter check
if( $aHorizonPos < 0 || $aHorizonPos > 1.0 ) { if( $aHorizonPos < 0 || $aHorizonPos > 1.0 ) {
JpGraphError::RaiseL(9001); JpGraphError::RaiseL(9001);
//("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1."); //("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
} }
   
$w = imagesx($aGdImg); $w = imagesx($aGdImg);
$h = imagesy($aGdImg); $h = imagesy($aGdImg);
   
// Create new image // Create new image
$ww = $w; $ww = $w;
if( $aMinSize ) if( $aMinSize )
$hh = ceil($h * $aHorizon / ($aSkewDist+$h)); $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
else else
$hh = $h; $hh = $h;
   
$newgdh = imagecreatetruecolor($ww,$hh); $newgdh = imagecreatetruecolor($ww,$hh);
$crgb = new RGB( $newgdh ); $crgb = new RGB( $newgdh );
$fillColor = $crgb->Allocate($aFillColor); $fillColor = $crgb->Allocate($aFillColor);
imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor); imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
   
if( $aBorder ) { if( $aBorder ) {
$colidx = $crgb->Allocate($aBorder); $colidx = $crgb->Allocate($aBorder);
imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx); imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
} }
   
$mid = round($w * $aHorizonPos); $mid = round($w * $aHorizonPos);
   
$last=$h; $last=$h;
for($y=0; $y < $h; ++$y) { for($y=0; $y < $h; ++$y) {
   
$yp = $h-$y-1; $yp = $h-$y-1;
$yt = floor($yp * $aHorizon / ($aSkewDist + $yp)); $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
   
if( !$aQuality ) { if( !$aQuality ) {
if( $last <= $yt ) continue ; if( $last <= $yt ) continue ;
$last = $yt; $last = $yt;
} }
   
for($x=0; $x < $w; ++$x) { for($x=0; $x < $w; ++$x) {
$xt = ($x-$mid) * $aSkewDist / ($aSkewDist + $yp); $xt = ($x-$mid) * $aSkewDist / ($aSkewDist + $yp);
if( $aDir == SKEW3D_UP ) if( $aDir == SKEW3D_UP )
$rgb = imagecolorat($aGdImg,$x,$h-$y-1); $rgb = imagecolorat($aGdImg,$x,$h-$y-1);
else else
$rgb = imagecolorat($aGdImg,$x,$y); $rgb = imagecolorat($aGdImg,$x,$y);
$r = ($rgb >> 16) & 0xFF; $r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF; $g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF; $b = $rgb & 0xFF;
$colidx = imagecolorallocate($newgdh,$r,$g,$b); $colidx = imagecolorallocate($newgdh,$r,$g,$b);
$xt = round($xt+$mid); $xt = round($xt+$mid);
if( $aDir == SKEW3D_UP ) { if( $aDir == SKEW3D_UP ) {
$syt = $yt; $syt = $yt;
} }
else { else {
$syt = $hh-$yt-1; $syt = $hh-$yt-1;
} }
   
if( !empty($set[$yt]) ) { if( !empty($set[$yt]) ) {
$nrgb = imagecolorat($newgdh,$xt,$syt); $nrgb = imagecolorat($newgdh,$xt,$syt);
$nr = ($nrgb >> 16) & 0xFF; $nr = ($nrgb >> 16) & 0xFF;
$ng = ($nrgb >> 8) & 0xFF; $ng = ($nrgb >> 8) & 0xFF;
$nb = $nrgb & 0xFF; $nb = $nrgb & 0xFF;
$colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2), $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
floor(($g+$ng)/2),floor(($b+$nb)/2)); floor(($g+$ng)/2),floor(($b+$nb)/2));
} }
   
imagesetpixel($newgdh,$xt,$syt,$colidx); imagesetpixel($newgdh,$xt,$syt,$colidx);
} }
   
$set[$yt] = true; $set[$yt] = true;
} }
   
return $newgdh; return $newgdh;
} }
   
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// _TransVert3D() and _TransHor3D() are helper methods to // _TransVert3D() and _TransHor3D() are helper methods to
// Skew3D(). // Skew3D().
// -------------------------------------------------------------------- // --------------------------------------------------------------------
function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) { function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
   
$w = imagesx($aGdImg); $w = imagesx($aGdImg);
$h = imagesy($aGdImg); $h = imagesy($aGdImg);
   
// Create new image // Create new image
$hh = $h; $hh = $h;
if( $aMinSize ) if( $aMinSize )
$ww = ceil($w * $aHorizon / ($aSkewDist+$w)); $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
else else
$ww = $w; $ww = $w;
   
$newgdh = imagecreatetruecolor($ww,$hh); $newgdh = imagecreatetruecolor($ww,$hh);
$crgb = new RGB( $newgdh ); $crgb = new RGB( $newgdh );
$fillColor = $crgb->Allocate($aFillColor); $fillColor = $crgb->Allocate($aFillColor);
imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor); imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
   
if( $aBorder ) { if( $aBorder ) {
$colidx = $crgb->Allocate($aBorder); $colidx = $crgb->Allocate($aBorder);
imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx); imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
} }
   
$mid = round($h * $aHorizonPos); $mid = round($h * $aHorizonPos);
   
$last = -1; $last = -1;
for($x=0; $x < $w-1; ++$x) { for($x=0; $x < $w-1; ++$x) {
$xt = floor($x * $aHorizon / ($aSkewDist + $x)); $xt = floor($x * $aHorizon / ($aSkewDist + $x));
if( !$aQuality ) { if( !$aQuality ) {
if( $last >= $xt ) continue ; if( $last >= $xt ) continue ;
$last = $xt; $last = $xt;
} }
   
for($y=0; $y < $h; ++$y) { for($y=0; $y < $h; ++$y) {
$yp = $h-$y-1; $yp = $h-$y-1;
$yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x); $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
   
if( $aDir == SKEW3D_RIGHT ) if( $aDir == SKEW3D_RIGHT )
$rgb = imagecolorat($aGdImg,$w-$x-1,$y); $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
else else
$rgb = imagecolorat($aGdImg,$x,$y); $rgb = imagecolorat($aGdImg,$x,$y);
$r = ($rgb >> 16) & 0xFF; $r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF; $g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF; $b = $rgb & 0xFF;
$colidx = imagecolorallocate($newgdh,$r,$g,$b); $colidx = imagecolorallocate($newgdh,$r,$g,$b);
$yt = floor($hh-$yt-$mid-1); $yt = floor($hh-$yt-$mid-1);
if( $aDir == SKEW3D_RIGHT ) { if( $aDir == SKEW3D_RIGHT ) {
$sxt = $ww-$xt-1; $sxt = $ww-$xt-1;
} }
else else
$sxt = $xt ; $sxt = $xt ;
   
if( !empty($set[$xt]) ) { if( !empty($set[$xt]) ) {
$nrgb = imagecolorat($newgdh,$sxt,$yt); $nrgb = imagecolorat($newgdh,$sxt,$yt);
$nr = ($nrgb >> 16) & 0xFF; $nr = ($nrgb >> 16) & 0xFF;
$ng = ($nrgb >> 8) & 0xFF; $ng = ($nrgb >> 8) & 0xFF;
$nb = $nrgb & 0xFF; $nb = $nrgb & 0xFF;
$colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2), $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
floor(($g+$ng)/2),floor(($b+$nb)/2)); floor(($g+$ng)/2),floor(($b+$nb)/2));
} }
imagesetpixel($newgdh,$sxt,$yt,$colidx); imagesetpixel($newgdh,$sxt,$yt,$colidx);
} }
   
$set[$xt] = true; $set[$xt] = true;
} }
   
return $newgdh; return $newgdh;
} }
   
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// Skew image for the apperance of a 3D effect // Skew image for the apperance of a 3D effect
// This transforms an image into a 3D-skewed version // This transforms an image into a 3D-skewed version
// of the image. The transformation is specified by giving the height // of the image. The transformation is specified by giving the height
// of the artificial horizon and specifying a "skew" factor which // of the artificial horizon and specifying a "skew" factor which
// is the distance on the horizon line between the point of // is the distance on the horizon line between the point of
// convergence and perspective line. // convergence and perspective line.
// //
// The function returns the GD handle of the transformed image // The function returns the GD handle of the transformed image
// leaving the original image untouched. // leaving the original image untouched.
// //
// Parameters: // Parameters:
// * $aGdImg, GD handle to the image to be transformed // * $aGdImg, GD handle to the image to be transformed
// * $aHorizon, Distance to the horizon // * $aHorizon, Distance to the horizon
// * $aSkewDist, Distance from the horizon point of convergence // * $aSkewDist, Distance from the horizon point of convergence
// on the horizon line to the perspective points. A larger // on the horizon line to the perspective points. A larger
// value will fore-shorten the image more // value will fore-shorten the image more
// * $aDir, parameter specifies type of convergence. This of this // * $aDir, parameter specifies type of convergence. This of this
// as the walls in a room you are looking at. This specifies if the // as the walls in a room you are looking at. This specifies if the
// image should be applied on the left,right,top or bottom walls. // image should be applied on the left,right,top or bottom walls.
// * $aMinSize, true=make the new image just as big as needed, // * $aMinSize, true=make the new image just as big as needed,
// false = keep the image the same size as the original image // false = keep the image the same size as the original image
// * $aFillColor, Background fill color in the image // * $aFillColor, Background fill color in the image
// * $aHiQuality, true=performa some interpolation that improves // * $aHiQuality, true=performa some interpolation that improves
// the image quality but at the expense of performace. Enabling // the image quality but at the expense of performace. Enabling
// high quality will have a dramatic effect on the time it takes // high quality will have a dramatic effect on the time it takes
// to transform an image. // to transform an image.
// * $aBorder, if set to anything besides false this will draw a // * $aBorder, if set to anything besides false this will draw a
// a border of the speciied color around the image // a border of the speciied color around the image
// -------------------------------------------------------------------- // --------------------------------------------------------------------
function Skew3D($aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) { function Skew3D($aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality, return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
$aMinSize,$aFillColor,$aBorder); $aMinSize,$aFillColor,$aBorder);
} }
   
function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) { function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
if( $aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP ) if( $aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP )
return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder); return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
else else
return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder); return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
   
} }
   
} }
   
   
?> ?>