function google_translate ($text, $from, $to
{
$transfile_get_contents"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="urlencode$text)."&langpair="urlencode$from)."|"urlencode$to));
$json = json_decode$trans);
if (!isset$json->responseStatus) || ($json->responseStatus!=200)) {
throw new Exception"Translation could not be retreived for '$text'");
}
return $json->responseData->translatedText;
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm image_callback
function image_callback ($image, $operation
{
/* perform a series of transformation on an image */
switch ($operation) {
case "flip"
$image = flip_image$image, "vertical");
break
case "skew"
$image = skew_image$image, 0.5);
break
case "invert"
$image = invert_image$image);
break
}
$ret = array();
$ret"return"] = $image
$ret"handler"] = HANDLER_IMAGE;
$ret"arguments"] = array$image);
return $ret
}
eXorithm – Execute Algorithm: View / Run Algorithm hello_world
function hello_world ()
{
return "Hello World"
}
eXorithm – Execute Algorithm: View / Run Algorithm stock_ticker
function stock_ticker ($symbols, $background_color, $stock_color, $price_color, $up_color, $down_color, $speed
{
sort$symbols);
if ($background_color==$stock_color) {
// something's wrong, colors weren't specified
$background_color = invert_color$background_color);
}
$return = '<div align="center">
<marquee bgcolor="#'$background_color'" loop="20" scrollamount="'$speed'">'
foreach ($symbols as $symbol) {
$data = file_get_contents"http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv");
$values = explode",", $data);
$lasttrade = $values[1];
$change = $values[4];
$return .= "<span style="color:#$stock_color">$symbol</span> "
$return .= "<span style="color:#$price_color">$lasttrade</span> "
if ($change<0)
$return .= "<span style="color:#$down_color">$change</span> "
else
$return .= "<span style="color:#$up_color">$change</span> "
$return .= " "
}
$return .= '</marque>
</div>'
return $return
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validate
function isbn_validate ($isbn, $type
{
$okay = false;
$isbn = str_replacearray'-'' '), '', $isbn);
// check ISBN 13
if ($type!=2)
{
if (strlen$isbn)==13)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<12;$i++)
{
if (($i%2)==1)
$weight=3;
else
$weight=1;
if (is_numeric$isbn$i]))
$sum += $weight$isbn$i];
else
$charsokay=false;
}
if ($charsokay
if ($sum>0)
if ((10-($sum % 10)) % 10==$isbn[12])
$okay = true;
}
}
// check ISBN 10
if ($type!=1)
{
if (strlen$isbn)==10)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<10;$i++)
{
if (is_numeric$isbn$i]))
$sum += ($i+1)*$isbn$i];
else
{
if ((strtolower$isbn$i])=='x') && ($i==9))
$sum += 100;
else
$charsokay=false;
}
}
if ($charsokay
if ($sum>0)
if (($sum % 11)==0)
$okay = true;
}
}
return $okay
}
eXorithm – Execute Algorithm: View / Run Algorithm shorten_url
function shorten_url ($url, $limit, $dotdotdot
{
$length = strlen$url);
if ($length > $limit) {
$s = substr$url, 0, floor(2*$limit/3));
$e = substr$url, -1*floor$limit/3));
return $s$dotdotdot$e
} else {
return $url
}
}
eXorithm – Execute Algorithm: View / Run Algorithm antialias_pixel
function antialias_pixel ($image, $x, $y, $color, $weight
{
$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 draw_playing_card
function draw_playing_card ($suit, $value, $face_down, $size, $x, $y
{
//based in part on http://www.brainjar.com/css/cards/
switch ($suit) {
case 'diamonds'
$color = 'red'
$symbol = '♦'
break
case 'hearts'
$color = 'red'
$symbol = '♥'
break
case 'clubs'
$color = 'black'
$symbol = '♣'
break
case 'spades'
$color = 'black'
$symbol = '♠'
break
default
return ''
}
if ($face_down) {
$bgcolor = "brown"
} else {
$bgcolor = "#fffff2"
}
// card container
$card = "<div style=""
"border:1px solid black;"
"background-color:$bgcolor;"
"position:absolute;"
"font-size:{$size}px;"
"font-family:Arial,sans-serif;"
"width:3.7em;"
"height:5em;"
"border-radius:.2em;"
"-moz-border-radius:.2em;"
"-webkit-border-radius:.2em;"
"color:$color;"
"left:{$x}px;"
"top:{$y}px;"
"">"
if (!$face_down) {
// number & symbol in top corner
$card .= "<div style=""
"position:absolute;"
"font-size:50%;"
"font-weight:bold;"
"text-align:center;"
"left:0.25em;"
"top:0.25em;"
"">$value<br />$symbol</div>"
// non-face card
if (($value>=2) && ($value<=10)) {
/****** column 1 symbols *****/
if (($value>=4) && ($value<=10)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:0.6em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:0.6em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==9) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:0.6em;"
"top:1.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 4
"position:absolute;"
"left:0.6em;"
"top:2.5em;"
"">$symbol</div>"
}
if (($value==6) || ($value==7) || ($value==8)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:0.6em;"
"top:2em;"
"">$symbol</div>"
}
/****** column 2 symbols *****/
if (($value==2) || ($value==3)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:1.55em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:1.55em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==7) || ($value==8) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:1.55em;"
"top:1em;"
"">$symbol</div>"
if ($value!=7) {
$card .= "<div style="". //row 4
"position:absolute;"
"left:1.55em;"
"top:3em;"
"">$symbol</div>"
}
}
if (($value==3) || ($value==5) || ($value==9)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:1.55em;"
"top:2em;"
"">$symbol</div>"
}
/****** column 3 symbols *****/
if (($value>=4) && ($value<=10)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:2.5em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:2.5em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==9) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:2.5em;"
"top:1.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 4
"position:absolute;"
"left:2.5em;"
"top:2.5em;"
"">$symbol</div>"
}
if (($value==6) || ($value==7) || ($value==8)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:2.5em;"
"top:2em;"
"">$symbol</div>"
}
}
// ace
else if ($value=='A') {
$card .= "<div style=""
"font-size:300%;"
"position:absolute;"
"text-align:center;"
"width:100%;"
"top:0.25em;"
"">$symbol</div>"
}
// jack, king, queen
else if (($value=='J')|| ($value=='Q')|| ($value=='K')) {
if ($value=='J') $sym'♞'
else if ($value=='Q') $sym'♛'
else if ($value=='K') $sym'♚'
$card .= "<div style=""
"border:1px solid black;"
"position:absolute;"
"left:0.6em;"
"top:0.55em;"
"width:2.45em;"
"height:3.85em;"
"">"
$card .= "<div style=""
"font-size:300%;"
"position:absolute;"
"text-align:center;"
"width:100%;"
"">$sym</div>"
$card .= "<div style=""
"position:absolute;"
"left:0.05em;"
"top:0em;"
"">$symbol</div>"
$card .= "<div style=""
"position:absolute;"
"right:0.05em;"
"bottom:0em;"
"">$symbol</div>"
$card .= "</div>"
}
}
$card .= "</div>"
return $card
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validator
function isbn_validator ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric or X chars
$casestrlen$isbn);
switch ($case
{
case 10:
if(!isbn_validate$isbn, 2))
return 0; //Failure - not valid ISBN
else
$isbnconv_isbn$isbn);
break
case 13:
$isbnpreg_replace'/[^d]/'''$isbn); //remove all non-numeric chars
if(!isbn_validate$isbn, 1))
return 0; //Failure - not valid ISBN
break
default
return 0;
}
$isbnisbn_hyphenate$isbn);
return $isbn
}
eXorithm – Execute Algorithm: View / Run Algorithm unique_image
function unique_image ($string
{
$size=200;
$steps=5;
$step$size$steps
$image = image_create_alpha$size, $size);
$n = 0;
$prev = 0;
$len = strlen$string);
$sum = 0;
for ($i=0;$i$len$i++) $sum += ord$string$i]);
for ($i=0;$i$steps$i++) {
for ($j=0;$j$steps$j++) {
$letter = $string$n++ % $len];
$u = ($n % (ord$letter)+$sum)) + ($prev % (ord$letter)+$len)) + (($sum-1) % ord$letter));
$color = imagecolorallocate$image, pow$u$prev$u$prev+5,2)%256, pow$u$prev$u$prev+3,2)%256, pow$u$prev$u$prev+1,2)%256);
if (($u%2)==0)
imagefilledpolygon$image, array$i$step, $j$step, $i$step$step, $j$step, $i$step, $j$step$step), 3, $color);
$prev = $u
$u = ($n % (ord$letter)+$len)) + ($prev % (ord$letter)+$sum)) + (($sum-1) % ord$letter));
if (($u%2)==0)
imagefilledpolygon$image, array$i$step, $j$step$step, $i$step$step, $j$step$step, $i$step$step, $j$step), 3, $color);
$prev = $u
}
}
return $image
}