function morse ($data, $direction
{
$codes = array
'a' => '.-'
'b' => '-...'
'c' => '-.-.'
'd' => '-...'
'e' => '.'
'f' => '..-.'
'g' => '--.'
'h' => '....'
'i' => '..'
'j' => '.---'
'k' => '-.-'
'l' => '.-..'
'm' => '--'
'n' => '-.'
'o' => '---'
'p' => '.--.'
'q' => '--.-'
'r' => '.-.'
's' => '...'
't' => '-'
'u' => '..-'
'v' => '...-'
'w' => '.--'
'x' => '-..-'
'y' => '-.--'
'z' => '--..'
'0' => '-----'
'1' => '.----'
'2' => '..---'
'3' => '...--'
'4' => '....-'
'5' => '.....'
'6' => '-....'
'7' => '--...'
'8' => '---..'
'9' => '----.'
',' => '--..--'
'.' => '.-.-.-'
'?' => '..--..'
);
$output = ''
if ($direction=='text') {
$codes = array_flip$codes);
$elements = explode' ', $data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element];
}
} else {
$data = strtolower$data);
$elements = str_split$data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element].' '
}
$output = trim$output);
}
return $output
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm get_average_color
function get_average_color ($image
{
$y = imagesy$image);
$x = imagesx$image);
$r_total = 0;
$g_total = 0;
$b_total = 0;
$tc = imageistruecolor$image);
for ($ii=0;$ii$y$ii++) {
for ($jj=0;$jj$x$jj++) {
$rgb = imagecolorat$image, $jj, $ii);
if ($tc) {
$r_total += ($rgb >> 16) & 0xFF;
$g_total += ($rgb >> 8) & 0xFF;
$b_total += $rgb & 0xFF;
$count++;
} else {
$rgb = imagecolorsforindex$image, $rgb);
if ($rgb'alpha']==0) {
$r_total += $rgb'red'];
$g_total += $rgb'green'];
$b_total += $rgb'blue'];
$count++;
}
}
}
}
$r = dechexround$r_total/($count)));
$g = dechexround$g_total/($count)));
$b = dechexround$b_total/($count)));
if (strlen$r)==1) $r = '0'$r
if (strlen$g)==1) $g = '0'$g
if (strlen$b)==1) $b = '0'$b
return $r$g$b
}
eXorithm – Execute Algorithm: View / Run Algorithm calendar_month
function calendar_month ($month, $year
{
$start = getdatemktime(0,0,0,$month,1,$year));
$end = getdatemktime(0,0,0,$month+1,0,$year));
$end_date = $end'mday'];
$start_day = $start'wday'];
$html = "<table border=1><tr>
<th width=100>Sunday</th>
<th width=100>Monday</th>
<th width=100>Tuesday</th>
<th width=100>Wednesday</th>
<th width=100 >Thursday</th>
<th width=100>Friday</th>
<th width=100>Saturday</th>
</tr>n"
$date = 0;
$week_day = 0;
while ($date$end_date) {
if ($week_day==0)
$html .= "<tr>n"
if ($date==0)
if ($week_day==$start_day
$date++;
else
$date++;
if ($date==0)
$html .= "<td></td>"
else
$html .= "<td height=85 valign=top>$date</td>";
$week_day = ($week_day+1) % 7;
if ($week_day==0)
$html .= "</tr>n"
}
if ($week_day!=0) {
$html .= str_repeat"<td></td>", 7-$week_day);
$html .= "</tr>n"
}
$html .= "</table>"
return $html
}
eXorithm – Execute Algorithm: View / Run Algorithm factorial
function factorial ($number
{
$factorial = 1;
while$number > 1)
{
$factorial *= $number
$number--;
}
return $factorial
}
eXorithm – Execute Algorithm: View / Run Algorithm sort_multi_array
function sort_multi_array ($array, $key
{
$keys = array();
for ($i=1;$ifunc_num_args();$i++) {
$keys$i-1] = func_get_arg$i);
}
// create a custom search function to pass to usort
$func = function ($a, $b) use ($keys) {
for ($i=0;$icount$keys);$i++) {
if ($a$keys$i]] != $b$keys$i]]) {
return ($a$keys$i]] < $b$keys$i]]) ? -1 : 1;
}
}
return 0;
};
usort$array, $func);
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm equilateral_shape
function equilateral_shape ($radius, $sides, $color
{
// blank image
$image = image_create_alpha$radius*2+4, $radius*2+4);
// create the color
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
// The fudge factor is only used to rotate the shape so a flat
// line is at the bottom.
if (($sides%2)==0) {
if (($sides/2%2)==0) {
$fudge = pi()*1/$sides
} else {
$fudge = 0;
}
} else {
if ((($sides-1)/2%2)==0) {
$fudge = pi()*1.5/$sides
} else {
$fudge = pi()*0.5/$sides
}
}
$x0 = 0;
$y0 = 0;
// for the number of sides...
for ($i=0; $i$sides; $i++) {
// compute a point on the perimeter $i/$sides from the beginning
$x1 = round$radiuscos(2*pi()*$i$sides$fudge)+$radius+2);
$y1 = round$radiussin(2*pi()*$i$sides$fudge)+$radius+2);
if ($i==0) {
// If this is the first point then we can't draw a line yet
// because we don't have a second set of points to connect to.
// However, we need we need these points to connect the last
// set of points to.
$x0 = $x1
$y0 = $y1
} else {
// draw a line
imageline$image, $x1, $y1, $x2, $y2, $color);
}
// remember these points
$x2 = $x1
$y2 = $y1
}
// draw the final line
imageline$image, $x2, $y2, $x0, $y0, $color);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_sort
function simple_sort ($array
{
sort$array);
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm convert2grayscale
function convert2grayscale ($image
{
$height = imagesy$image);
$width = imagesx$image);
$tc = imageistruecolor$image);
$new_image = imagecreatetruecolor$width, $height);
imagealphablending$new_image, false);
imagesavealpha$new_image, true);
for ($ii=0; $ii$width; $ii++) {
for ($jj=0; $jj$height; $jj++) {
$rgb = imagecolorat$image, $ii, $jj);
if ($tc) {
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb >> 24) & 0xFF;
} else {
$rgb = imagecolorsforindex$image, $rgb);
$r = $rgb'red'];
$g = $rgb'green'];
$b = $rgb'blue'];
$alpha = $rgb'alpha'];
}
$avg = round(($r$g$b)/3);
$color = imagecolorallocatealpha$new_image, $avg, $avg, $avg, $alpha);
imagesetpixel$new_image, $ii, $jj, $color);
}
}
return $new_image
}
eXorithm – Execute Algorithm: View / Run Algorithm rot13
function rot13 ($string
{
for$i=0;$istrlen$string);$i++) {
$jord$string$i]);
if ((($j>=ord"n")) & ($j<=ord"z"))) | ($j>=ord"N")) & ($j<=ord"Z"))) {
$j$j-13;
}
elseif ((($j>=ord"a")) & ($j<=ord"m"))) | ($j>=ord"A")) & ($j<=ord"M"))) {
$j$j+13;
}
$new.=chr$j);
}
return$new);
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_sierpinski
function draw_sierpinski ($size, $color
{
// see http://en.wikipedia.org/wiki/Sierpinski_triangle
// this code from http://php.net/manual/en/function.imagesetpixel.php
$hght = roundsqrt$size$size - ($size/2)*($size/2)));
$diff = round(($size - $hght)/2);
$gd = imagecreatetruecolor$size, $size);
$corners[0] = array'x' => round$size/2), 'y' => $diff);
$corners[1] = array'x' => 0, 'y' => $size$diff);
$corners[2] = array'x' => $size, 'y' => $size$diff);
$red = hexdecsubstr$color, 0, 2));
$green = hexdecsubstr$color, 2, 2));
$blue = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$gd, $red, $green, $blue);
$x = $size
$y = $size
for ($i = 0; $i < 200000; $i++) {
imagesetpixel$gd, round$x),round$y), $color);
$a = rand(0, 2);
$x = ($x + $corners$a]['x']) / 2;
$y = ($y + $corners$a]['y']) / 2;
}
return $gd
}