1

http://localhost/image.php?application=first当我从一切正常的代码中触发以下代码时,我不知道我在哪里出错了。

session_start();
$name = $_GET['application'];   

$text = rand(10000,99999); 
$_SESSION['application'][$name] = $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);

但是当我为此更改代码时

if (isset($_GET['application']) && !empty($_GET['application'])) {
    if (isset($_GET['image']) && $_GET['image'] == 'get'){
    session_start();
    $name = $_GET['application'];   

    $text = rand(10000,99999); 
    $_SESSION['application'][$name] = $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);
}
}

然后我在浏览器中得到 RAW 格式,所以我添加

header('Content-type: image/jpeg'); 
imagejpeg($image_p, null, 80);

到代码,现在我收到消息,我的图片被中断,但是当我将它保存在驱动器上并从 IrfanView 启动时,它会正常打开。

我想补充一点,我正在测试ob_start()功能,但它没有改变任何东西。

4

2 回答 2

2

当我点击此 URL 时对我很有用:

http://localhost/image.php?application=first&image=get

这是我在 image.php 中使用的确切代码:

if (isset($_GET['application']) && !empty($_GET['application'])) {
    if (isset($_GET['image']) && $_GET['image'] == 'get'){
        session_start();
        $name = $_GET['application'];   

        $text = rand(10000,99999); 
        $_SESSION['application'][$name] = $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); 

        header('Content-type: image/jpeg');
        imagejpeg($image_p, null, 80);
    }
}

?>
于 2012-06-14T17:17:17.143 回答
0

好的,我想通了。问题是我require 'common.php'在主 IF 语句之前添加。我的整个代码是这样的

<?php
require 'common.php';
    if (isset($_GET['application']) && !empty($_GET['application'])) {
    if (isset($_GET['image']) && $_GET['image'] == 'get'){
        session_start();
        $name = $_GET['application'];                   $text = rand(10000,99999);             $_SESSION['application'][$name] = $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);                 header('Content-type: image/jpeg');
        imagejpeg($image_p, null, 80);
    }
.
.
.

所以当我把要求搬到其他地方时,一切都很好!感谢小伙伴们的帮助!:)

于 2012-06-15T21:07:44.943 回答