KISS Stuff for MySQL
SI suffix for numbers.
function si($v) {
$six=array('K','M','G','T','P','E','Are you nuts?');
$sis='';
while ($v>800) {
$v=$v/1024.0;
$sis=array_shift($six);
}
return (round($v*10)/10.0).$sis;
}
It's a nice and quick function to take those ridiculous large numbers and slab k/m/g afterward.
Escape, if necessary, with quotes for MySQL.
function mesc($s) {
if (is_numeric($s)) return $s;
return "'".mysql_escape_string($s)."'";
}
Can't remember how many time's I wrote that code..
HSV Processing, good for picture twisting & captcha
class ColorMangler {
// HSV Values:Number 0-1
// RGB Results:Number 0-255
var $authorative="rgb";
var $r,$g,$b,$h,$s,$v;
function toHSV () {
$var_R = ($this->r / 255);
$var_G = ($this->g / 255);
$var_B = ($this->b / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$V = $var_Max;
if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
$S = $del_Max / $var_Max;
$del_R = ( ( ( $max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
if (H<0) $H++;
if (H>1) $H--;
}
$this->h = $H;
$this->s = $S;
$this->v = $V;
}
function toRGB () {
$H=$this->h;
$S=$this->s;
$V=$this->v;
if($S == 0)
{
$R = $G = $B = $V * 255;
}
else
{
$var_H = $H * 6;
$var_i = floor( $var_H );
$var_1 = $V * ( 1 - $S );
$var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
$var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );
if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }
$R = $var_R * 255;
$G = $var_G * 255;
$B = $var_B * 255;
}
$this->r = $R;
$this->g = $G;
$this->b = $B;
}
function toHEX() {
if ($this->authorative=="hsv") $this->toRGB();
return sprintf('#%02X%02X%02X', $this->r, $this->g, $this->b);
}
function toCSSRGB() {
if ($this->authorative=="hsv") $this->toRGB();
return "rgb(".$this->r.",".$this->g.",".$this->b.")";
}
function getTrueColor(&$im) {
if ($this->authorative=="hsv") $this->toRGB();
return imagecolorallocate($im,$this->r,$this->g,$this->b);
}
}