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