写了一个生成验证码,但是在本地wampsever环境中不能显示图片
但我上传到服务器里,却可以显示。
本地的gd库是打开的
新手弄得一头雾水,求教!
<code> <?php require_once '../include.php'; function verifyImage($type=3,$length=4,$pixel=1,$line=1,$sess_name = "verify"){ //使用GD库制作验证码 //定义画布宽高 $width=80; $height=30; //创建画布 $image = imagecreatetruecolor ( $width, $height ); //定义画布中的颜色 $white = imagecolorallocate ( $image, 255, 255, 255 ); $black = imagecolorallocate ( $image, 0, 0, 0 ); //用填充矩形填充画布 imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white ); //获取字符串 $chars=buildRandomString(3,4); //传值验证 $_SESSION [$sess_name] = $chars; $fontfiles=array("SIMYOU.TTF"); $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); //在画布中添加字符 for($i = 0; $i < $length; $i ++) { $size = mt_rand ( 14, 18 ); $angle = mt_rand ( - 15, 15 ); $x = 5 + $i * $size; $y = mt_rand ( 20, 26 ); $fontfile = '../fonts/' . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )]; $text = substr ( $chars, $i, 1 ); imagettftext ( $image, $size, $angle, $x, $y, $black, $fontfile, $text ); } //添加干扰元素 点 if ($pixel) { for ($i=0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $color); } } //添加干扰元素 线 if ($line) { for ($i=0; $i < $line; $i++) { imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1),mt_rand(0,$width-1), mt_rand(0,$height-1), $color); } } //浏览器标示输出是图像 header ( "content-type:image/jpeg" ); //创建图像资源 imagejpeg ( $image ); //注销图像资源 imagedestroy($image); } verifyImage(); ?> </code>
回复内容:
写了一个生成验证码,但是在本地wampsever环境中不能显示图片
但我上传到服务器里,却可以显示。
本地的gd库是打开的
新手弄得一头雾水,求教!
<code> <?php require_once '../include.php'; function verifyImage($type=3,$length=4,$pixel=1,$line=1,$sess_name = "verify"){ //使用GD库制作验证码 //定义画布宽高 $width=80; $height=30; //创建画布 $image = imagecreatetruecolor ( $width, $height ); //定义画布中的颜色 $white = imagecolorallocate ( $image, 255, 255, 255 ); $black = imagecolorallocate ( $image, 0, 0, 0 ); //用填充矩形填充画布 imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white ); //获取字符串 $chars=buildRandomString(3,4); //传值验证 $_SESSION [$sess_name] = $chars; $fontfiles=array("SIMYOU.TTF"); $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); //在画布中添加字符 for($i = 0; $i < $length; $i ++) { $size = mt_rand ( 14, 18 ); $angle = mt_rand ( - 15, 15 ); $x = 5 + $i * $size; $y = mt_rand ( 20, 26 ); $fontfile = '../fonts/' . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )]; $text = substr ( $chars, $i, 1 ); imagettftext ( $image, $size, $angle, $x, $y, $black, $fontfile, $text ); } //添加干扰元素 点 if ($pixel) { for ($i=0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $color); } } //添加干扰元素 线 if ($line) { for ($i=0; $i < $line; $i++) { imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1),mt_rand(0,$width-1), mt_rand(0,$height-1), $color); } } //浏览器标示输出是图像 header ( "content-type:image/jpeg" ); //创建图像资源 imagejpeg ( $image ); //注销图像资源 imagedestroy($image); } verifyImage(); ?> </code>
图片不显示一般是因为生成的图片流不正确,你直接在浏览器里面打开这个图片的地址。
可能就你会发现:
<code>php</code><code>Fatal error :xxxxxx or Warning : xxxxx </code>
的错误提示。
另外一般本地是开启 display_erorrs 的,但是服务器生产环境里面为了安全起见是关闭所有的错误输出的。
- check file encoding,
utf-8
usually. - try
ob_clean();
to clean the wrong output buffer.
这段代码,为何看着如此眼熟.......!
题主应该是从慕课网上的PHP电商网站开发教程中提取出来的。
这段代码有个BUG,就是在
$fontfiles=array("SIMYOU.TTF");
$fontfile = '../fonts/' . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
fonts文件夹中,你没有放置SIMYOU.TTF或者其他相关的字体文件,所以服务器找不到字体。
只要把TTF字体文件给补上即可。
输出验证码不能有其他内容输出的!!
针对你的情况,是因为你本地环境display_errors开启了,
你只需要在最上面,也就是require_once '../include.php'; 这一行的上面加上ini_set('display_errors', 'Off'); 就可以了。
另外你的

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版
中文版,非常好用

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版