【问题标题】:generating images with imagejpeg in PHP在 PHP 中使用 imagejpeg 生成图像
【发布时间】:2012-06-16 05:07:44
【问题描述】:

当我从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() 函数,但它并没有改变任何东西。

【问题讨论】:

  • 您是否将正确的 GET 参数传递给您的脚本?

标签: php image


【解决方案1】:

当我点击此 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);
    }
}

?>

【讨论】:

    【解决方案2】:

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

    <?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);
        }
    .
    .
    .
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2013-08-20
      • 2021-08-14
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多