function convert2grayscale ($image
{
$height = imagesy$image);
$width = imagesx$image);
$tc = imageistruecolor$image);
$new_image = imagecreatetruecolor$width, $height);
imagealphablending$new_image, false);
imagesavealpha$new_image, true);
for ($ii=0; $ii$width; $ii++) {
for ($jj=0; $jj$height; $jj++) {
$rgb = imagecolorat$image, $ii, $jj);
if ($tc) {
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb >> 24) & 0xFF;
} else {
$rgb = imagecolorsforindex$image, $rgb);
$r = $rgb'red'];
$g = $rgb'green'];
$b = $rgb'blue'];
$alpha = $rgb'alpha'];
}
$avg = round(($r$g$b)/3);
$color = imagecolorallocatealpha$new_image, $avg, $avg, $avg, $alpha);
imagesetpixel$new_image, $ii, $jj, $color);
}
}
return $new_image
}
Category: Algorithm
eXorithm – Execute Algorithm: Highest Rated Algorithms
eXorithm – Execute Algorithm: Algorithms Beginning with K
PI Digits
<?php /** * pi_digits * * Calculate the digits of pi. * * @version 0.4 * @author Contributors at eXorithm * @link /algorithm/view/pi_digits Listing at eXorithm * @link /algorithm/history/pi_digits History at eXorithm * @license /home/show/license * * @param number $digits * @return mixed */ function pi_digits($digits=500) { $n = floor($digits * 14/4); $scale = 10000; $init = 2000; $carry = 0; $result = ''; for($i=0;$i<=$n;$i++) { $arr[$i] = $init; } for($i=$n;$i>0;$i=$i-14) { $sum = 0; for($j=$i;$j>0;$j--) { $sum = ($sum * $j) + ($scale * $arr[$j]); $arr[$j] = $sum % (($j*2)-1); $sum = floor($sum / (($j*2)-1)); } $result .= sprintf("%04d", ($carry + ($sum / $scale))); $carry = $sum % $scale; } if ($digits>1) { return $result[0].'.'.substr($result,1,$digits-1); } else { return substr($result,0,$digits); } } ?>
eXorithm – Execute Algorithm: View / Run Algorithm rot13
function rot13 ($string
{
for$i=0;$istrlen$string);$i++) {
$jord$string$i]);
if ((($j>=ord"n")) & ($j<=ord"z"))) | ($j>=ord"N")) & ($j<=ord"Z"))) {
$j$j-13;
}
elseif ((($j>=ord"a")) & ($j<=ord"m"))) | ($j>=ord"A")) & ($j<=ord"M"))) {
$j$j+13;
}
$new.=chr$j);
}
return$new);
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_sierpinski
function draw_sierpinski ($size, $color
{
// see http://en.wikipedia.org/wiki/Sierpinski_triangle
// this code from http://php.net/manual/en/function.imagesetpixel.php
$hght = roundsqrt$size$size - ($size/2)*($size/2)));
$diff = round(($size - $hght)/2);
$gd = imagecreatetruecolor$size, $size);
$corners[0] = array'x' => round$size/2), 'y' => $diff);
$corners[1] = array'x' => 0, 'y' => $size$diff);
$corners[2] = array'x' => $size, 'y' => $size$diff);
$red = hexdecsubstr$color, 0, 2));
$green = hexdecsubstr$color, 2, 2));
$blue = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$gd, $red, $green, $blue);
$x = $size
$y = $size
for ($i = 0; $i < 200000; $i++) {
imagesetpixel$gd, round$x),round$y), $color);
$a = rand(0, 2);
$x = ($x + $corners$a]['x']) / 2;
$y = ($y + $corners$a]['y']) / 2;
}
return $gd
}
eXorithm – Execute Algorithm: Algorithms Beginning with L
eXorithm – Execute Algorithm: View / Run Algorithm number2roman
function number2roman ($num
{
// http://www.go4expert.com/forums/showthread.php?t=4948
// Make sure that we only use the integer portion of the value
$n = intval$num);
$result = ''
// Declare a lookup array that we will use to traverse the number:
$lookup = array'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
foreach ($lookup as $roman => $value)
{
// Determine the number of matches
$matches = intval$n / $value);
// Store that many characters
$result .= str_repeat$roman, $matches);
// Substract that from the number
$n = $n % $value
}
// The Roman numeral should be built, return it
return $result
}
eXorithm – Execute Algorithm: View / Run Algorithm skew_image
function skew_image ($image, $skew_value
{
// note: this function was original posted by etienne on php.net.
$width = imagesx$image);
$height = imagesy$image);
$height2 = $height + ($width * $skew_value);
$imgdest = image_create_alpha$width, $height2);
// Process the image
for$x = 0, $level = 0; $x < $width - 1; $x++)
{
$floor = floor$level);
// To go faster, some lines are being copied at once
if ($level == $floor
imagecopy$imgdest, $image, $x, $level, $x, 0, 1, $height - 1);
else
{
$temp = $level - $floor
// The first pixel of the line
// We get the color then apply a fade on it depending on the level
$color1 = imagecolorsforindex$image, imagecolorat$img, $x, 0));
$alpha = $color1'alpha'] + ($temp * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha$imgdest, $color1'red'], $color1'green'], $color1'blue'], $alpha);
imagesetpixel$imgdest, $x, $floor, $color);
}
// The rest of the line
for$y = 1; $y < $height - 1; $y++)
{
// Merge this pixel and the upper one
$color2 = imagecolorsforindex$image, imagecolorat$image, $x, $y));
$alpha = ($color1'alpha'] * $temp) + ($color2'alpha'] * (1 - $temp));
if ($alpha < 127)
{
$red = ($color1'red'] * $temp) + ($color2'red'] * (1 - $temp));
$green = ($color1'green'] * $temp) + ($color2'green'] * (1 - $temp));
$blue = ($color1'blue'] * $temp) + ($color2'blue'] * (1 - $temp));
$color = imagecolorallocatealpha$imgdest, $red, $green, $blue, $alpha);
imagesetpixel$imgdest, $x, $floor + $y, $color);
}
$color1 = $color2
}
// The last pixel of the line
$color1 = imagecolorsforindex$image, imagecolorat$image, $x, $height - 1));
$alpha = $color1'alpha'] + ((1 - $temp) * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha$imgdest, $color1'red'], $color1'green'], $color1'blue'], $alpha);
imagesetpixel$imgdest, $x, $floor + $height - 1, $color);
}
}
// The line is finished, the next line will be lower
$level += $skew_value
}
// Finished processing, return the skewed image
return $imgdest
}