Rumah > Artikel > pembangunan bahagian belakang > 验证码类,php验证码类_PHP教程
<span><br /> 1</span> <span>//</span><span>code class</span> <span> 2</span> <span>class</span><span> ValidateCode { </span><span> 3</span> <span>private</span> <span>$charset</span> = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; <span>//</span><span>random effects</span> <span> 4</span> <span>private</span> <span>$code</span>; <span>//</span><span>code </span> <span> 5</span> <span>private</span> <span>$codelen</span> = 4 ; <span>//</span><span>code length</span> <span> 6</span> <span>private</span> <span>$width</span> = 130; <span>//</span><span>width</span> <span> 7</span> <span>private</span> <span>$heigth</span> = 50; <span>//</span><span>height</span> <span> 8</span> <span>private</span> <span>$img</span>; <span>//</span><span>image handle</span> <span> 9</span> <span>private</span> <span>$font</span>; <span>//</span><span>font file</span> <span>10</span> <span>private</span> <span>$fontsize</span> = 20; <span>//</span><span>font size</span> <span>11</span> <span>private</span> <span>$fontcolor</span>; <span>//</span><span>font color </span><span>12</span> <span>13</span> <span> //the construct initialization</span> <span>14</span> <span>public</span> <span>function</span><span> __construct(){ </span><span>15</span> <span>$this</span>->font = ROOT_PATH.'/font/elephant.ttf'<span>; </span><span>16</span> <span> } </span><span>17</span> <span>18</span> <span>//</span><span>create random code from $charset</span> <span>19</span> <span>private</span> <span>function</span><span> createCode(){ </span><span>20</span> <span>$_len</span> = <span>strlen</span>(<span>$this</span>-><span>charset); </span><span>21</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=<span>$this</span>->codelen;<span>$i</span>++<span>){ </span><span>22</span> <span>$this</span>->code .= <span>$this</span>->charset[<span>mt_rand</span>(0,<span>$_len</span><span>)]; </span><span>23</span> <span> } </span><span>24</span> <span> } </span><span>25</span> <span>26</span> <span>//</span><span>create background</span> <span>27</span> <span>private</span> <span>function</span><span> createBg(){ </span><span>28</span> <span>$this</span>->img = imagecreatetruecolor(<span>$this</span>->width, <span>$this</span>-><span>heigth); </span><span>29</span> <span>$_color</span> = imagecolorallocate(<span>$this</span>->img, <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255<span>)); </span><span>30</span> imagefilledrectangle(<span>$this</span>->img,0,0,<span>$this</span>->width,<span>$this</span>->heigth,<span>$_color</span><span>); </span><span>31</span> <span> } </span><span>32</span> <span>33</span> <span>//</span><span>create font</span> <span>34</span> <span>private</span> <span>function</span><span> createFont(){ </span><span>35</span> <span>$_x</span> = <span>$this</span>->width / <span>$this</span>-><span>codelen; </span><span>36</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$this</span>->codelen;<span>$i</span>++<span>){ </span><span>37</span> <span>$this</span>->fontcolor = imagecolorallocate(<span>$this</span>->img, <span>mt_rand</span>(0,156), <span>mt_rand</span>(0,156), <span>mt_rand</span>(0,156<span>)); </span><span>38</span> imagettftext(<span>$this</span>->img, <span>$this</span>->fontsize, <span>mt_rand</span>(-30,30), <span>$_x</span>*<span>$i</span>+<span>mt_rand</span>(1,5), <span>$this</span>->heigth/1.4, <span>$this</span>->fontcolor, <span>$this</span>->font, <span>$this</span>->code[<span>$i</span><span>]); </span><span>39</span> <span> } </span><span>40</span> <span> } </span><span>41</span> <span>42</span> <span>//</span><span>create line,snowflake</span> <span>43</span> <span>private</span> <span>function</span><span> createLine(){ </span><span>44</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><6;<span>$i</span>++<span>){ </span><span>45</span> <span>$_color</span> = imagecolorallocate(<span>$this</span>->img, <span>mt_rand</span>(0,156), <span>mt_rand</span>(0,156), <span>mt_rand</span>(0,156<span>)); </span><span>46</span> imageline(<span>$this</span>->img, <span>mt_rand</span>(0,<span>$this</span>->width), <span>mt_rand</span>(0, <span>$this</span>->heigth),<span>mt_rand</span>(0,<span>$this</span>->width), <span>mt_rand</span>(0, <span>$this</span>->heigth), <span>$_color</span><span>); </span><span>47</span> <span> } </span><span>48</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){ </span><span>49</span> <span>$_color</span> = imagecolorallocate(<span>$this</span>->img, <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255<span>)); </span><span>50</span> imagestring(<span>$this</span>->img, <span>mt_rand</span>(1, 5), <span>mt_rand</span>(0,<span>$this</span>->width), <span>mt_rand</span>(0,<span>$this</span>->heigth), '*', <span>$_color</span><span>); </span><span>51</span> <span> } </span><span>52</span> <span> } </span><span>53</span> <span>54</span> <span>//</span><span>export image</span> <span>55</span> <span>private</span> <span>function</span><span> outPut(){ </span><span>56</span> <span>header</span>('Content-type:image/png'<span>); </span><span>57</span> imagepng(<span>$this</span>-><span>img); </span><span>58</span> imagedestroy(<span>$this</span>-><span>img); </span><span>59</span> <span> } </span><span>60</span> <span>61</span> <span>//</span><span>display</span> <span>62</span> <span>public</span> <span>function</span><span> doimg(){ </span><span>63</span> <span>$this</span>-><span>createBg(); </span><span>64</span> <span>$this</span>-><span>createCode(); </span><span>65</span> <span>$this</span>-><span>createLine(); </span><span>66</span> <span>$this</span>-><span>createFont(); </span><span>67</span> <span>$this</span>-><span>outPut(); </span><span>68</span> <span> } </span><span>69</span> <span>70</span> <span>//</span><span>get code</span> <span>71</span> <span>public</span> <span>function</span><span> getCode(){ </span><span>72</span> <span>return</span> <span>strtolower</span>(<span>$this</span>-><span>code); </span><span>73</span> <span> } </span><span>74</span> <span>75</span> <span> } </span><span>76</span>