source: trunk/xgraph/jpgraph/jpgraph_imgtrans.php @ 42

Last change on this file since 42 was 42, checked in by marrucho, 10 years ago
File size: 8.2 KB
Line 
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// Ver:         $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $
7//
8// Copyright (c) Asial Corporation. All rights reserved.
9//========================================================================
10
11//------------------------------------------------------------------------
12// Class ImgTrans
13// Perform some simple image transformations.
14//------------------------------------------------------------------------
15class ImgTrans {
16    private $gdImg=null;
17
18    function __construct($aGdImg) {
19        // Constructor
20        $this->gdImg = $aGdImg;
21    }
22
23    // --------------------------------------------------------------------
24    // _TransVert3D() and _TransHor3D() are helper methods to
25    // Skew3D().
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
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    }
35
36    $w = imagesx($aGdImg);
37    $h = imagesy($aGdImg);
38
39    // Create new image
40    $ww = $w;
41    if( $aMinSize )
42    $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
43    else
44    $hh = $h;
45
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);
50
51    if( $aBorder ) {
52        $colidx = $crgb->Allocate($aBorder);
53        imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
54    }
55
56    $mid = round($w * $aHorizonPos);
57
58    $last=$h;
59    for($y=0; $y < $h; ++$y) {
60
61        $yp = $h-$y-1;
62        $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
63
64        if( !$aQuality ) {
65            if( $last <= $yt ) continue ;
66            $last = $yt;
67        }
68
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            }
86
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            }
95
96            imagesetpixel($newgdh,$xt,$syt,$colidx);
97        }
98
99        $set[$yt] = true;
100    }
101
102    return $newgdh;
103    }
104
105    // --------------------------------------------------------------------
106    // _TransVert3D() and _TransHor3D() are helper methods to
107    // Skew3D().
108    // --------------------------------------------------------------------
109    function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
110
111        $w = imagesx($aGdImg);
112        $h = imagesy($aGdImg);
113
114        // Create new image
115        $hh = $h;
116        if( $aMinSize )
117        $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
118        else
119        $ww = $w;
120
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);
125
126        if( $aBorder ) {
127            $colidx = $crgb->Allocate($aBorder);
128            imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
129        }
130
131        $mid = round($h * $aHorizonPos);
132
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            }
140
141            for($y=0; $y < $h; ++$y) {
142                $yp = $h-$y-1;
143                $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
144
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 ;
159
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            }
170
171            $set[$xt] = true;
172        }
173
174        return $newgdh;
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
182    // is the distance on the horizon line between the point of
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
190    // * $aHorizon, Distance to the horizon
191    // * $aSkewDist, Distance from the horizon point of convergence
192    //   on the horizon line to the perspective points. A larger
193    //   value will fore-shorten the image more
194    // * $aDir, parameter specifies type of convergence. This of this
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.
204    // * $aBorder, if set to anything besides false this will draw a
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) {
208        return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
209        $aMinSize,$aFillColor,$aBorder);
210    }
211
212    function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
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);
217
218    }
219
220}
221
222
223?>
Note: See TracBrowser for help on using the repository browser.