Project

General

Profile

1
<?php
2

    
3
// $Id: calc_ttf_image.php 915 2009-01-21 19:27:01Z Ruebenwurzel $
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(eregi('\.ttf$',$file)) $fonts[]=$file;
40
foreach($t_bgs as $file) if(eregi('\.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

    
150
?>
(5-5/13)