歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> PHP繪制漸變顏色圖片

PHP繪制漸變顏色圖片

日期:2017/3/1 10:11:59   编辑:Linux編程

使用范例如下,基本函數封裝在gd-gradient-fill.php中,

  1. require_once('/path/to/gd-gradient-fill.php');
  2. $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);
gd_gradient_fill有5個必要參數和一個可選參數:
  1. integer $width
    Width of the image
  2. integer $height
    Height of the image
  3. string $direction
    The shape or direction of the gradient, which can be any of : vertical, horizontal, ellipse, ellipse2, circle, circle2, rectangle, diamond.
  4. string $startcolor
    Gradient start color, 3 or 6 digit hexadecimal ("#ff0" or "#dd4578")
  5. string $endcolor
    Gradient end color
  6. Optional : $step
    Breaks the gradient smooth blending

下面是一些實例,顏色從#101040漸變到#a1a1ff, 漸變方向不同, 鼠標移動到圖片上可看到提示信息.


另外, 這個函數處理非矩形圖片一樣可以.


附函數源碼如下:

  1. <?php
  2. /*
  3. Script Name: GD Gradient Fill
  4. Script URI: http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/
  5. Description: Creates a gradient fill of any shape (rectangle, ellipse, vertical, horizontal, diamond)
  6. Author: Ozh
  7. Version: 1.1
  8. Author URI: http://planetozh.com/
  9. */
  10. /* Release history :
  11. * 1.1
  12. * - changed : more nicely packaged as a class
  13. * - fixed : not displaying proper gradient colors with image dimension greater than 255 (because of a limitation in imagecolorallocate)
  14. * - added : optional parameter 'step', more options for 'direction'
  15. * 1.0
  16. * - initial release
  17. */
  18. /* Usage :
  19. *
  20. * require_once('/path/to/gd-gradient-fill.php');
  21. * $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);
  22. *
  23. * Parameters :
  24. * - width and height : integers, dimesions of your image.
  25. * - direction : string, shape of the gradient.
  26. * Can be : vertical, horizontal, rectangle (or square), ellipse, ellipse2, circle, circle2, diamond.
  27. * - startcolor : string, start color in 3 or 6 digits hexadecimal.
  28. * - endcolor : string, end color in 3 or 6 digits hexadecimal.
  29. * - step : integer, optional, default to 0. Step that breaks the smooth blending effect.
  30. * Returns a resource identifier.
  31. *
  32. * Examples :
  33. *
  34. * 1.
  35. * require_once('/home/ozh/www/includes/gd-gradient-fill.php');
  36. * $image = new gd_gradient_fill(200,200,'horizontal','#fff','#f00');
  37. *
  38. * 2.
  39. * require_once('c:/iis/inet/include/gd-gradient-fill.php');
  40. * $myimg = new gd_gradient_fill(80,20,'diamond','#ff0010','#303060');
  41. *
  42. */
  43. // Test it :
  44. // $image = new gd_gradient_fill(400,200,'ellipse','#f00','#000',0);
  45. class gd_gradient_fill {
  46. // Constructor. Creates, fills and returns an image
  47. function gd_gradient_fill($w,$h,$d,$s,$e,$step=0) {
  48. $this->width = $w;
  49. $this->height = $h;
  50. $this->direction = $d;
  51. $this->startcolor = $s;
  52. $this->endcolor = $e;
  53. $this->step = intval(abs($step));
  54. // Attempt to create a blank image in true colors, or a new palette based image if this fails
  55. if (function_exists('imagecreatetruecolor')) {
  56. $this->image = imagecreatetruecolor($this->width,$this->height);
  57. } elseif (function_exists('imagecreate')) {
  58. $this->image = imagecreate($this->width,$this->height);
  59. } else {
  60. die('Unable to create an image');
  61. }
  62. // Fill it
  63. $this->fill($this->image,$this->direction,$this->startcolor,$this->endcolor);
  64. // Show it
  65. $this->display($this->image);
  66. // Return it
  67. return $this->image;
  68. }
  69. // Displays the image with a portable function that works with any file type
  70. // depending on your server software configuration
  71. function display ($im) {
  72. if (function_exists("imagepng")) {
  73. header("Content-type: image/png");
  74. imagepng($im);
  75. }
  76. elseif (function_exists("imagegif")) {
  77. header("Content-type: image/gif");
  78. imagegif($im);
  79. }
  80. elseif (function_exists("imagejpeg")) {
  81. header("Content-type: image/jpeg");
  82. imagejpeg($im, "", 0.5);
  83. }
  84. elseif (function_exists("imagewbmp")) {
  85. header("Content-type: image/vnd.wap.wbmp");
  86. imagewbmp($im);
  87. } else {
  88. die("Doh ! No graphical functions on this server ?");
  89. }
  90. return true;
  91. }
  92. // The main function that draws the gradient
  93. function fill($im,$direction,$start,$end) {
  94. switch($direction) {
  95. case 'horizontal':
  96. $line_numbers = imagesx($im);
  97. $line_width = imagesy($im);
  98. list($r1,$g1,$b1) = $this->hex2rgb($start);
  99. list($r2,$g2,$b2) = $this->hex2rgb($end);
  100. break;
  101. case 'vertical':
  102. $line_numbers = imagesy($im);
  103. $line_width = imagesx($im);
  104. list($r1,$g1,$b1) = $this->hex2rgb($start);
  105. list($r2,$g2,$b2) = $this->hex2rgb($end);
  106. break;
  107. case 'ellipse':
  108. $width = imagesx($im);
  109. $height = imagesy($im);
  110. $rh=$height>$width?1:$width/$height;
  111. $rw=$width>$height?1:$height/$width;
  112. $line_numbers = min($width,$height);
  113. $center_x = $width/2;
  114. $center_y = $height/2;
  115. list($r1,$g1,$b1) = $this->hex2rgb($end);
  116. list($r2,$g2,$b2) = $this->hex2rgb($start);
  117. imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
  118. break;
  119. case 'ellipse2':
  120. $width = imagesx($im);
  121. $height = imagesy($im);
  122. $rh=$height>$width?1:$width/$height;
  123. $rw=$width>$height?1:$height/$width;
  124. $line_numbers = sqrt(pow($width,2)+pow($height,2));
  125. $center_x = $width/2;
  126. $center_y = $height/2;
  127. list($r1,$g1,$b1) = $this->hex2rgb($end);
  128. list($r2,$g2,$b2) = $this->hex2rgb($start);
  129. break;
  130. case 'circle':
  131. $width = imagesx($im);
  132. $height = imagesy($im);
  133. $line_numbers = sqrt(pow($width,2)+pow($height,2));
  134. $center_x = $width/2;
  135. $center_y = $height/2;
  136. $rh = $rw = 1;
  137. list($r1,$g1,$b1) = $this->hex2rgb($end);
  138. list($r2,$g2,$b2) = $this->hex2rgb($start);
  139. break;
  140. case 'circle2':
  141. $width = imagesx($im);
  142. $height = imagesy($im);
  143. $line_numbers = min($width,$height);
  144. $center_x = $width/2;
  145. $center_y = $height/2;
  146. $rh = $rw = 1;
  147. list($r1,$g1,$b1) = $this->hex2rgb($end);
  148. list($r2,$g2,$b2) = $this->hex2rgb($start);
  149. imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
  150. break;
  151. case 'square':
  152. case 'rectangle':
  153. $width = imagesx($im);
  154. $height = imagesy($im);
  155. $line_numbers = max($width,$height)/2;
  156. list($r1,$g1,$b1) = $this->hex2rgb($end);
  157. list($r2,$g2,$b2) = $this->hex2rgb($start);
  158. break;
  159. case 'diamond':
  160. list($r1,$g1,$b1) = $this->hex2rgb($end);
  161. list($r2,$g2,$b2) = $this->hex2rgb($start);
  162. $width = imagesx($im);
  163. $height = imagesy($im);
  164. $rh=$height>$width?1:$width/$height;
  165. $rw=$width>$height?1:$height/$width;
  166. $line_numbers = min($width,$height);
  167. break;
  168. default:
  169. }
  170. for ( $i = 0; $i < $line_numbers; $i=$i+1+$this->step ) {
  171. // old values :
  172. $old_r=$r;
  173. $old_g=$g;
  174. $old_b=$b;
  175. // new values :
  176. $r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
  177. $g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
  178. $b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
  179. // if new values are really new ones, allocate a new color, otherwise reuse previous color.
  180. // There's a "feature" in imagecolorallocate that makes this function
  181. // always returns '-1' after 255 colors have been allocated in an image that was created with
  182. // imagecreate (everything works fine with imagecreatetruecolor)
  183. if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")
  184. $fill = imagecolorallocate( $im, $r, $g, $b );
  185. switch($direction) {
  186. case 'vertical':
  187. imagefilledrectangle($im, 0, $i, $line_width, $i+$this->step, $fill);
  188. break;
  189. case 'horizontal':
  190. imagefilledrectangle( $im, $i, 0, $i+$this->step, $line_width, $fill );
  191. break;
  192. case 'ellipse':
  193. case 'ellipse2':
  194. case 'circle':
  195. case 'circle2':
  196. imagefilledellipse ($im,$center_x, $center_y, ($line_numbers-$i)*$rh, ($line_numbers-$i)*$rw,$fill);
  197. break;
  198. case 'square':
  199. case 'rectangle':
  200. imagefilledrectangle ($im,$i*$width/$height,$i*$height/$width,$width-($i*$width/$height), $height-($i*$height/$width),$fill);
  201. break;
  202. case 'diamond':
  203. imagefilledpolygon($im, array (
  204. $width/2, $i*$rw-0.5*$height,
  205. $i*$rh-0.5*$width, $height/2,
  206. $width/2,1.5*$height-$i*$rw,
  207. 1.5*$width-$i*$rh, $height/2 ), 4, $fill);
  208. break;
  209. default:
  210. }
  211. }
  212. }
  213. // #ff00ff -> array(255,0,255) or #f0f -> array(255,0,255)
  214. function hex2rgb($color) {
  215. $color = str_replace('#','',$color);
  216. $s = strlen($color) / 3;
  217. $rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));
  218. $rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));
  219. $rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));
  220. return $rgb;
  221. }
  222. }
  223. ?>
Copyright © Linux教程網 All Rights Reserved