PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/jpgraph-3.0.7/src/jpgraph_imgtrans.php

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