Project

General

Profile

1
<?php
2

    
3
// $Id: ttf_image.php 615 2008-01-27 14:29:23Z 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.'/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
// make random string
43
if(!function_exists('randomString')) {
44
	function randomString($len) {
45
		list($usec, $sec) = explode(' ', microtime());
46
		srand((float)$sec + ((float)$usec * 100000));
47
		//$possible="ABCDEFGHJKLMNPRSTUVWXYZabcdefghkmnpqrstuvwxyz23456789";
48
		$possible="abdfhkrsvwxz23456789";
49
		$str="";
50
		while(strlen($str)<$len) {
51
			$str.=substr($possible,(rand()%(strlen($possible))),1);
52
		}
53
		return($str);
54
	}
55
}
56
$text = randomString(5); // number of characters
57
$_SESSION['captcha'] = $text; 
58

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

    
65
// create image
66
$image = ImageCreateFromPNG($bg); // background image
67
$grey = rand(0,50);
68
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
69
$ttf = $font;
70
$ttfsize = 25; // fontsize
71

    
72
if(rand(0,2)==0) { // 1 out of 3
73

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

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

    
132
?>
(12-12/13)