Project

General

Profile

1
<?php
2

    
3
// $Id: ttf_image.php 610 2008-01-27 00:03:04Z thorn $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, 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.'/search/search_modext.php');
35
list($fonts, $dirs) = list_files_dirs(WB_PATH.'/include/captcha/fonts', false);
36
list($bgs, $dirs) = list_files_dirs(WB_PATH.'/include/captcha/backgrounds', false);
37
$fonts = clear_filelist($fonts, '\.ttf$');
38
$bgs = clear_filelist($bgs, '\.png$');
39

    
40
// make random string
41
if(!function_exists('randomString')) {
42
	function randomString($len) {
43
		list($usec, $sec) = explode(' ', microtime());
44
		srand((float)$sec + ((float)$usec * 100000));
45
		//$possible="ABCDEFGHJKLMNPRSTUVWXYZabcdefghkmnpqrstuvwxyz23456789";
46
		$possible="abdfhkrsvwxz23456789";
47
		$str="";
48
		while(strlen($str)<$len) {
49
			$str.=substr($possible,(rand()%(strlen($possible))),1);
50
		}
51
		return($str);
52
	}
53
}
54
$text = randomString(5); // number of characters
55
$_SESSION['captcha'] = $text; 
56

    
57
// choose a font and background
58
$font = $fonts[array_rand($fonts)];
59
$bg = $bgs[array_rand($bgs)];
60
// get image-dimensions
61
list($width, $height, $type, $attr) = getimagesize($bg);
62

    
63
// create image
64

    
65
$image = ImageCreateFromPNG($bg); // background image
66
$grey = rand(0,50);
67
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
68
$ttf = $font;
69
$ttfsize = 25; // fontsize
70
$angle = rand(-15,15);
71
$x = rand(10,25);
72
$y = rand($height-10,$height-2);
73

    
74
if(rand(0,2)==0) { // 1 out of 3
75

    
76
	// draw each character individualy
77
	$count = 0;
78
	$image_failed = true;
79
	do {
80
		for($i=0;$i<5;$i++) {
81
			$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i});
82
			$angle = rand(-15,15);
83
			$x = rand($res[4],$res[4]+10);
84
			$y = rand($height-15,$height-5);
85
		}
86
		if($res[4] > $width) {
87
			$image_failed = true;
88
		} else {
89
			$image_failed = false;
90
		}
91
		if(++$count > 4) // too many tries! Use the image
92
			break;
93
	} while($image_failed);
94
	
95
} else {
96
	
97
	// draw whole string at once
98
	$image_failed = true;
99
	$count=0;
100
	do {
101
		$image = ImageCreateFromPNG($bg); // background image
102
		$grey = rand(0,50);
103
		$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
104
		$ttf = $font;
105
		$ttfsize = 25; // fontsize
106
		$angle = rand(0,5);
107
		$t_x = rand(5,30);
108
		$t_y = rand($height-10,$height-2);
109
		$res = imagettftext($image, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text);
110
		// check if text fits into the image
111
		if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && 
112
			 ($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && 
113
			 ($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) && 
114
			 ($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height)
115
		) {
116
			$image_failed = false;
117
		}
118
		if(++$count > 5) // too many tries! Use the image
119
			break;
120
	} while($image_failed);
121
	
122
}
123

    
124
captcha_header();
125
ob_start();
126
imagepng($image);
127
header("Content-Length: ".ob_get_length()); 
128
ob_end_flush();
129
imagedestroy($image);
130

    
131
?>
(8-8/9)