Project

General

Profile

1
<?php
2

    
3
// $Id: 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
// make random string
43
if(!function_exists('randomString')) {
44
	function randomString($len) {
45
		list($usec, $sec) = explode(' ', microtime());
46
		mt_srand((float)$sec + ((float)$usec * 100000));
47
		//$possible="ABCDEFGHJKLMNPRSTUVWXYZabcdefghkmnpqrstuvwxyz23456789";
48
		$possible="abdfhkrsvwxz23456789";
49
		$str="";
50
		while(strlen($str)<$len) {
51
			$str.=substr($possible,(mt_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 reload-image
66
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay
67

    
68
if(mt_rand(0,2)==0) { // 1 out of 3
69

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

    
126
imagealphablending($reload, TRUE);
127
imagesavealpha($reload, TRUE);
128

    
129
// overlay
130
imagecopy($reload, $image, 0,0,0,0, 140,40);
131
imagedestroy($image);
132
$image = $reload;
133

    
134
captcha_header();
135
ob_start();
136
imagepng($image);
137
header("Content-Length: ".ob_get_length()); 
138
ob_end_flush();
139
imagedestroy($image);
140

    
141
?>
(12-12/13)