Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / web / public_php / admin / jpgraph / jpgraph_imgtrans.php
blobc2f260b26924708550ec0fd2c53550c50e4c726b
1 <?php
2 //=======================================================================
3 // File: JPGRAPH_IMGTRANS.PHP
4 // Description: Extension for JpGraph to do some simple img transformations
5 // Created: 2003-09-06
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_imgtrans.php,v 1.1 2006/07/07 13:37:14 powles Exp $
8 //
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
12 //------------------------------------------------------------------------
13 // Class ImgTrans
14 // Perform some simple image transformations.
15 //------------------------------------------------------------------------
16 class ImgTrans {
17 var $gdImg=null;
19 function ImgTrans($aGdImg) {
20 // Constructor
21 $this->gdImg = $aGdImg;
24 // --------------------------------------------------------------------
25 // _TransVert3D() and _TransHor3D() are helper methods to
26 // Skew3D().
27 // --------------------------------------------------------------------
28 function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
31 // Parameter check
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);
40 // Create new image
41 $ww = $w;
42 if( $aMinSize )
43 $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
44 else
45 $hh = $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);
52 if( $aBorder ) {
53 $colidx = $crgb->Allocate($aBorder);
54 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
57 $mid = round($w * $aHorizonPos);
59 $last=$h;
60 for($y=0; $y < $h; ++$y) {
62 $yp = $h-$y-1;
63 $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
65 if( !$aQuality ) {
66 if( $last <= $yt ) continue ;
67 $last = $yt;
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);
74 else
75 $rgb = imagecolorat($aGdImg,$x,$y);
76 $r = ($rgb >> 16) & 0xFF;
77 $g = ($rgb >> 8) & 0xFF;
78 $b = $rgb & 0xFF;
79 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
80 $xt = round($xt+$mid);
81 if( $aDir == SKEW3D_UP ) {
82 $syt = $yt;
84 else {
85 $syt = $hh-$yt-1;
88 if( !empty($set[$yt]) ) {
89 $nrgb = imagecolorat($newgdh,$xt,$syt);
90 $nr = ($nrgb >> 16) & 0xFF;
91 $ng = ($nrgb >> 8) & 0xFF;
92 $nb = $nrgb & 0xFF;
93 $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
94 floor(($g+$ng)/2),floor(($b+$nb)/2));
97 imagesetpixel($newgdh,$xt,$syt,$colidx);
100 $set[$yt] = true;
103 return $newgdh;
106 // --------------------------------------------------------------------
107 // _TransVert3D() and _TransHor3D() are helper methods to
108 // Skew3D().
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);
115 // Create new image
116 $hh = $h;
117 if( $aMinSize )
118 $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
119 else
120 $ww = $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);
127 if( $aBorder ) {
128 $colidx = $crgb->Allocate($aBorder);
129 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
132 $mid = round($h * $aHorizonPos);
134 $last = -1;
135 for($x=0; $x < $w-1; ++$x) {
136 $xt = floor($x * $aHorizon / ($aSkewDist + $x));
137 if( !$aQuality ) {
138 if( $last >= $xt ) continue ;
139 $last = $xt;
142 for($y=0; $y < $h; ++$y) {
143 $yp = $h-$y-1;
144 $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
146 if( $aDir == SKEW3D_RIGHT )
147 $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
148 else
149 $rgb = imagecolorat($aGdImg,$x,$y);
150 $r = ($rgb >> 16) & 0xFF;
151 $g = ($rgb >> 8) & 0xFF;
152 $b = $rgb & 0xFF;
153 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
154 $yt = floor($hh-$yt-$mid-1);
155 if( $aDir == SKEW3D_RIGHT ) {
156 $sxt = $ww-$xt-1;
158 else
159 $sxt = $xt ;
161 if( !empty($set[$xt]) ) {
162 $nrgb = imagecolorat($newgdh,$sxt,$yt);
163 $nr = ($nrgb >> 16) & 0xFF;
164 $ng = ($nrgb >> 8) & 0xFF;
165 $nb = $nrgb & 0xFF;
166 $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
167 floor(($g+$ng)/2),floor(($b+$nb)/2));
169 imagesetpixel($newgdh,$sxt,$yt,$colidx);
172 $set[$xt] = true;
175 return $newgdh;
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.
189 // Parameters:
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);
216 else
217 return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);