Blog
Create Gradient
<?php
/**
 * create_gradient
 *
 * Create an image that is a color gradient.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/create_gradient Listing at eXorithm
 * @link /algorithm/history/create_gradient History at eXorithm
 * @license /home/show/license
 *
 * @param string $start_color (hex color code) The start color of the gradient.
 * @param string $end_color (hex color code) The end color of the gradient.
 * @param number $size The length of the resulting image.
 * @param number $thickness The thickness of the resulting image.
 * @param mixed $orientation The orientation of the gradient.
 * @return resource GD image
 */
function create_gradient($start_color='ffffff',$end_color='000000',$size=100,$thickness=5,$orientation='')
{
	if ($orientation=="vertical") {
		$img=imagecreatetruecolor($thickness,$size);
	} else {
		$img=imagecreatetruecolor($size,$thickness);
	}
	
	$start_r  = hexdec(substr($start_color, 0, 2));
	$start_g  = hexdec(substr($start_color, 2, 2));
	$start_b  = hexdec(substr($start_color, 4, 2));
	
	$end_r  = hexdec(substr($end_color, 0, 2));
	$end_g = hexdec(substr($end_color, 2, 2));
	$end_b = hexdec(substr($end_color, 4, 2));
	
	for ($i=0;$i<$size;$i++) {
		$red = round($start_r - ($start_r-$end_r) * ($i / ($size-1)));
		$green = round($start_g - ($start_g-$end_g) * ($i / ($size-1)));
		$blue = round($start_b - ($start_b-$end_b) * ($i / ($size-1)));
		$color = imagecolorallocate($img, $red, $green, $blue);
		if ($orientation=="vertical") {
			for ($k=0;$k<$thickness;$k++)
				imagesetpixel($img, $k, $i, $color);
		} else {
			for ($k=0;$k<$thickness;$k++)
				imagesetpixel($img, $i, $k, $color);
		}
	}
	
	return  $img;
}
?>
	eXorithm – Execute Algorithm: View / Run Algorithm geonames_populations
function geonames_populations ($countries
{
  $data = file_get_contents"http://www.geonames.org/countryInfo?");
  
  if ($data==false) {
    throw new Exception"Data was not returned by geonames.org");
  } else {
      
    // parse the XML
    $obj = simplexml_load_string$data);
  
    // put countries in lower case
    for ($ii=0;$iicount$countries); $ii++) {
      $countries$ii]=strtolower$countries$ii]);
    }
  
    // get the population data for the matching countries
    $all_countries = $obj->country;
    $valuesarray();
    foreach ($all_countries as $country) {
      if (array_searchstrtolower$country->countryName), $countries)!==false) {
        $country_name = $country->countryName.''; 
        $country_pop = $country->population+0;
        $values$country_name] = $country_pop
      }
    }
    
    return $values
  }
} 
eXorithm – Execute Algorithm: View / Run Algorithm validate_url
function validate_url ($url
{
  $regex = '/^(https?|ftp)://'; //protocol
  $regex .= '(([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+'; //username
  $regex .= '(:([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+)?'; //password
  $regex .= '@)?'; //auth requires @
  $regex .= '((([a-z0-9][a-z0-9-]*[a-z0-9].)*'; //domain segments AND
  $regex .= '[a-z][a-z0-9-]*[a-z0-9]'; //top level domain  OR
  $regex .= '|((d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5]).){3}'
  $regex .= '(d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5])'; //IP address
  $regex .= ')(:d+)?'; //port
  $regex .= ')(((/+([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)*'; //path
  $regex .= '(?([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)'; //query string
  $regex .= '?)?)?'; //path and query string optional
  $regex .= '(#([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
  $regex .= '$/i'
  
  return (preg_match$regex, $url) ? true : false);
} 
eXorithm – Execute Algorithm: History For Algorithm stock_ticker
eXorithm – Execute Algorithm: View / Run Algorithm merge_sort
function merge_sort ($array
{
  if (count$array) <= 1)  
    return $array;  
  
  // sort each half
  $mid = floorcount$array)/2);
  $left = merge_sortarray_slice$array, 0, $mid));  
  $right = merge_sortarray_slice$array, $mid));  
  
  // merge the arrays
  $array = array();
  while (count$left)>0 && count$right)>0) {  
    if ($left[0] <= $right[0]) {  
      array_push$array, array_shift$left));  
    } else {  
      array_push$array, array_shift$right));  
    }  
  }  
  $array = array_merge$array, $left, $right);  
  
  return $array
} 
eXorithm – Execute Algorithm: View / Run Algorithm highlight
function highlight ($text, $phrase, $highlighter
{
  if (empty$phrase)) {
    return $text
  }
  
  if (is_array$phrase)) {
    $replace = array();
    $with = array();
    
    foreach ($phrase as $key => $value) {
      $key = $value
      $value = $highlighter
      $key = '([s])(' . $key . ')([s.,!?<])'
      $replace[] = '|' . $key . '|ix'
      $with[] = empty$value) ? $highlighter : $value
    }
    return preg_replace$replace, $with, $text);
  } else {
    $phrase = '([s])(' . $phrase . ')([s])'
    
    return preg_replace'|'$phrase'|i', $highlighter, $text);
  }
} 
eXorithm – Execute Algorithm: View / Run Algorithm lathe_shape
function lathe_shape ($equation, $start_x, $end_x, $detail, $degree_x, $degree_y, $degree_z, $face_color, $vertex_color, $scale, $image_size
{
  $vdist = 20;
  $dist = 20;
  
  $sides = lathe_polygons$equation, $start_x, $end_x, $detail);
  
  // project each of the polygons
  for ($i=0; $icount$sides); $i++) {
    $points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, ($end_x$start_x)/2, 0, 0, 20, 20, true);
  }
  
  // scale the image somewhat
  $scale = $image_size$scale/($end_x$start_x);
  
  return render_polygons$points, $vertex_color, $face_color, false, false, $image_size, $scale);
} 
	eXorithm – Execute Algorithm: View / Run Algorithm kmp_search
function kmp_search ($x, $y
{
  // see http://www-igm.univ-mlv.fr/~lecroq/string/node8.html
  
  // set-up phase
  $i = 0;
  $j = -1;
  
  $kmpNext = array();
  
  while ($i < $m) {
    while ($j > -1 && $x[i] != $x[j])
      $j = $kmpNext$j];
    $i++;
    $j++;
    if ($x$i] == $x$j])
      $kmpNext$i] = $kmpNext$j];
    else
      $kmpNext$i] = $j
  }
  
  // search phase
  $i = 0;
  $j = 0;
  $m = strlen$x);
  $n = strlen$y);
  
  $results = array();
  
  while ($j < $n) {
    while ($i > -1 && $x$i] != $y$j])
      $i = $kmpNext$i];
    $i++;
    $j++;
    if ($i >= $m) {
      $results[] = $j$i+1;
      $i = $kmpNext$i];
    }
  }
  
  return $results
} 
eXorithm – Execute Algorithm: Discuss Algorithm show_address
show_address    version 0.6    Given an address, show the location on a map.
There are no comments yet
New Comment
