写了一个生成验证码,但是在本地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 and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)