function countries_from_freebase ()
{
// build query and issue it
$query = array'limit'=>1000, 'type'=>'/location/country', 'name'=>array(), 'iso3166_1_alpha2'=>array());
$results = freebase_query$query);
// extract country names and codes from the results
$return = array();
foreach ($results as $result) {
if (isset$result'iso3166_1_alpha2'][0])) {
$return$result'iso3166_1_alpha2'][0]] = $result'name'][0];
}
}
// sort
asort$return);
return $return
}
Category: Algorithm
Round Corners
<?php /** * round_corners * * Round the corners of an image. Transparency and anti-aliasing are supported. * * @version 0.2 * @author Contributors at eXorithm * @link /algorithm/view/round_corners Listing at eXorithm * @link /algorithm/history/round_corners History at eXorithm * @license /home/show/license * * @param resource $image (GD image) * @param number $radius Radius of the rounded corners. * @param string $color (hex color code) Color of the background. * @param mixed $transparency Level of transparency. 0 is no transparency, 127 is full transparency. * @return resource GD image */ function round_corners($image=null,$radius=20,$color='ffffff',$transparency='127') { $width = imagesx($image); $height = imagesy($image); $image2 = imagecreatetruecolor($width, $height); imagesavealpha($image2, true); imagealphablending($image2, false); imagecopy($image2, $image, 0, 0, 0, 0, $width, $height); $full_color = allocate_color($image2, $color, $transparency); // loop 4 times, for each corner... for ($left=0;$left<=1;$left++) { for ($top=0;$top<=1;$top++) { $start_x = $left * ($width-$radius); $start_y = $top * ($height-$radius); $end_x = $start_x+$radius; $end_y = $start_y+$radius; $radius_origin_x = $left * ($start_x-1) + (!$left) * $end_x; $radius_origin_y = $top * ($start_y-1) + (!$top) * $end_y; for ($x=$start_x;$x<$end_x;$x++) { for ($y=$start_y;$y<$end_y;$y++) { $dist = sqrt(pow($x-$radius_origin_x,2)+pow($y-$radius_origin_y,2)); if ($dist>($radius+1)) { imagesetpixel($image2, $x, $y, $full_color); } else { if ($dist>$radius) { $pct = 1-($dist-$radius); $color2 = antialias_pixel($image2, $x, $y, $full_color, $pct); imagesetpixel($image2, $x, $y, $color2); } } } } } } return $image2; } /** * allocate_color * * Helper function to allocate a color to an image. Color should be a 6-character hex string. * * @version 0.2 * @author Contributors at eXorithm * @link /algorithm/view/allocate_color Listing at eXorithm * @link /algorithm/history/allocate_color History at eXorithm * @license /home/show/license * * @param resource $image (GD image) The image that will have the color allocated to it. * @param string $color (hex color code) The color to allocate to the image. * @param mixed $transparency The level of transparency from 0 to 127. * @return mixed */ function allocate_color($image=null,$color='268597',$transparency='0') { if (preg_match('/[0-9ABCDEF]{6}/i', $color)==0) { throw new Exception("Invalid color code."); } if ($transparency<0 || $transparency>127) { throw new Exception("Invalid transparency."); } $r = hexdec(substr($color, 0, 2)); $g = hexdec(substr($color, 2, 2)); $b = hexdec(substr($color, 4, 2)); if ($transparency>127) $transparency = 127; if ($transparency<=0) return imagecolorallocate($image, $r, $g, $b); else return imagecolorallocatealpha($image, $r, $g, $b, $transparency); } /** * antialias_pixel * * Helper function to apply a certain weight of a certain color to a pixel in an image. The index of the resulting color is returned. * * @version 0.1 * @author Contributors at eXorithm * @link /algorithm/view/antialias_pixel Listing at eXorithm * @link /algorithm/history/antialias_pixel History at eXorithm * @license /home/show/license * * @param resource $image (GD image) The image containing the pixel. * @param number $x X-axis position of the pixel. * @param number $y Y-axis position of the pixel. * @param number $color The index of the color to be applied to the pixel. * @param number $weight Should be between 0 and 1, higher being more of the original pixel color, and 0.5 being an even mixture. * @return mixed */ function antialias_pixel($image=null,$x=0,$y=0,$color=0,$weight=0.5) { $c = imagecolorsforindex($image, $color); $r1 = $c['red']; $g1 = $c['green']; $b1 = $c['blue']; $t1 = $c['alpha']; $color2 = imagecolorat($image, $x, $y); $c = imagecolorsforindex($image, $color2); $r2 = $c['red']; $g2 = $c['green']; $b2 = $c['blue']; $t2 = $c['alpha']; $cweight = $weight+($t1/127)*(1-$weight)-($t2/127)*(1-$weight); $r = round($r2*$cweight + $r1*(1-$cweight)); $g = round($g2*$cweight + $g1*(1-$cweight)); $b = round($b2*$cweight + $b1*(1-$cweight)); $t = round($t2*$weight + $t1*(1-$weight)); return imagecolorallocatealpha($image, $r, $g, $b, $t); } ?>
eXorithm – Execute Algorithm: View / Run Algorithm evaluate_for_x
function evaluate_for_x ($equation, $x
{
return evaluate_equation$equation, array'x'=>$x));
}
eXorithm – Execute Algorithm: View / Run Algorithm plot_3d_function
function plot_3d_function ($function, $variable1, $variable2, $center_x, $center_y, $range, $z_scale, $width, $detail, $line_color, $surface_color
{
  $x1 = $center_x$range/2;
  $y1 = $center_y$range/2;
 Â
  $xfinal = $center_x$range/2;
  $yfinal = $center_y$range/2;
 Â
  // step size
  $step = $range$detail
 Â
  // all the polygons that make up the graph
  $polygons = array();
 Â
  // generate the polygons for the graph
  for ($ii=0;$ii<=$detail$ii++) {
    $x$x1+($ii$step);
    for ($jj=0;$jj<=$detail$jj++) {
      $y$y1+($jj$step);
     Â
      $z = evaluate_equation$function, array$variable1=>$x, $variable2=>$y))*$z_scale
     Â
      if (!is_nan$z) && !is_infinite$z)) {
       Â
        if ($ii$detail) {
          if ($jj$detail) {
            $poly = $ii$detail$jj+1;
            $polygons$poly][6] = $x
            $polygons$poly][7] = $y
            $polygons$poly][8] = $z
          }
         Â
          if ($jj>0) {
            $poly = $ii$detail$jj
            $polygons$poly][9] = $x
            $polygons$poly][10] = $y
            $polygons$poly][11] = $z
          }
        }
       Â
        if ($ii>0) {
          if ($jj$detail) {
            $poly = ($ii-1)*$detail$jj+1;
            $polygons$poly][3] = $x
            $polygons$poly][4] = $y
            $polygons$poly][5] = $z
          }
         Â
          if ($jj>0) {
            $poly = ($ii-1)*$detail$jj
            $polygons$poly][0] = $x
            $polygons$poly][1] = $y
            $polygons$poly][2] = $z
          }
        }
      }
    }
  }
 Â
  $polygons = array_values$polygons);
 Â
  // project each of the polygons into 2-d
  for ($i=0; $icount$polygons); $i++) {
    $points[] = project_polygon$polygons$i], -60, 180, 135, $center_x, $center_y, 0, $range, $range*2, true);
  }
 Â
  // scale the image somewhat
  $scale = $width*2/$range
 Â
  return render_polygons$points, $line_color, $surface_color, false, false, $width, $scale);
}Â
eXorithm – Execute Algorithm: View / Run Algorithm soundex
function soundex ($word
{
$letters = 'abcdefgijklmnopqrstuvxyz'
$codes = '012301202245501262301202'
$word = strtolower$word);
$soundex = ''
$count = 0;
for ($ii=0; $iistrlen$word); $ii++) {
$letter = $word$ii];
$p = strpos$letters, $letter);
if ($p!==false) {
if ($codes$p]!==substr$soundex,-1)) {
$soundex .= $codes$p];
if ($codes$p]!='0') $count++; //count only consonants
if ($count>=5) break; //if we have 5 characters we're sure we can break
}
}
}
// add initial letter
$init = substr$word, 0, 1);
$p = strpos$letters, $init);
if ($p===false)
$soundex = strtoupper$init).$soundex
else
$soundex = strtoupper$init).substr$soundex, 1);
// get rid of the vowels
$soundex = str_replace'0', '', $soundex);
// trim or pad
if (strlen$soundex)>=4) {
$soundex = substr$soundex, 0, 4);
} else {
$soundex .= str_repeat'0', 4-strlen$soundex));
}
return $soundex
}
eXorithm – Execute Algorithm: View / Run Algorithm php_manual
function php_manual ($function_name
{
$function_name = strtolowerstr_replace'_', '-', $function_name));
return "http://www.php.net/manual/en/function."$function_name".php"
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_hyphenate
function isbn_hyphenate ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric chars
// strip prefix from ISBN13
if (strlen$isbn)==13) {
$prefix = substr$isbn,0,3).'-'
$isbn = substr$isbn,3);
} else if (strlen$isbn)==10) {
$prefix = ''
} else {
return ''
}
$unknown = substr$isbn,1,4)+0;
$publisher = ''
$unit = ''
if (($isbn[0] == '0') || ($isbn[0] == '3') || ($isbn[0] == '4'))
// english region 0
// german region 3
// japan region 4
{
if$unknown<=1999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=2000 && $unknown<=6999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=7000 && $unknown<=8499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=8500 && $unknown<=8999)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=9000 && $unknown<=9499)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9500)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else if ($isbn[0] == '1'
// english region 1
{
if$unknown<=999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=1000 && $unknown<=3999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=4000 && $unknown<=5499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=5500 && $unknown<=8697)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=8698 && $unknown<=9989)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9990)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else
// other regions are not fully supported
{
return $prefixsubstr$isbn,0,9)."-"$isbn[9];
}
}
eXorithm – Execute Algorithm: View / Run Algorithm conv_isbn
function conv_isbn ($isbn
{
$isbn = "978" . $isbn; //add ‘978’ prefix
$isbn = str_split$isbn); //split into 13-element array
$m=3; //initialize variables
$sum=0;
for$i=0; $i<12; $i++)
{
($m==1) ? $m=3 : $m=1; //alternate $m between 1 & 3
$sum+=($isbn$i]*$m); //sum products
}
$isbn[12]=10-($sum%10); //Write new check digit (10 - $sum%10)
$isbn = implode$isbn);
return $isbn
}
eXorithm – Execute Algorithm: View / Run Algorithm word_counts
function word_counts ($text, $noise
{
$words = preg_split'/[^A-Za-z]+/', strtolower$text));
$counts = array();
foreach ($words as $word) {
if (strlen$word)>1) { // 1-letter words are ignored
if (array_search$word, $noise)===false) { // noise word?
if (array_key_exists$word, $counts)) {
$counts$word] = $counts$word]+1;
} else {
$counts$word] = 1;
}
}
}
}
return $counts
}
eXorithm – Execute Algorithm: Embed Algorithm create_gradient
Embed This Algorithm
This page will help you embed the algorithm create_gradient on a page on your own website. Just configure the inputs, then click the generate button to get a snippet of code you can paste onto your site. You have two options.
- You can embed the entire form. Users will be able to enter their own arguments, and will need to press the run button to execute the algorithm.
- You can add only the output of the algorithm to your website. There will be no argument inputs or run button.