source: trunk/client/modules/Elezioni/grafici/jpgraph_imgtrans.php@ 429

Last change on this file since 429 was 284, checked in by roby, 6 years ago
File size: 8.0 KB
RevLine 
[2]1<?php
2//=======================================================================
[284]3// File: JPGRAPH_IMGTRANS.PHP
4// Description: Extension for JpGraph to do some simple img transformations
5// Created: 2003-09-06
6// Ver: $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $
[2]7//
[284]8// Copyright (c) Asial Corporation. All rights reserved.
[2]9//========================================================================
10
11//------------------------------------------------------------------------
12// Class ImgTrans
[284]13// Perform some simple image transformations.
[2]14//------------------------------------------------------------------------
15class ImgTrans {
16 private $gdImg=null;
17
[284]18 function __construct($aGdImg) {
19 // Constructor
20 $this->gdImg = $aGdImg;
[2]21 }
22
23 // --------------------------------------------------------------------
[284]24 // _TransVert3D() and _TransHor3D() are helper methods to
25 // Skew3D().
[2]26 // --------------------------------------------------------------------
27 function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
28
29
[284]30 // Parameter check
31 if( $aHorizonPos < 0 || $aHorizonPos > 1.0 ) {
32 JpGraphError::RaiseL(9001);
33 //("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
34 }
[2]35
[284]36 $w = imagesx($aGdImg);
37 $h = imagesy($aGdImg);
[2]38
[284]39 // Create new image
40 $ww = $w;
41 if( $aMinSize )
42 $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
43 else
44 $hh = $h;
[2]45
[284]46 $newgdh = imagecreatetruecolor($ww,$hh);
47 $crgb = new RGB( $newgdh );
48 $fillColor = $crgb->Allocate($aFillColor);
49 imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
[2]50
[284]51 if( $aBorder ) {
52 $colidx = $crgb->Allocate($aBorder);
53 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
54 }
[2]55
[284]56 $mid = round($w * $aHorizonPos);
[2]57
[284]58 $last=$h;
59 for($y=0; $y < $h; ++$y) {
[2]60
[284]61 $yp = $h-$y-1;
62 $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
[2]63
[284]64 if( !$aQuality ) {
65 if( $last <= $yt ) continue ;
66 $last = $yt;
67 }
[2]68
[284]69 for($x=0; $x < $w; ++$x) {
70 $xt = ($x-$mid) * $aSkewDist / ($aSkewDist + $yp);
71 if( $aDir == SKEW3D_UP )
72 $rgb = imagecolorat($aGdImg,$x,$h-$y-1);
73 else
74 $rgb = imagecolorat($aGdImg,$x,$y);
75 $r = ($rgb >> 16) & 0xFF;
76 $g = ($rgb >> 8) & 0xFF;
77 $b = $rgb & 0xFF;
78 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
79 $xt = round($xt+$mid);
80 if( $aDir == SKEW3D_UP ) {
81 $syt = $yt;
82 }
83 else {
84 $syt = $hh-$yt-1;
85 }
[2]86
[284]87 if( !empty($set[$yt]) ) {
88 $nrgb = imagecolorat($newgdh,$xt,$syt);
89 $nr = ($nrgb >> 16) & 0xFF;
90 $ng = ($nrgb >> 8) & 0xFF;
91 $nb = $nrgb & 0xFF;
92 $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
93 floor(($g+$ng)/2),floor(($b+$nb)/2));
94 }
[2]95
[284]96 imagesetpixel($newgdh,$xt,$syt,$colidx);
97 }
98
99 $set[$yt] = true;
[2]100 }
101
[284]102 return $newgdh;
103 }
104
[2]105 // --------------------------------------------------------------------
[284]106 // _TransVert3D() and _TransHor3D() are helper methods to
107 // Skew3D().
[2]108 // --------------------------------------------------------------------
109 function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
110
[284]111 $w = imagesx($aGdImg);
112 $h = imagesy($aGdImg);
[2]113
[284]114 // Create new image
115 $hh = $h;
116 if( $aMinSize )
117 $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
118 else
119 $ww = $w;
[2]120
[284]121 $newgdh = imagecreatetruecolor($ww,$hh);
122 $crgb = new RGB( $newgdh );
123 $fillColor = $crgb->Allocate($aFillColor);
124 imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
[2]125
[284]126 if( $aBorder ) {
127 $colidx = $crgb->Allocate($aBorder);
128 imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
129 }
[2]130
[284]131 $mid = round($h * $aHorizonPos);
[2]132
[284]133 $last = -1;
134 for($x=0; $x < $w-1; ++$x) {
135 $xt = floor($x * $aHorizon / ($aSkewDist + $x));
136 if( !$aQuality ) {
137 if( $last >= $xt ) continue ;
138 $last = $xt;
139 }
[2]140
[284]141 for($y=0; $y < $h; ++$y) {
142 $yp = $h-$y-1;
143 $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
[2]144
[284]145 if( $aDir == SKEW3D_RIGHT )
146 $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
147 else
148 $rgb = imagecolorat($aGdImg,$x,$y);
149 $r = ($rgb >> 16) & 0xFF;
150 $g = ($rgb >> 8) & 0xFF;
151 $b = $rgb & 0xFF;
152 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
153 $yt = floor($hh-$yt-$mid-1);
154 if( $aDir == SKEW3D_RIGHT ) {
155 $sxt = $ww-$xt-1;
156 }
157 else
158 $sxt = $xt ;
[2]159
[284]160 if( !empty($set[$xt]) ) {
161 $nrgb = imagecolorat($newgdh,$sxt,$yt);
162 $nr = ($nrgb >> 16) & 0xFF;
163 $ng = ($nrgb >> 8) & 0xFF;
164 $nb = $nrgb & 0xFF;
165 $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
166 floor(($g+$ng)/2),floor(($b+$nb)/2));
167 }
168 imagesetpixel($newgdh,$sxt,$yt,$colidx);
169 }
[2]170
[284]171 $set[$xt] = true;
172 }
173
174 return $newgdh;
[2]175 }
176
177 // --------------------------------------------------------------------
178 // Skew image for the apperance of a 3D effect
179 // This transforms an image into a 3D-skewed version
180 // of the image. The transformation is specified by giving the height
181 // of the artificial horizon and specifying a "skew" factor which
[284]182 // is the distance on the horizon line between the point of
[2]183 // convergence and perspective line.
184 //
185 // The function returns the GD handle of the transformed image
186 // leaving the original image untouched.
187 //
188 // Parameters:
189 // * $aGdImg, GD handle to the image to be transformed
[284]190 // * $aHorizon, Distance to the horizon
[2]191 // * $aSkewDist, Distance from the horizon point of convergence
[284]192 // on the horizon line to the perspective points. A larger
[2]193 // value will fore-shorten the image more
[284]194 // * $aDir, parameter specifies type of convergence. This of this
[2]195 // as the walls in a room you are looking at. This specifies if the
196 // image should be applied on the left,right,top or bottom walls.
197 // * $aMinSize, true=make the new image just as big as needed,
198 // false = keep the image the same size as the original image
199 // * $aFillColor, Background fill color in the image
200 // * $aHiQuality, true=performa some interpolation that improves
201 // the image quality but at the expense of performace. Enabling
202 // high quality will have a dramatic effect on the time it takes
203 // to transform an image.
[284]204 // * $aBorder, if set to anything besides false this will draw a
[2]205 // a border of the speciied color around the image
206 // --------------------------------------------------------------------
207 function Skew3D($aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
[284]208 return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
209 $aMinSize,$aFillColor,$aBorder);
[2]210 }
211
212 function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
[284]213 if( $aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP )
214 return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
215 else
216 return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
[2]217
218 }
[284]219
[2]220}
221
222
[284]223?>
Note: See TracBrowser for help on using the repository browser.