dongqinta4174 2012-09-11 08:25
浏览 40

php ImageTTFText,如果之前是echo,则图像只填充Text

Hey i got some crazy problem by making a captcha.

I creating a shuffle variable and give it to the session, then i opening the image.php

captcha.php

<?php 
error_reporting(-1);
ini_set('display_errors', 1);

// Session start
SESSION_START();


// delet old CAPTCHA-Code from Session 
unset( $_SESSION["captcha"] );



// the Array
$text[] = "2";
$text[] = "4";
$text[] = "5";
$text[] = "7";
$text[] = "8";
$text[] = "A";
$text[] = "B";
$text[] = "C";
$text[] = "D";
$text[] = "E";
$text[] = "F";
$text[] = "G";
$text[] = "H";
$text[] = "J";
$text[] = "K";
$text[] = "M";
$text[] = "N";
$text[] = "P";
$text[] = "Q";
$text[] = "R";
$text[] = "S";
$text[] = "T";
$text[] = "U";
$text[] = "V";
$text[] = "W";
$text[] = "X";
$text[] = "Y";
$text[] = "Z";

//mixup array
shuffle ( $text );

//getting the first 5 Charakters
$outputtext = array_slice ($text, 0, 5);


//give it to the session
$_SESSION["captcha"] = implode($outputtext);
session_write_close();

echo "Captcha ist: ".$_SESSION["captcha"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

 <img src="captchaimage.php" alt="" style="vertical-align: middle;">

</html>

captchaimage.php

<?php

error_reporting(-1);
ini_set('display_errors', 1);


SESSION_START();

Header ("Content-type: image/png");

$str = $_SESSION["captcha"];



$bild = ImageCreateFromPNG ("http://www.php.de/images/captcha.logo1.png");


$farbe_w = ImageColorAllocate ($bild, 255, 255, 255);
$farbe_b = ImageColorAllocate ($bild, 0, 0, 0);


ImageTTFText ($bild, 32, -30, 224,  112, $farbe_w, "arial.ttf",
              $str[0]);
ImageTTFText ($bild, 32, -90, 288, 208, $farbe_w, "arial.ttf",
              $str[1]);
ImageTTFText ($bild, 32,   0, 208, 336, $farbe_w, "arial.ttf",
              $str[2]);
ImageTTFText ($bild, 32,  45,  128, 304, $farbe_w, "arial.ttf",
              $str[3]);
ImageTTFText ($bild, 32,  99,  128, 192, $farbe_w, "arial.ttf",
              $str[4]);


header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" ); 



ImagePng     ($bild);


ImageDestroy ($bild);

?>

this works fine, but if i delete the echo from captcha.php

this Line: echo "Captcha ist: ".$_SESSION["captcha"];

i get an image without characters, like the session is empty, but if the session is realy empty the image wount be createt.

Why dose the captchaimage.php only getting the correct session, if i wrote the echo line in the captcha.php?

  • 写回答

1条回答 默认 最新

  • duansengcha9114 2012-09-11 09:26
    关注

    A small test:

    Change the code a little bit - instead of calling captchaimage.php as the source of Your image, try to move Your captchaimage.php logic into the captcha.php this way:

    captcha.php

    <?php 
    error_reporting(-1);
    ini_set('display_errors', 1);
    
    // Session start
    SESSION_START();
    
    // delet old CAPTCHA-Code from Session 
    unset( $_SESSION["captcha"] );
    
    // the Array
    $text = array("2", "4", "5", "7", "8", "A", "B", "C", "D", "E", /*...*/);
    
    //mixup array
    shuffle ( $text );
    
    //getting the first 5 Charakters
    $outputtext = array_slice ($text, 0, 5);
    
    //give it to the session
    $_SESSION["captcha"] = implode($outputtext);
    
    $bild = ImageCreateFromPNG ("http://www.php.de/images/captcha.logo1.png");
    
    $farbe_w = ImageColorAllocate ($bild, 255, 255, 255);
    $farbe_b = ImageColorAllocate ($bild, 0, 0, 0);
    
    ImageTTFText ($bild, 32, -30, 224,  112, $farbe_w, "arial.ttf", $str[0]);
    ImageTTFText ($bild, 32, -90, 288, 208, $farbe_w, "arial.ttf", $str[1]);
    ImageTTFText ($bild, 32,   0, 208, 336, $farbe_w, "arial.ttf", $str[2]);
    ImageTTFText ($bild, 32,  45,  128, 304, $farbe_w, "arial.ttf", $str[3]);
    ImageTTFText ($bild, 32,  99,  128, 192, $farbe_w, "arial.ttf", $str[4]);
    
    ob_start();
    ImagePng     ($bild);
    $image = ob_get_clean();
    
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    
     <img src="data:image/png;base64,<?php echo base64_encode($image); ?>" alt="" style="vertical-align: middle;">
    
    </html>
    

    I hope this will work.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口