Use gd image verification code to generate and save in php

WBOY
Release: 2016-07-29 09:01:05
Original
1140 people have browsed it

<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> < <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><<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> < <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><<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></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the use of gd image verification code generation and saving in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template