【问题标题】:Image corrupt or truncated in firefox generated by php scriptphp脚本生成的firefox中的图像损坏或截断
【发布时间】:2016-05-12 08:39:07
【问题描述】:

我对从 png 图像生成验证码的 php 脚本有疑问。 ie 和 chrome 一切正常。在 Firefox 浏览器中测试脚本时出现问题。

我的代码:

 function executeGenerateCaptcha()
{
  header('Content-Type: image/jpg');
  // number of characters to type
  define('NB_CARACTERE', 6);
  $sImage = imagecreatefrompng(dirname(__FILE__) . '/../captcha/images/fond.png');
  $nCpt   = 0;

// possible characters for captcha
  $aChars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ';

// string generation
  while ($nCpt < NB_CARACTERE)
  {
    $sCaractere         = $aChars{ mt_rand(0, strlen($aChars) - 1)};
    $aCaracteres[$nCpt] = $sCaractere;
    $nCpt++;
  }

  $sCode        = null;
  $iAbcisse     = 0;
  $iInclinaison = -20;

// characters generation on image
  foreach ($aCaracteres as $sCaractere)
  {
    $sCode .= $sCaractere;
    $color = imagecolorallocate($sImage, mt_rand(0, 253), mt_rand(0, 253), mt_rand(0, 253));
    imagettftext($sImage, 40, $iInclinaison, 0 + $iAbcisse, 45, $color, dirname(__FILE__) . '/../captcha/images/arial.ttf', $sCaractere);

    $iAbcisse     = $iAbcisse + 30;
    $iInclinaison = $iInclinaison - 12;
  }

  unset($sCaractere);
  unset($nCpt);
  unset($sCaractere);
  unset($aCaracteres);

  imagepng($sImage);

}

【问题讨论】:

    标签: image firefox symfony-1.4 captcha


    【解决方案1】:

    解决方案就是添加 'flush();'在函数结束时保存生成的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多