Susan will have to speak to the mystery variable problem, I’m taking her word for it.
I do have and editor with an excellent code explorer, although I always appreciate a coder saving me the time with a comment.
You and I obviously have really different ideas about what constitutes "ugly" code. I can often figure out what code is doing by looking at the raw code, but not very easily when the code is calling member functions of objects it has instantiated that in turn call functions of objects *it* instantiates. Why not just tell me and save me the time. Even when the code is self-contained, why take the chance that I misinterpret what it’s doing or why? If I had a nickel for every time I’ve misinterpreted my *own* code when looking at it after a long delay, I could have retired long ago.
Put the code in the documentation section at the top? Why make me bounce back and forth or open a separate window? I use end-of-line comments often because I can hit the end key and two slashes much faster than I can do anything else and the easier the job of commenting, the more comments there are. In my book, there’s no such thing as over-commented code. I’m sure you disagree.
Here’s some code (a little of it is mine but mostly from others) that I find attractive and you probably consider ugly:
/*
Class for producing CAPTCHA-style images.
This class generates an image with random or selected text
to be used in form verification. It has visual
elements design to confuse OCR software preventing
the use of BOTS.
Original Author: Huda M Elmatsani
Modifications: Extensively modified by Bob Ray and many others
Original date: 25/07/2004
Copyright (c) 2004 Huda M Elmatsani All rights reserved.
This program is free for any purpose use.
USAGE
create some image with noise texture, put in image directory,
rename to noise_, see examples
put some true type font into font directory,
rename to font_, see exmplae
you can search and put free font you like
*/
class VeriWord {
/* path to font directory*/
var $dir_font = "fonts/";
/* path to background image directory*/
var $dir_noise = "noises/";
var $word = "";
var $im_width = 0;
var $im_height = 0;
function VeriWord($w=200, $h=80) {
//$this->set_veriword(); // called directly by user
$this->im_width = $w;
$this->im_height = $h;
}
/* Set_veriword has three options. With no argument, it picks calls pick_word(),which
selects a random word and appends a random number to it. With the argument "MathString"
*/
function set_veriword($useWord="") {
/* create session variable for verification,
you may change the session variable name */
if ($useWord == "") { // no word sent, use random word
$this->word = $this->pick_word();
$_SESSION['veriword'] = $this->word;
} elseif ($useWord == "MathString") { // use math string for veriword
require "mathstringclass.inc.php";
$ms = new MathString();
$this->word = $ms->getDisplayString();
$_SESSION['veriword'] = $ms->getValue();
} else { // word sent as argument - use it.
$this->word = $useWord;
$_SESSION['veriword'] = $this->word;
}
}
function output_image() {
/* output the image as jpeg */
$this->draw_image();
header("Content-type: image/jpeg");
imagejpeg($this->im);
}
function pick_word() { // private - do not call directly. set_veriword() with no arg calls it automaticaly
// set default words
$words="Ack,Arps,Alag,Atex,Bek,Bix,Chux,Caxt,Depp,Dex,Ext,Enya,Fet,Fets,Tek,Text,Gurk,Gex,Het,Heft,Inet,Ibex,Jax,Jerp,Jink,Lak,Lest,Liv,Mars,Mamp,Nex,Nelp,Paxt,Pex,Reks,Rix,Snix,Sept,Turp,Thix,Elps,Vix,Veks,Wect,Wex,Yap,Yef,Yeff,Zub,Zeks";
$arr_words = explode(",", $words);
/* pick one randomly for text verification */
$vWord = (string) $arr_words[array_rand($arr_words)];
$vNum = (string) rand(10,999);
while(strpos($vNum, "0")) { // Change all zeros to threes
$vNum = str_replace("0", "3", $vNum);
}
return $vWord.$vNum;
}
function draw_text() {
// dynamically load GD2 lib
if (!extension_loaded('gd')) {
if (strtoupper(substr(PHP_OS, 0,3) == 'WIN')) {
@dl('php_gd2.dll');
} else {
@dl('gd2.so');
}
}
/* pick one font type randomly from font directory */
//$text_font = $this->dir_font."".rand(1,3).".ttf";
// added by Alex - read ttf dir
$dir = dir("./ttf");
$fontstmp = array();
while (false !== ($file = $dir->read())) {
if($file!="." && $file!=".." && file_exists('./ttf/'.$file) && !is_dir('./ttf/'.$file)) {
$fontstmp[] = './ttf/'.$file;
}
}
$dir->close();
$text_font = (string) $fontstmp[array_rand($fontstmp)];
/* angle for text inclination */
$text_angle = rand(-9,9);
/* initial text size */
$text_size = 30;
/* calculate text width and height */
$box = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word);
$text_width = $box[2]-$box[0]; //text width
$text_height= $box[5]-$box[3]; //text height
/* adjust text size */
$text_size = round((18 * $this->im_width)/$text_width);
/* recalculate text width and height */
$box = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word);
$text_width = $box[2]-$box[0]; //text width
$text_height= $box[5]-$box[3]; //text height
/* calculate center position of text */
$text_x = ($this->im_width - $text_width)/2;
$text_y = ($this->im_height - $text_height)/2;
/* create canvas for text drawing */
$im_text = imagecreate ($this->im_width, $this->im_height);
$bg_color = imagecolorallocate ($im_text, 255, 255, 255);
/* pick color for text */
$text_color = imagecolorallocate ($im_text, 0, 51, 153);
/* draw text into canvas */
imagettftext( $im_text,
$text_size,
$text_angle,
$text_x,
$text_y,
$text_color,
$text_font,
$this->word);
/* remove background color */
imagecolortransparent($im_text, $bg_color);
return $im_text;
imagedestroy($im_text);
}
function draw_image() {
/* pick one background image randomly from image directory */
$img_file = $this->dir_noise."noise".rand(1,4).".jpg";
/* create "noise" background image from your image stock*/
$noise_img = @imagecreatefromjpeg ($img_file);
$noise_width = imagesx($noise_img);
$noise_height = imagesy($noise_img);
/* resize the background image to fit the size of image output */
$this->im = imagecreatetruecolor($this->im_width,$this->im_height);
imagecopyresampled ($this->im,
$noise_img,
0, 0, 0, 0,
$this->im_width,
$this->im_height,
$noise_width,
$noise_height);
/* put text image into background image */
imagecopymerge ( $this->im,
$this->draw_text(),
0, 0, 0, 0,
$this->im_width,
$this->im_height,
70 );
return $this->im;
}
function destroy_image() {
imagedestroy($this->im);
}
}