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

stock_ticker     version 3.2     Generate an HTML stock ticker with current quotes.
Version Note Created Diff
3.2 [edit Reverted from version 2.5 Dec 1, 2016 09:38 am by Mike Campbell
3.1 [revert Reverted from version 2.1 Feb 2, 2016 02:20 am by moneymunch
3.0 [revert Feb 2, 2016 02:19 am by moneymunch
2.6 [revert Feb 2, 2016 02:16 am by moneymunch
2.5 [revert Reverted from version 2.2 Feb 27, 2015 04:02 pm by Mike Campbell
2.4 [revert Jan 15, 2015 04:22 am by tela34
2.3 [revert Oct 20, 2014 09:09 pm by gendon
2.2 [revert Aug 29, 2014 01:17 am by Mike Campbell
2.1 [revert Aug 27, 2014 11:46 am by stocks
2.0 [revert Aug 27, 2014 11:44 am by stocks
1.23 [revert Aug 27, 2014 11:00 am by stocks
1.22 [revert Reverted from version 1.15 Jul 21, 2014 11:47 am by Mike Campbell
1.21 [revert Jul 6, 2014 12:47 am by liveticker
1.20 [revert Jul 6, 2014 12:46 am by liveticker
1.19 [revert Jul 6, 2014 12:44 am by liveticker
1.18 [revert Jul 5, 2014 02:05 am by liveticker
1.17 [revert Jul 5, 2014 12:50 am by liveticker
1.16 [revert Jul 5, 2014 12:48 am by liveticker
1.15 [revert Dec 1, 2012 12:47 am by Mike Campbell
1.14 [revert Nov 27, 2012 11:59 am by harrison7042

    

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