1

I have a captcha Code which works perfectly but I really dont know why it displays funny. Can anyone help me figure out what might be the cause of this? Here is the code:

<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>

This is what is displays:

kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦­ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ

4

2 回答 2

7

你错过了标题。在 之后添加以下内容session_start()

header('Content-Type: image/jpeg');

输出将类似于:

在此处输入图像描述

于 2013-11-12T13:26:39.110 回答
2

当您创建这样的图像时,您需要告诉浏览器您想要显示什么类型的内容。因此原因:

header("Content-type: image/jpeg");

是需要的。如果这让您感到困惑,请阅读有关MIME的 Wiki 文章。您还会发现,每当您生成其他文件类型时,您都需要指定内容类型(例如:使用 PHP 动态生成的 PDF 文件将需要header('Content-type: application/pdf');

于 2013-11-12T13:27:54.877 回答