首页 > 语言 > PHP > 正文

PHP动态生成指定大小随机图片的方法

2024-09-04 11:46:55
字体:大 中 小
来源:转载
供稿:网友

本文实例讲述了PHP动态生成指定大小随机图片的方法。分享给大家供大家参考,具体如下:

  1. $image_width = 100; 
  2.  
  3. $image_height = 100; 
  4.  
  5. $image_str = ''; 
  6.  
  7. if (isset($_GET['w'])) 
  8.  
  9. { 
  10.  
  11.   $image_width = intval($_GET['w']); 
  12.  
  13. } 
  14.  
  15. if (isset($_GET['h'])) 
  16.  
  17. { 
  18.  
  19.   $image_height = intval($_GET['h']); 
  20. //Vevb.com 
  21. } 
  22.  
  23. if (isset($_GET['s'])) 
  24.  
  25. { 
  26.  
  27.   $image_str = $_GET['s']; 
  28.  
  29. } 
  30.  
  31. $img = imagecreate($image_width, $image_height); 
  32.  
  33. $color = imagecolorallocate($img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 
  34.  
  35. imagefilledrectangle($img, 0, $image_height, $image_width, 0, $color); 
  36.  
  37. $step = mt_rand(15, 30); 
  38.  
  39. $start = mt_rand(0, $step); 
  40.  
  41. $color = imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); 
  42.  
  43. imagesetthickness($img, mt_rand(3, 10)); 
  44.  
  45. if ($image_height > $image_width) 
  46.  
  47. { 
  48.  
  49.   for ($i=$start; $i<$image_height * 2; $i+=$step) 
  50.  
  51.   { 
  52.  
  53.     imageline($img, 0, $i, $i, 0, $color); 
  54.  
  55.   } 
  56.  
  57. } 
  58.  
  59. else 
  60.  
  61. { 
  62.  
  63.   for ($i=$start; $i<$image_width * 2; $i+=$step) 
  64.  
  65.   { 
  66.  
  67.     imageline($img, $i, 0, 0, $i, $color); 
  68.  
  69.   } 
  70.  
  71. } 
  72.  
  73. if ($image_str != '') 
  74.  
  75. { 
  76.  
  77.   $black = imagecolorallocate($img, 0, 0, 0); 
  78.  
  79.   imagestring($img, 12, 5, 5, $image_str, $black); 
  80.  
  81. } 
  82.  
  83. header('Content-type:image/png'); 
  84.  
  85. imagepng($img); 
  86.  
  87. imagedestroy($img); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表