Revision 1371
Added by Dietmar over 13 years ago
calc_ttf_image.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
// $Id$ |
|
4 |
|
|
5 |
/* |
|
6 |
|
|
7 |
Website Baker Project <http://www.websitebaker.org/> |
|
8 |
Copyright (C) 2004-2009, Ryan Djurovich |
|
9 |
|
|
10 |
Website Baker is free software; you can redistribute it and/or modify |
|
11 |
it under the terms of the GNU General Public License as published by |
|
12 |
the Free Software Foundation; either version 2 of the License, or |
|
13 |
(at your option) any later version. |
|
14 |
|
|
15 |
Website Baker is distributed in the hope that it will be useful, |
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
GNU General Public License for more details. |
|
19 |
|
|
20 |
You should have received a copy of the GNU General Public License |
|
21 |
along with Website Baker; if not, write to the Free Software |
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
|
24 |
*/ |
|
25 |
|
|
26 |
require_once("../../../config.php"); |
|
27 |
require_once(WB_PATH.'/include/captcha/captcha.php'); |
|
28 |
|
|
29 |
if(!isset($_SESSION['captcha_time'])) |
|
30 |
exit; |
|
31 |
unset($_SESSION['captcha_time']); |
|
32 |
|
|
33 |
// get lists of fonts and backgrounds |
|
34 |
require_once(WB_PATH.'/framework/functions.php'); |
|
35 |
$t_fonts = file_list(WB_PATH.'/include/captcha/fonts'); |
|
36 |
$t_bgs = file_list(WB_PATH.'/include/captcha/backgrounds'); |
|
37 |
$fonts = array(); |
|
38 |
$bgs = array(); |
|
39 |
foreach($t_fonts as $file) { if(preg_match('/\.ttf/',$file)) { $fonts[]=$file; } } |
|
40 |
foreach($t_bgs as $file) { if(preg_match('/\.png/',$file)) { $bgs[]=$file; } } |
|
41 |
|
|
42 |
// Captcha |
|
43 |
$_SESSION['captcha'] = ''; |
|
44 |
mt_srand((double)microtime()*1000000); |
|
45 |
$n = mt_rand(1,3); |
|
46 |
switch ($n) { |
|
47 |
case 1: |
|
48 |
$x = mt_rand(1,9); |
|
49 |
$y = mt_rand(1,9); |
|
50 |
$_SESSION['captcha'] = $x + $y; |
|
51 |
$cap = "$x+$y"; |
|
52 |
break; |
|
53 |
case 2: |
|
54 |
$x = mt_rand(10,20); |
|
55 |
$y = mt_rand(1,9); |
|
56 |
$_SESSION['captcha'] = $x - $y; |
|
57 |
$cap = "$x-$y"; |
|
58 |
break; |
|
59 |
case 3: |
|
60 |
$x = mt_rand(2,10); |
|
61 |
$y = mt_rand(2,5); |
|
62 |
$_SESSION['captcha'] = $x * $y; |
|
63 |
$cap = "$x*$y"; |
|
64 |
break; |
|
65 |
} |
|
66 |
$text = $cap; |
|
67 |
|
|
68 |
// choose a font and background |
|
69 |
$font = $fonts[array_rand($fonts)]; |
|
70 |
$bg = $bgs[array_rand($bgs)]; |
|
71 |
// get image-dimensions |
|
72 |
list($width, $height, $type, $attr) = getimagesize($bg); |
|
73 |
|
|
74 |
// create reload-image |
|
75 |
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay |
|
76 |
|
|
77 |
if(mt_rand(0,2)==0) { // 1 out of 3 |
|
78 |
|
|
79 |
// draw each character individualy |
|
80 |
$image = ImageCreateFromPNG($bg); // background image |
|
81 |
$grey = mt_rand(0,50); |
|
82 |
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color |
|
83 |
$ttf = $font; |
|
84 |
$ttfsize = 25; // fontsize |
|
85 |
$count = 0; |
|
86 |
$image_failed = true; |
|
87 |
$angle = mt_rand(-10,10); |
|
88 |
$x = mt_rand(20,35); |
|
89 |
$y = mt_rand($height-10,$height-2); |
|
90 |
do { |
|
91 |
for($i=0;$i<strlen($text);$i++) { |
|
92 |
$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i}); |
|
93 |
$angle = mt_rand(-10,10); |
|
94 |
$x = mt_rand($res[4],$res[4]+10); |
|
95 |
$y = mt_rand($height-12,$height-7); |
|
96 |
} |
|
97 |
if($res[4] > $width) { |
|
98 |
$image_failed = true; |
|
99 |
} else { |
|
100 |
$image_failed = false; |
|
101 |
} |
|
102 |
if(++$count > 4) // too many tries! Use the image |
|
103 |
break; |
|
104 |
} while($image_failed); |
|
105 |
|
|
106 |
} else { |
|
107 |
|
|
108 |
// draw whole string at once |
|
109 |
$image_failed = true; |
|
110 |
$count=0; |
|
111 |
do { |
|
112 |
$image = ImageCreateFromPNG($bg); // background image |
|
113 |
$grey = mt_rand(0,50); |
|
114 |
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color |
|
115 |
$ttf = $font; |
|
116 |
$ttfsize = 25; // fontsize |
|
117 |
$angle = mt_rand(0,5); |
|
118 |
$x = mt_rand(20,35); |
|
119 |
$y = mt_rand($height-10,$height-2); |
|
120 |
$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text); |
|
121 |
// check if text fits into the image |
|
122 |
if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && |
|
123 |
($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && |
|
124 |
($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) && |
|
125 |
($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height) |
|
126 |
) { |
|
127 |
$image_failed = false; |
|
128 |
} |
|
129 |
if(++$count > 4) // too many tries! Use the image |
|
130 |
break; |
|
131 |
} while($image_failed); |
|
132 |
|
|
133 |
} |
|
134 |
|
|
135 |
imagealphablending($reload, TRUE); |
|
136 |
imagesavealpha($reload, TRUE); |
|
137 |
|
|
138 |
// overlay |
|
139 |
imagecopy($reload, $image, 0,0,0,0, 140,40); |
|
140 |
imagedestroy($image); |
|
141 |
$image = $reload; |
|
142 |
|
|
143 |
captcha_header(); |
|
144 |
ob_start(); |
|
145 |
imagepng($image); |
|
146 |
header("Content-Length: ".ob_get_length()); |
|
147 |
ob_end_flush(); |
|
148 |
imagedestroy($image); |
|
149 |
|
|
1 |
<?php |
|
2 |
|
|
3 |
// $Id$ |
|
4 |
|
|
5 |
/* |
|
6 |
|
|
7 |
Website Baker Project <http://www.websitebaker.org/> |
|
8 |
Copyright (C) 2004-2009, Ryan Djurovich |
|
9 |
|
|
10 |
Website Baker is free software; you can redistribute it and/or modify |
|
11 |
it under the terms of the GNU General Public License as published by |
|
12 |
the Free Software Foundation; either version 2 of the License, or |
|
13 |
(at your option) any later version. |
|
14 |
|
|
15 |
Website Baker is distributed in the hope that it will be useful, |
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
GNU General Public License for more details. |
|
19 |
|
|
20 |
You should have received a copy of the GNU General Public License |
|
21 |
along with Website Baker; if not, write to the Free Software |
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
|
24 |
*/ |
|
25 |
|
|
26 |
require_once("../../../config.php"); |
|
27 |
require_once(WB_PATH.'/include/captcha/captcha.php'); |
|
28 |
|
|
29 |
if(!isset($_SESSION['captcha_time'])) |
|
30 |
exit; |
|
31 |
//unset($_SESSION['captcha_time']); // otherwise there can't be 2 captchas on one page! |
|
32 |
|
|
33 |
// get lists of fonts and backgrounds |
|
34 |
require_once(WB_PATH.'/framework/functions.php'); |
|
35 |
$t_fonts = file_list(WB_PATH.'/include/captcha/fonts'); |
|
36 |
$t_bgs = file_list(WB_PATH.'/include/captcha/backgrounds'); |
|
37 |
$fonts = array(); |
|
38 |
$bgs = array(); |
|
39 |
foreach($t_fonts as $file) if(eregi('\.ttf$',$file)) $fonts[]=$file; |
|
40 |
foreach($t_bgs as $file) if(eregi('\.png$',$file)) $bgs[]=$file; |
|
41 |
|
|
42 |
// Captcha |
|
43 |
$sec_id = ''; |
|
44 |
if(isset($_GET['s'])) $sec_id = $_GET['s']; |
|
45 |
$_SESSION['captcha'.$sec_id] = ''; |
|
46 |
mt_srand((double)microtime()*1000000); |
|
47 |
$n = mt_rand(1,3); |
|
48 |
switch ($n) { |
|
49 |
case 1: |
|
50 |
$x = mt_rand(1,9); |
|
51 |
$y = mt_rand(1,9); |
|
52 |
$_SESSION['captcha'.$sec_id] = $x + $y; |
|
53 |
$cap = "$x+$y"; |
|
54 |
break; |
|
55 |
case 2: |
|
56 |
$x = mt_rand(10,20); |
|
57 |
$y = mt_rand(1,9); |
|
58 |
$_SESSION['captcha'.$sec_id] = $x - $y; |
|
59 |
$cap = "$x-$y"; |
|
60 |
break; |
|
61 |
case 3: |
|
62 |
$x = mt_rand(2,10); |
|
63 |
$y = mt_rand(2,5); |
|
64 |
$_SESSION['captcha'.$sec_id] = $x * $y; |
|
65 |
$cap = "$x*$y"; |
|
66 |
break; |
|
67 |
} |
|
68 |
$text = $cap; |
|
69 |
|
|
70 |
// choose a font and background |
|
71 |
$font = $fonts[array_rand($fonts)]; |
|
72 |
$bg = $bgs[array_rand($bgs)]; |
|
73 |
// get image-dimensions |
|
74 |
list($width, $height, $type, $attr) = getimagesize($bg); |
|
75 |
|
|
76 |
// create reload-image |
|
77 |
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay |
|
78 |
|
|
79 |
if(mt_rand(0,2)==0) { // 1 out of 3 |
|
80 |
|
|
81 |
// draw each character individualy |
|
82 |
$image = ImageCreateFromPNG($bg); // background image |
|
83 |
$grey = mt_rand(0,50); |
|
84 |
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color |
|
85 |
$ttf = $font; |
|
86 |
$ttfsize = 25; // fontsize |
|
87 |
$count = 0; |
|
88 |
$image_failed = true; |
|
89 |
$angle = mt_rand(-10,10); |
|
90 |
$x = mt_rand(20,35); |
|
91 |
$y = mt_rand($height-10,$height-2); |
|
92 |
do { |
|
93 |
for($i=0;$i<strlen($text);$i++) { |
|
94 |
$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i}); |
|
95 |
$angle = mt_rand(-10,10); |
|
96 |
$x = mt_rand($res[4],$res[4]+10); |
|
97 |
$y = mt_rand($height-12,$height-7); |
|
98 |
} |
|
99 |
if($res[4] > $width) { |
|
100 |
$image_failed = true; |
|
101 |
} else { |
|
102 |
$image_failed = false; |
|
103 |
} |
|
104 |
if(++$count > 4) // too many tries! Use the image |
|
105 |
break; |
|
106 |
} while($image_failed); |
|
107 |
|
|
108 |
} else { |
|
109 |
|
|
110 |
// draw whole string at once |
|
111 |
$image_failed = true; |
|
112 |
$count=0; |
|
113 |
do { |
|
114 |
$image = ImageCreateFromPNG($bg); // background image |
|
115 |
$grey = mt_rand(0,50); |
|
116 |
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color |
|
117 |
$ttf = $font; |
|
118 |
$ttfsize = 25; // fontsize |
|
119 |
$angle = mt_rand(0,5); |
|
120 |
$x = mt_rand(20,35); |
|
121 |
$y = mt_rand($height-10,$height-2); |
|
122 |
$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text); |
|
123 |
// check if text fits into the image |
|
124 |
if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && |
|
125 |
($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && |
|
126 |
($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) && |
|
127 |
($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height) |
|
128 |
) { |
|
129 |
$image_failed = false; |
|
130 |
} |
|
131 |
if(++$count > 4) // too many tries! Use the image |
|
132 |
break; |
|
133 |
} while($image_failed); |
|
134 |
|
|
135 |
} |
|
136 |
|
|
137 |
imagealphablending($reload, TRUE); |
|
138 |
imagesavealpha($reload, TRUE); |
|
139 |
|
|
140 |
// overlay |
|
141 |
imagecopy($reload, $image, 0,0,0,0, 140,40); |
|
142 |
imagedestroy($image); |
|
143 |
$image = $reload; |
|
144 |
|
|
145 |
captcha_header(); |
|
146 |
ob_start(); |
|
147 |
imagepng($image); |
|
148 |
header("Content-Length: ".ob_get_length()); |
|
149 |
ob_end_flush(); |
|
150 |
imagedestroy($image); |
|
151 |
|
|
150 | 152 |
?> |
Also available in: Unified diff
captcha patch (Tks to FrankH)