All pastes #732054 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-10-10 16:48 UTC
rendered paste body
<?php
	class verification_image {
		var $image;
		var $_w;
		var $_h;
		var $text_color;
		var $bg_color;
		var $ttf_font;

		function verification_image($width = 10, $height = 50, $font = "") {
			$this->_w = $width;
			$this->_h = $height;
			
			$this->ttf_font = $font;
		}
		
		function init() {
			
			$this->image = imagecreate($this->_w, $this->_h);
			//$background_color = imagecolorallocate($this->image, 255, 255, 255);
			$this->set_bgcolor(255,255,255);
			//$this->set_textcolor(54,149,243);
			$this->set_textcolor(mt_rand(1,255),mt_rand(1,255),mt_rand(1,255));
			
		}		
		
		function set_bgcolor($r,$g,$b) {
			$background_color = imagecolorallocate($this->image, $r, $g, $b);	
			
		}
		
		function set_textcolor($r,$g,$b) {
			$this->text_color = imagecolorallocate($this->image, $r, $g, $b);	
		}
		
		function draw() {
			$code = $this->generate_code();
			$this->register_code($code);

			$x = 4;
			$y = 20;

			for($i = 0; $i < strlen($code); $i++) {
			
				$calc_y = rand(200,250) / 10;
				//imagestring( $this->image, 3, $x, $calc_y,  $code{$i}, $this->text_color);	
				$angle = rand(-20,20);
				$this->write_string($x, $calc_y, $angle, $code{$i});
					$x += rand(17,19) ;
			}
			
		}
		
		
		function write_string($x_offset, $y_offset, $angle, $string) {
				if($this->ttf_font != "") {
					if(file_exists($this->ttf_font)) {
						putenv('GDFONTPATH=' . realpath('.'));
						$font_size = 13;
						$grey = imagecolorallocate($this->image, 128, 128, 128);
						imagettftext($this->image, $font_size, $angle, $x_offset + 1, $y_offset + 1, $grey, $this->ttf_font, $string);	
						imagettftext($this->image, $font_size, $angle, $x_offset, $y_offset, $this->text_color, $this->ttf_font, $string);
					
					} else {
						die("Font doesn't exist, or not in same directory as a .ttf");
					}
				} else {
					die("No font set.");
				}
		}

		function generate_code() {
			$string = md5(rand(0,9999));
			$output = substr($string, 14, 3);
			return $output;
		}

		function get_registered_code() {
			return $_SESSION['verification_key'];
		}

		function register_code($c) {
			$_SESSION['verification_key'] = $c;
		}

		function validate_code($code) {
			return $code == $this->get_registered_code(); 
		}

		function _output() {
			$this->init();
			$this->draw();
			header("Content-type: image/png");
			imagepng($this->image);
			imagedestroy($this->image);
		}
	
}




?>