2 //=======================================================================
3 // File: JPGRAPH_IMGTRANS.PHP
4 // Description: Extension for JpGraph to do some simple img transformations
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_imgtrans.php,v 1.1 2006/07/07 13:37:14 powles Exp $
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
12 //------------------------------------------------------------------------
14 // Perform some simple image transformations.
15 //------------------------------------------------------------------------
19 function ImgTrans($aGdImg) {
21 $this->gdImg
= $aGdImg;
24 // --------------------------------------------------------------------
25 // _TransVert3D() and _TransHor3D() are helper methods to
27 // --------------------------------------------------------------------
28 function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN
,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
32 if( $aHorizonPos < 0 ||
$aHorizonPos > 1.0 ) {
33 JpGraphError
::RaiseL(9001);
34 //("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
37 $w = imagesx($aGdImg);
38 $h = imagesy($aGdImg);
43 $hh = ceil($h * $aHorizon / ($aSkewDist+
$h));
47 $newgdh = imagecreatetruecolor($ww,$hh);
48 $crgb = new RGB( $newgdh );
49 $fillColor = $crgb->Allocate($aFillColor);
50 imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
53 $colidx = $crgb->Allocate($aBorder);
54 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
57 $mid = round($w * $aHorizonPos);
60 for($y=0; $y < $h; ++
$y) {
63 $yt = floor($yp * $aHorizon / ($aSkewDist +
$yp));
66 if( $last <= $yt ) continue ;
70 for($x=0; $x < $w; ++
$x) {
71 $xt = ($x-$mid) * $aSkewDist / ($aSkewDist +
$yp);
72 if( $aDir == SKEW3D_UP
)
73 $rgb = imagecolorat($aGdImg,$x,$h-$y-1);
75 $rgb = imagecolorat($aGdImg,$x,$y);
76 $r = ($rgb >> 16) & 0xFF;
77 $g = ($rgb >> 8) & 0xFF;
79 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
80 $xt = round($xt+
$mid);
81 if( $aDir == SKEW3D_UP
) {
88 if( !empty($set[$yt]) ) {
89 $nrgb = imagecolorat($newgdh,$xt,$syt);
90 $nr = ($nrgb >> 16) & 0xFF;
91 $ng = ($nrgb >> 8) & 0xFF;
93 $colidx = imagecolorallocate($newgdh,floor(($r+
$nr)/2),
94 floor(($g+
$ng)/2),floor(($b+
$nb)/2));
97 imagesetpixel($newgdh,$xt,$syt,$colidx);
106 // --------------------------------------------------------------------
107 // _TransVert3D() and _TransHor3D() are helper methods to
109 // --------------------------------------------------------------------
110 function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT
,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
112 $w = imagesx($aGdImg);
113 $h = imagesy($aGdImg);
118 $ww = ceil($w * $aHorizon / ($aSkewDist+
$w));
122 $newgdh = imagecreatetruecolor($ww,$hh);
123 $crgb = new RGB( $newgdh );
124 $fillColor = $crgb->Allocate($aFillColor);
125 imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
128 $colidx = $crgb->Allocate($aBorder);
129 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
132 $mid = round($h * $aHorizonPos);
135 for($x=0; $x < $w-1; ++
$x) {
136 $xt = floor($x * $aHorizon / ($aSkewDist +
$x));
138 if( $last >= $xt ) continue ;
142 for($y=0; $y < $h; ++
$y) {
144 $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist +
$x);
146 if( $aDir == SKEW3D_RIGHT
)
147 $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
149 $rgb = imagecolorat($aGdImg,$x,$y);
150 $r = ($rgb >> 16) & 0xFF;
151 $g = ($rgb >> 8) & 0xFF;
153 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
154 $yt = floor($hh-$yt-$mid-1);
155 if( $aDir == SKEW3D_RIGHT
) {
161 if( !empty($set[$xt]) ) {
162 $nrgb = imagecolorat($newgdh,$sxt,$yt);
163 $nr = ($nrgb >> 16) & 0xFF;
164 $ng = ($nrgb >> 8) & 0xFF;
166 $colidx = imagecolorallocate($newgdh,floor(($r+
$nr)/2),
167 floor(($g+
$ng)/2),floor(($b+
$nb)/2));
169 imagesetpixel($newgdh,$sxt,$yt,$colidx);
178 // --------------------------------------------------------------------
179 // Skew image for the apperance of a 3D effect
180 // This transforms an image into a 3D-skewed version
181 // of the image. The transformation is specified by giving the height
182 // of the artificial horizon and specifying a "skew" factor which
183 // is the distance on the horizon line between the point of
184 // convergence and perspective line.
186 // The function returns the GD handle of the transformed image
187 // leaving the original image untouched.
190 // * $aGdImg, GD handle to the image to be transformed
191 // * $aHorizon, Distance to the horizon
192 // * $aSkewDist, Distance from the horizon point of convergence
193 // on the horizon line to the perspective points. A larger
194 // value will fore-shorten the image more
195 // * $aDir, parameter specifies type of convergence. This of this
196 // as the walls in a room you are looking at. This specifies if the
197 // image should be applied on the left,right,top or bottom walls.
198 // * $aMinSize, true=make the new image just as big as needed,
199 // false = keep the image the same size as the original image
200 // * $aFillColor, Background fill color in the image
201 // * $aHiQuality, true=performa some interpolation that improves
202 // the image quality but at the expense of performace. Enabling
203 // high quality will have a dramatic effect on the time it takes
204 // to transform an image.
205 // * $aBorder, if set to anything besides false this will draw a
206 // a border of the speciied color around the image
207 // --------------------------------------------------------------------
208 function Skew3D($aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN
,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
209 return $this->_Skew3D($this->gdImg
,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
210 $aMinSize,$aFillColor,$aBorder);
213 function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN
,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
214 if( $aDir == SKEW3D_DOWN ||
$aDir == SKEW3D_UP
)
215 return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
217 return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);