function blur_image ($image, $level
{
// based on http://www.tuxradar.com/practicalphp/11/2/25
$imagex = imagesx$image);
$imagey = imagesy$image);
$tc = imageistruecolor$image);
$new_image = imagecreatetruecolor$imagex, $imagey);
imagealphablending$new_image, false);
imagesavealpha$new_image, true);
$level = $level-1;
for ($x = 0; $x < $imagex; ++$x) {
for ($y = 0; $y < $imagey; ++$y) {
$newr = 0;
$newg = 0;
$newb = 0;
$newt = 0;
$colours = array();
$thiscol = imagecolorat$image, $x, $y);
for ($k = $x - $level; $k <= $x + $level; ++$k) {
for ($l = $y - $level; $l <= $y + $level; ++$l) {
if ($k < 0) { $colours[] = $thiscol; continue; }
if ($k >= $imagex) { $colours[] = $thiscol; continue; }
if ($l < 0) { $colours[] = $thiscol; continue; }
if ($l >= $imagey) { $colours[] = $thiscol; continue; }
$colours[] = imagecolorat$image, $k, $l);
}
}
foreach$colours as $colour) {
if ($tc) {
$newr += ($colour >> 16) & 0xFF;
$newg += ($colour >> 8) & 0xFF;
$newb += $colour & 0xFF;
$newt += ($colour >> 24) & 0xFF;
} else {
$c = imagecolorsforindex$image, $colour);
$newr += $c'red'];
$newg += $c'green'];
$newb += $c'blue'];
$newt += $c'alpha'];
}
}
$numelements = count$colours);
$newr /= $numelements
$newg /= $numelements
$newb /= $numelements
$newt /= $numelements
$newcol = imagecolorallocatealpha$new_image, $newr, $newg, $newb, $newt);
imagesetpixel$new_image, $x, $y, $newcol);
}
}
return $new_image
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm triangulate
function triangulate ($side1, $side2, $side3, $angle1, $angle2, $angle3, $radians
{
  $sides = array();
  $angles = array();
 Â
  // get sides
  if ($side1!='') $sides[1] = $side1+0;
  if ($side2!='') $sides[2] = $side2+0;
  if ($side3!='') $sides[3] = $side3+0;
 Â
  // get angles
  if ($angle1!='') $angles[1] = $angle1+0;
  if ($angle2!='') $angles[2] = $angle2+0;
  if ($angle3!='') $angles[3] = $angle3+0;
 Â
  // convert to radians if necessary
  if (!$radians) {
    foreach ($angles as $angle => $value) {
      $angles$angle] = deg2rad$value);
    }
  }
 Â
  // need 3 values and at least one side
  if (((count$sides)+count$angles))!=3) || (count$sides)==0)) {
    throw new Exception"You must only provide 3 values for the sides and angles. At least one value must be a side. Leave the other three values blank.");
  }
 Â
  // iterate three times to make sure that we get everything
  for ($count=0;$count<3;$count++) {
    // for the three sides/angles
    for ($i1=1;$i1<=3;$i1++) {
      $i2 = ($i1 % 3)+1; // the other side/angle
      $i3 = (($i1+1) % 3)+1; // the other other side/angle
      if (!isset$angles$i1])) {
        // try $angle1 = 180 - $angle2 - $angle3
        if ((isset$angles$i2])) && (isset$angles$i3]))) {
          $angles$i1] = pi() - $angles$i2] - $angles$i3];
        }
      }
      if (!isset$sides$i1])) {
        if (isset$angles$i1])) {
          // try $side1 = $side2 * (sin($angle1)/sin($angle2))
          if ((isset$sides$i2])) && (isset$angles$i2]))) {
            $sides$i1] = $sides$i2] * sin$angles$i1]) / sin$angles$i2]);
          }
          // try $side1 = $side3 * (sin($angle1)/sin($angle3))
          if ((isset$sides$i3])) && (isset$angles$i3]))) {
            $sides$i1] = $sides$i3] * sin$angles$i1]) / sin$angles$i3]);
          }
        }
      }
      if (!isset$angles$i1])) {
        if (isset$sides$i1])) {
          // try $angle1 = arcsin(sin($angle2) * $side1/$side2)
          if ((isset$sides$i2])) && (isset$angles$i2]))) {
            $angles$i1] = asinsin$angles$i2]) * $sides$i1] / $sides$i2]);
          }
          // try $angle1 = arcsin(sin($angle3) * $side1/$side3)
          if ((isset$sides$i3])) && (isset$angles$i3]))) {
            $angles$i1] = asinsin$angles$i3]) * $sides$i1] / $sides$i3]);
          }
        }
      }
      if (!isset$sides$i1])) {
        // try $side1 = sqrt($side2^2 + $side3^2 - 2*$side2*$side3*cos($angle1))
        if ((isset$sides$i2])) && (isset$sides$i3])) && (isset$angles$i1]))) {
          $sides$i1] = sqrt$sides$i2]*$sides$i2] + $sides$i3]*$sides$i3] - 2 * $sides$i2] * $sides$i3] * cos$angles$i1]));
        }
      }
      if (!isset$angles$i1])) {
        // try $angle1 = arccos($side2^2 + $side3^2 - $side1^2 / 2*$side2*$side3))
        if ((isset$sides$i2])) && (isset$sides$i3])) && (isset$sides$i1]))) {
          $angles$i1] = acos(($sides$i2]*$sides$i2] + $sides$i3]*$sides$i3] - $sides$i1]*$sides$i1]) / (2 * $sides$i2] * $sides$i3]));
        }
      }
    }
    // were we able to calculate everything?
    if ((count$sides)+count$angles))==6) break
  }
 Â
  // bad values?
  foreach ($angles as $value) {
    if (is_nan$value) || is_infinite$value) || ($value<=0)) {
      throw new Exception"A triangle cannot be constructed with those values.");
    }
  }
 Â
  ksort$sides);ksort$angles);
 Â
  // convert to degrees if necessary
  if (!$radians) {
    foreach ($angles as $angle => $value) {
      $angles$angle] = rad2deg$value);
    }
  }
 Â
  return (array'sides'=>$sides, 'angles'=>$angles));
}Â
eXorithm – Execute Algorithm: View / Run Algorithm backwards
function backwards ($text
{
// this function will put the words in $text backwards
$text = str_replace" ", " ", $text);
$text = strip_tags$text);
$text = str_replace"n", " ", $text);
$text = str_replace"r", " ", $text);
$text = preg_replace'/s+/', ' ', $text);
$arr = explode" ", $text);
$arr = array_reverse$arr);
return implode" ", $arr);
}
eXorithm – Execute Algorithm: View / Run Algorithm swirl_image
function swirl_image ($image, $iterations, $angle
{
  $height = imagesy$image);
  $width = imagesx$image);
 Â
  $new_image = imagecreatetruecolor$width, $height);
  $iterations = max(1, $iterations);
 Â
  $wstep = ($width/2)/($iterations+1);
  $hstep = ($height/2)/($iterations+1);
 Â
  for ($ii=0;$ii$iterations$ii++) {
    $w = $width - ($ii+1)*$wstep*2;
    $h = $height - ($ii+1)*$hstep*2;
    $images$ii] = imagecreatetruecolor$w, $h);
    imagecopy$images$ii], $image, 0, 0, ($ii+1)*$wstep, ($ii+1)*$hstep, $w, $h);
    $images$ii] = rotate_image_alpha$images$ii], $angle*($ii+1), 'ffffff', 127);
  }
 Â
  for ($ii=0;$ii$iterations$ii++) {
    // Set the brush
    imagesetbrush$image, $images$ii]);
    // Draw a couple of brushes, each overlaying each
    imageline$image, imagesx$image) / 2, imagesy$image) / 2, imagesx$image) / 2, imagesy$image) / 2, IMG_COLOR_BRUSHED);
  }
 Â
  return $image
}Â
eXorithm – Execute Algorithm: View / Run Algorithm duotone_image
function duotone_image ($image, $rplus, $gplus, $bplus, $pcnt
{
// Adapted from http://www.tuxradar.com/practicalphp/11/2/21
$imagex = imagesx$image);
$imagey = imagesy$image);
$image2 = imagecreatetruecolor$imagex, $imagey);
imagesavealpha$image2, true);
imagealphablending$image2, false);
for ($x = 0; $x <$imagex; ++$x) {
for ($y = 0; $y <$imagey; ++$y) {
$rgb = imagecolorat$image, $x, $y);
$color = imagecolorsforindex$image, $rgb);
$grey = floor(($color'red']+$color'green']+$color'blue'])/3);
if ($pcnt) {
$red = $grey + $grey*($rplus/150);
$green = $grey + $grey*($gplus/150);
$blue = $grey + $grey*($bplus/150);
} else {
$red = $grey + $rplus
$green = $grey + $gplus
$blue = $grey + $bplus
}
if ($red > 255) $red = 255;
if ($green > 255) $green = 255;
if ($blue > 255) $blue = 255;
if ($red < 0) $red = 0;
if ($green < 0) $green = 0;
if ($blue < 0) $blue = 0;
$newcol = imagecolorallocatealpha$image2, $red$green$blue$color'alpha']);
imagesetpixel ($image2, $x, $y, $newcol);
}
}
return $image2
}
eXorithm – Execute Algorithm: View / Run Algorithm frame_picture
function frame_picture ($image, $thickness
{
$color = get_average_color$image);
$image2 = imagecreatetruecolorimagesx$image)+$thickness$thickness
imagesy$image)+$thickness$thickness);
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image2, $r, $g, $b);
imagefilledrectangle$image2, 0, 0, imagesx$image2)-1, imagesy$image2)-1, $color);
imagecopy$image2, $image, $thickness, $thickness, 0, 0, imagesx$image)-1, imagesy$image)-1);
return $image2
}
eXorithm – Execute Algorithm: View / Run Algorithm volume_cube
function volume_cube ($width$height$depth
{
return ($width * $depth * $height);
}
eXorithm – Execute Algorithm: View / Run Algorithm country_population_graph
function country_population_graph ($country_list, $data_source
{
# ------- The external data comes from elsewhere, load it....
if ($data_source=='geonames') {
$valuesgeonames_populations$country_list);
} else if ($data_source=='freebase') {
$valuesfreebase_populations$country_list);
} else {
$valuesxithm_world_stats_population$country_list);
}
ksort$values);
// how much rounding to do for the numbers on the y-axis
$zeroes = strlen""roundmax$values)))-2;
$divs = roundmax$values)/10, $zeroes*-1);
$chrt = new stdClass
$chrt->cht = "bvs"
$chrt->chds = "0,"max$values);
$chrt->chxt = "x,y"
$chrt->chs = "600x300"
$chrt->chbh = "a"
$chrt->chxr = "1,0,"max$values).","$divs
$chrt->chxl="0:|"implode"|", array_keys$values));
$chrt->chd = "t:"implode",", $values);
return $chrt
}
eXorithm – Execute Algorithm: View / Run Algorithm pluralize
function pluralize ($word, $exceptions
{
if (array_key_exists$word, $exceptions))
return $exceptions$word];
if (substr_compare$word, 'y', -1, 1)==0) {
if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u''y'))===false)
return substr$word, 0, -1).'ies'
else
return $word's'
}
if (substr_compare$word, 'o', -1, 1)==0) {
if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u'))===false)
return $word'es'
else
return $word's'
}
if (substr_compare$word, 's', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'x', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'z', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'ch', -2, 2)==0)
return $word'es'
if (substr_compare$word, 'sh', -2, 2)==0)
return $word'es'
return $word's'
}
eXorithm – Execute Algorithm: View / Run Algorithm ordinal
function ordinal ($num
{
// get the last two digits
$last = $num % 100;
// get last digit unless the number is in the teens
$last = ($last < 20) ? $last : $num % 10;
$ord = ($last==1) ? 'st' : (($last==2) ? 'nd' : (($last==3) ? 'rd' : 'th'));
return $num$ord
}