PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Scaffold/modules/Gradient/libraries/gradientgd.php

https://github.com/grandison/budo16
PHP | 145 lines | 97 code | 26 blank | 22 comment | 16 complexity | 427752ffb3ed2d551f1a734182f11a56 MD5 | raw file
  1. <?php
  2. class GradientGD
  3. {
  4. // $image = new GradientGD(width,height,)
  5. // Constructor. Creates, fills and returns an image
  6. function __construct($w,$h,$d,$s,$e,$stops=array())
  7. {
  8. $this->width = $w;
  9. $this->height = $h;
  10. $this->direction = $d;
  11. $this->startcolor = $s;
  12. $this->endcolor = $e;
  13. // Attempt to create a blank image in true colors, or a new palette based image if this fails
  14. if(function_exists('imagecreatetruecolor'))
  15. {
  16. $this->image = imagecreatetruecolor($this->width,$this->height);
  17. }
  18. elseif(function_exists('imagecreate'))
  19. {
  20. $this->image = imagecreate($this->width,$this->height);
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. array_unshift($stops, array('position'=>0,'color'=>$s));
  27. array_push($stops, array('position'=>1,'color'=>$e));
  28. for ($i = 0; $i < count($stops) - 1; $i++)
  29. {
  30. $this->fill(
  31. $this->image,
  32. $this->direction,
  33. (string)$stops[$i]['position'],
  34. (string)$stops[$i + 1]['position'],
  35. $stops[$i]['color'],
  36. $stops[$i + 1]['color']
  37. );
  38. }
  39. return $this->image;
  40. }
  41. /**
  42. * Saves the image to a file
  43. *
  44. * @param $file
  45. * @return void
  46. */
  47. function save($file)
  48. {
  49. imagepng($this->image, $file);
  50. }
  51. // The main function that draws the gradient
  52. function fill($im,$direction,$start,$end,$from,$to)
  53. {
  54. if($direction == 'horizontal')
  55. {
  56. if($start != 0)
  57. floor($start = $start * $this->width);
  58. $end = floor($end * $this->width);
  59. //$line_numbers = imagesx($im);
  60. $line_width = imagesy($im);
  61. list($r1,$g1,$b1) = $this->hex2rgb($from);
  62. list($r2,$g2,$b2) = $this->hex2rgb($to);
  63. }
  64. elseif($direction == 'vertical')
  65. {
  66. if($start != 0)
  67. $start = floor($start * $this->height);
  68. $end = floor($end * $this->height);
  69. //$line_numbers = imagesy($im);
  70. $line_width = imagesx($im);
  71. list($r1,$g1,$b1) = $this->hex2rgb($from);
  72. list($r2,$g2,$b2) = $this->hex2rgb($to);
  73. }
  74. //echo $start . '-' . $end . '--' . $from . '-' . $to . "\n";
  75. $r = $g = $b = '';
  76. for ( $i = 0; $i < ($end - $start); $i++ )
  77. {
  78. // old values :
  79. $old_r = $r;
  80. $old_g = $g;
  81. $old_b = $b;
  82. $line = $start + $i;
  83. // new values :
  84. //$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
  85. $r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / ($end - $start) )): $r1;
  86. $g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / ($end - $start) )): $g1;
  87. $b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / ($end - $start) )): $b1;
  88. // if new values are really new ones, allocate a new color, otherwise reuse previous color.
  89. // There's a "feature" in imagecolorallocate that makes this function
  90. // always returns '-1' after 255 colors have been allocated in an image that was created with
  91. // imagecreate (everything works fine with imagecreatetruecolor)
  92. if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")
  93. {
  94. $fill = imagecolorallocate( $im, $r, $g, $b );
  95. }
  96. switch($direction)
  97. {
  98. case 'vertical':
  99. // ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
  100. imagefilledrectangle($im, 0, $line, $line_width, $line, $fill);
  101. break;
  102. case 'horizontal':
  103. imagefilledrectangle( $im, $line, 0, $line, $line_width, $fill);
  104. break;
  105. default:
  106. }
  107. }
  108. }
  109. // #ff00ff -> array(255,0,255) or #f0f -> array(255,0,255)
  110. function hex2rgb($color)
  111. {
  112. $color = str_replace('#','',$color);
  113. $s = strlen($color) / 3;
  114. $rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));
  115. $rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));
  116. $rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));
  117. return $rgb;
  118. }
  119. }