打印

[php] 关于PHP GD库生成的图片。

1,在FF运行正常,在IE就变成下载了,怎么解决?
2,用PHP GD库生成图能返回给JS吗?(用于AJAX)

TOP

达人快来

TOP

1:把代码贴出来看看先,一般header("Content-type: image/XXX")后(XXX为你图片格式)没问题
2:那是必须的,用AJAX获取你生成图片的地址就随便你操纵了(有时候,你JS得到地址后,图片还没生成完,就需要setTimeout一会后再调用了,这是我的方法)

TOP

复制内容到剪贴板
代码:
<?php
header("Content-type: image/jpg");
$img=imagecreate(100,40);
$bg=imagecolorallocate($img,111,119,145);
$linecolor=imagecolorallocate($img,0,0,0);
imageline($img,rand(49,50),rand(0,30),rand(50,100),rand(0,100),$linecolor);
imageline($img,rand(0,50),rand(0,20),rand(0,50),rand(0,20),$linecolor);
imageline($img,rand(0,50),rand(0,20),rand(0,50),rand(0,20),$linecolor);
imageline($img,rand(0,50),rand(0,20),rand(0,50),rand(0,20),$linecolor);
imageline($img,rand(0,50),rand(0,20),rand(0,50),rand(50,100),$linecolor);
$font = 'SIMYOU.TTF';
$text_color = imagecolorallocate($img, 0, 0, 0);
$rand_array=array(
            0=>array(65,90),
            1=>array(97,122),
            2=>array(48,57)
);
for($i=0;$i<=3;$i++) {
    $tt=rand(0,2);
    $rand_num[$i]=chr(rand($rand_array[$tt][0],$rand_array[$tt][1]));
  }
imagettftext($img, 18, rand(-10,10), rand(1,10), rand(20,30), $text_color, $font, $rand_num[0]);
imagettftext($img, 18, rand(-30,30), rand(15,30), rand(20,30), $text_color, $font, $rand_num[1]);
imagettftext($img, 18, rand(-30,30), rand(35,60), rand(20,30), $text_color, $font, $rand_num[2]);
imagettftext($img, 18, rand(-30,30), rand(65,70), rand(20,30), $text_color, $font, $rand_num[3]);
imagepng($img);
imagedestroy($img);
$str=implode("", $rand_num);
session_start();
$_SESSION['validate']=$str;
?>

TOP

验证码的验证除了用SESSION外还有其他好的验证方法吗?

TOP