php中利用gd图片验证码生成和保存

WBOY
Lepaskan: 2016-07-29 09:01:05
asal
1122 orang telah melayarinya

<code><span><span><?php </span><span>//画画布</span><span>$img</span> = imagecreatetruecolor(<span>100</span>, <span>40</span>);
<span>//三种颜色</span><span>$black</span> = imagecolorallocate(<span>$img</span>, <span>0x00</span>, <span>0x00</span>, <span>0x00</span>);
<span>$green</span> = imagecolorallocate(<span>$img</span>, <span>0x00</span>, <span>0xFF</span>, <span>0x00</span>);
<span>$white</span> = imagecolorallocate(<span>$img</span>, <span>0xFF</span>, <span>0xFF</span>, <span>0xFF</span>);
<span>//填充白色</span>
imagefill(<span>$img</span>,<span>0</span>,<span>0</span>,<span>$white</span>);
<span>//生成随机的验证码</span><span>$code</span> = <span>''</span>;
<span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> 4</span>; <span>$i</span>++) {
    <span>$code</span> .= rand(<span>0</span>, <span>9</span>);
}
imagestring(<span>$img</span>, <span>5</span>, <span>10</span>, <span>10</span>, <span>$code</span>, <span>$black</span>);
<span>//加入噪点干扰</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>50</span>;<span>$i</span>++) {
    imagesetpixel(<span>$img</span>, rand(<span>0</span>, <span>100</span>) , rand(<span>0</span>, <span>100</span>) , <span>$black</span>);
    imagesetpixel(<span>$img</span>, rand(<span>0</span>, <span>100</span>) , rand(<span>0</span>, <span>100</span>) , <span>$green</span>);
}
<span>//png输出验证码jpeg输出到当前文件夹</span>
header(<span>"content-type: image/png"</span>);
imagepng(<span>$img</span>);
imagejpeg(<span>$img</span>, <span>"./ok.jpeg"</span>, <span>75</span>);
imagedestroy(<span>$img</span>);

```<span><?php </span><span>//画画布</span><span>$img</span> = imagecreatetruecolor(<span>100</span>, <span>40</span>);
<span>//三种颜色</span><span>$black</span> = imagecolorallocate(<span>$img</span>, <span>0x00</span>, <span>0x00</span>, <span>0x00</span>);
<span>$green</span> = imagecolorallocate(<span>$img</span>, <span>0x00</span>, <span>0xFF</span>, <span>0x00</span>);
<span>$white</span> = imagecolorallocate(<span>$img</span>, <span>0xFF</span>, <span>0xFF</span>, <span>0xFF</span>);
<span>//填充白色</span>
imagefill(<span>$img</span>,<span>0</span>,<span>0</span>,<span>$white</span>);
<span>//生成随机的验证码</span><span>$code</span> = <span>''</span>;
<span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> 4</span>; <span>$i</span>++) {
    <span>$code</span> .= rand(<span>0</span>, <span>9</span>);
}
imagestring(<span>$img</span>, <span>5</span>, <span>10</span>, <span>10</span>, <span>$code</span>, <span>$black</span>);
<span>//加入噪点干扰</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>50;<span>$i</span>++) {
    imagesetpixel(<span>$img</span>, rand(<span>0</span>, <span>100</span>) , rand(<span>0</span>, <span>100</span>) , <span>$black</span>);
    imagesetpixel(<span>$img</span>, rand(<span>0</span>, <span>100</span>) , rand(<span>0</span>, <span>100</span>) , <span>$green</span>);
}
<span>//png输出验证码jpeg输出到当前文件夹</span>
header(<span>"content-type: image/png"</span>);
imagepng(<span>$img</span>);
imagejpeg(<span>$img</span>, <span>"./ok.jpeg"</span>, <span>75</span>);
imagedestroy(<span>$img</span>);</code>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php中利用gd图片验证码生成和保存,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!