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

yipeiwu_com5年前PHP代码库

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

<?php
$image_width = 100;
$image_height = 100;
$image_str = '';
if (isset($_GET['w']))
{
  $image_width = intval($_GET['w']);
}
if (isset($_GET['h']))
{
  $image_height = intval($_GET['h']);
}
if (isset($_GET['s']))
{
  $image_str = $_GET['s'];
}
$img = imagecreate($image_width, $image_height);
$color = imagecolorallocate($img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($img, 0, $image_height, $image_width, 0, $color);
$step = mt_rand(15, 30);
$start = mt_rand(0, $step);
$color = imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
imagesetthickness($img, mt_rand(3, 10));
if ($image_height > $image_width)
{
  for ($i=$start; $i<$image_height * 2; $i+=$step)
  {
    imageline($img, 0, $i, $i, 0, $color);
  }
}
else
{
  for ($i=$start; $i<$image_width * 2; $i+=$step)
  {
    imageline($img, $i, 0, 0, $i, $color);
  }
}
if ($image_str != '')
{
  $black = imagecolorallocate($img, 0, 0, 0);
  imagestring($img, 12, 5, 5, $image_str, $black);
}
header('Content-type:image/png');
imagepng($img);
imagedestroy($img);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

详谈PHP中的密码安全性Password Hashing

如果你还在用md5加密,建议看看下方密码加密和验证方式。 先看一个简单的Password Hashing例子: <?php //require 'password....

轻松实现php文件上传功能

轻松实现php文件上传功能

PHP 文件上传 通过 PHP,可以把文件上传到服务器。 本章节实例在 test 项目下完成,目录结构为: test |-----upload   &...

PHP实现抓取迅雷VIP账号的方法

本文实例讲述了PHP实现抓取迅雷VIP账号的方法。分享给大家供大家参考。具体如下: 看了@Jinn_Wei Python版本的抓取账号,于是顺手写了个PHP版本 PS1:代码没经过优化,...

PHPExcel内存泄漏问题解决方法

使用 PHPExcel 来生成 excel 文档是比较消耗内存的,有时候可能会需要通过一个循环来把大数据切分成若干个小的 excel 文档保存来避免内存耗尽。 然而 PHPExcel 存...

php实现获取近几日、月时间示例

本文实例讲述了php实现获取近几日、月时间。分享给大家供大家参考,具体如下: <?php date_default_timezone_set('Asia/Shangha...