当前位置:首页 > PHP教程 > php图像处理 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-21 15:33:39 浏览: 评论:0 

这篇文章主要介绍了PHP动态生成指定大小随机图片的方法,涉及PHP根据传入参数动态生成图片的相关技巧,需要的朋友可以参考下。

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

  1. <?php 
  2. $image_width = 100; 
  3. $image_height = 100; 
  4. $image_str = ''; 
  5. if (isset($_GET['w'])) 
  6. { 
  7.   $image_width = intval($_GET['w']); 
  8. } 
  9. if (isset($_GET['h'])) 
  10. { 
  11.   $image_height = intval($_GET['h']); 
  12. } 
  13. if (isset($_GET['s'])) 
  14. { 
  15.   $image_str = $_GET['s']; 
  16. } 
  17. $img = imagecreate($image_width, $image_height); 
  18. $color = imagecolorallocate($img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 
  19. imagefilledrectangle($img, 0, $image_height, $image_width, 0, $color); 
  20. $step = mt_rand(15, 30); 
  21. $start = mt_rand(0, $step); 
  22. $color = imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); 
  23. imagesetthickness($img, mt_rand(3, 10)); 
  24. if ($image_height > $image_width) 
  25. { 
  26.   for ($i=$start; $i<$image_height * 2; $i+=$step) 
  27.   { 
  28.     imageline($img, 0, $i, $i, 0, $color); 
  29.   } 
  30. } 
  31. else 
  32. { 
  33.   for ($i=$start; $i<$image_width * 2; $i+=$step) 
  34.   { 
  35.     imageline($img, $i, 0, 0, $i, $color); 
  36.   } 
  37. } 
  38. if ($image_str != '') 
  39. { 
  40.   $black = imagecolorallocate($img, 0, 0, 0); 
  41.   imagestring($img, 12, 5, 5, $image_str, $black); 
  42. } 
  43. header('Content-type:image/png'); 
  44. imagepng($img); 
  45. imagedestroy($img);

Tags: PHP生成随机图片

分享到: