duanjupiao4730 2017-07-18 14:43
浏览 37
已采纳

PHP文件作为图像源不起作用(HTML,PHP)

My php file creates a png image. The image is ready and is saved into the given folder, but cannot load it on the page (it just show the place of the image, but empty).

This is the "captcha_image.php":

<?php
    $dirPath="/opt/lampp/htdocs/WebSiteFolder/dfxCaptcha/";
    $font='/opt/lampp/htdocs/WebSiteFolder/DejaVuSerif-Bold.ttf';
    $imgWidth=200;
    $imgHeight=50;        
    global $image;
    $image = imagecreatetruecolor($imgWidth, $imgHeight) or die("Cannot initialize a new GD image stream.");
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);    
    imagefilledrectangle($image, 0, 0, $imgWidth, $imgHeight, $background_color);    
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $word = "";    
    for ($i = 0; $i < 4; $i++) {
        $letter = $letters[rand(0, $len - 1)];         
        imagettftext($image, 15, 0, $i*50+25, 50, $text_color, $font, $letter);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;
    $images = glob($dirPath."*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
    header ("Content-type: image/png");
    imagepng($image, $dirPath."image" . time() . ".png");
?>

I use this in the HTML:

<img src="includes/captcha_image.php" id="captcha">

How can I display the image to the page?

  • 写回答

1条回答 默认 最新

  • drzb7969753 2017-07-18 14:46
    关注

    Your captcha_image.php file doesn't output the file to the browser, because you gave two arguments to the imagepng function.

    The documentation says:

    to

    The path or an open stream resource (which is automatically being closed after this function returns) to save the file to. If not set or NULL, the raw image stream will be outputted directly.

    You probably want to specify no second argument – this will mean the image is not saved, however. You did not show us your logic for accepting CAPTCHAs, but it seems to involve session management, in which case you don't need to save the file.

    This should be your last line:

    imagepng($image);
    

    Additionally, please consider using an existing CAPTCHA solution. What you are doing with your code produces clear text, which is extremely readable to OCR bots nowadays. Depending on the purpose of your project you might even get away with no CAPTCHA (e.g. for comments on blog articles you can specify hidden form fields, then use a spam filter). If a CAPTCHA is truly crucial, you will need a better solution than what you have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?