php生成渐变图片并添加水印

现在还有个问题,就是渐变色只能是灰色,不清楚什么原因。。

PHP代码
  1. // 创建画布  
  2. $imageWidth = 720;  
  3. $imageHeight = 480;  
  4. $image = imagecreatetruecolor($imageWidth$imageHeight);  
  5.   
  6. // 生成随机渐变  
  7. $startColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));  
  8. $endColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));  
  9.   
  10. // 填充渐变背景  
  11. for ($y = 0; $y < $imageHeight$y++) {  
  12.     $progress = $y / ($imageHeight - 1);  
  13.   
  14.     $r = lerp($startColor$endColor$progress'red');  
  15.     $g = lerp($startColor$endColor$progress'green');  
  16.     $b = lerp($startColor$endColor$progress'blue');  
  17.   
  18.     $color = imagecolorallocate($image$r$g$b);  
  19.     imagefilledrectangle($image, 0, $y$imageWidth - 1, $y$color);  
  20. }  
  21.   
  22. // 添加水印文字  
  23. $watermarkText = $title;  
  24. $font = 'fonts/msyh.ttc';  // 替换为实际的字体文件路径  
  25. $fontSize = 20;  
  26. $fontColor = imagecolorallocate($image, 255, 255, 255);  
  27. $padding = 10;  
  28. $box = imagettfbbox($fontSize, 0, $font$watermarkText);  
  29. $textWidth = $box[2] - $box[0];  
  30. $textHeight = $box[1] - $box[7];  
  31. $textX = $imageWidth - $textWidth - $padding;  
  32. $textY = $imageHeight - $textHeight - $padding;  
  33. imagettftext($image$fontSize, 0, $textX$textY$fontColor$font$watermarkText);  
  34.   
  35. // 输出图片  
  36. header('Content-type: image/png');  
  37. imagepng($image);  
  38. imagedestroy($image);  
  39.   
  40. // 辅助函数,用于线性插值  
  41. function lerp($startColor$endColor$progress$channel) {  
  42.     $start = $startColor & 0xFF;  
  43.     $end = $endColor & 0xFF;  
  44.     return (int) round($start * (1 - $progress) + $end * $progress);  
  45. }  

 



上一篇: vb.net关于线程的问题
下一篇: vb.net 分割字符用split快还是用mid快?
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 335
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑