function pixelate ($image, $blocksize
{
// Based on http://www.tuxradar.com/practicalphp/11/2/24
$imagex = imagesx$image);
$imagey = imagesy$image);
$image2 = imagecreatetruecolor$imagex, $imagey);
imagesavealpha$image2, true);
imagealphablending$image2, false);
if ($blocksize<1) {
$blocksize = 1;
}
for ($x = 0; $x < $imagex; $x += $blocksize) {
for ($y = 0; $y < $imagey; $y += $blocksize) {
// get the pixel colour at the top-left of the square
$colour = imagecolorat$image, $x, $y);
// set the new red, green, blue and alpha values to 0
$newr = 0;
$newg = 0;
$newb = 0;
$newt = 0;
// create an empty array for the colours
$colours = array();
// cycle through each pixel in the block
for ($k = $x; $k < $x + $blocksize; ++$k) {
for ($l = $y; $l < $y + $blocksize; ++$l) {
// if we are outside the valid bounds of the image, use a safe colour
if ($k < 0) { $colours[] = $colour; continue; }
if ($k >= $imagex) { $colours[] = $colour; continue; }
if ($l < 0) { $colours[] = $colour; continue; }
if ($l >= $imagey) { $colours[] = $colour; continue; }
// if not outside the image bounds, get the colour at this pixel
$colours[] = imagecolorat$image, $k, $l);
}
}
// cycle through all the colours we can use for sampling
foreach$colours as $colour) {
$colour = imagecolorsforindex$image, $colour);
// add their red, green, and blue values to our master numbers
$newr += $colour'red'];
$newg += $colour'green'];
$newb += $colour'blue'];
$newt += $colour'alpha'];
}
// now divide the master numbers by the number of valid samples to get an average
$numelements = count$colours);
$newr /= $numelements
$newg /= $numelements
$newb /= $numelements
$newt /= $numelements
// and use the new numbers as our colour
$newcol = imagecolorallocatealpha$image2, $newr, $newg, $newb, $newt);
imagefilledrectangle$image2, $x, $y, $x + $blocksize - 1, $y + $blocksize - 1, $newcol);
}
}
return $image2
}
Tag: Volume
eXorithm – Execute Algorithm: View / Run Algorithm quick_sort
function quick_sort ($array
{
if (count$array)<=1) {
return $array
} else {
$pivot = $array[0];
$lesser = array();
$greater = array();
for ($i=1;$icount$array);$i++) {
if ($array$i]<=$pivot) {
$lesser[] = $array$i];
} else {
$greater[] = $array$i];
}
}
return array_mergequick_sort$lesser), array$pivot), quick_sort$greater));
}
}
eXorithm – Execute Algorithm: View / Run Algorithm random_name_caption
function random_name_caption ($image, $your_name
{
$sizey = imagesy$image);
$sizex = imagesx$image);
if (mt_rand(0,1)==1)
$color = imagecolorallocate$image, 254, 0, 0);
else
$color = imagecolorallocate$image, 0, 0, 254);
$y = mt_rand(0,$sizey-10);
$x = mt_rand(0,$sizex-10);
imagestring$image, 4, $x, $y, $your_name, $color);
$return = array
'return' => $image
'arguments' => array$image, $your_name
);
return $return
}
eXorithm – Execute Algorithm: View / Run Algorithm random_usort
function random_usort ($a, $b
{
return mt_rand(-1,1);
}
eXorithm – Execute Algorithm: View / Run Algorithm render_polygons
function render_polygons ($polygons, $vertex_color, $face_color, $wireframe, $dashes, $image_size, $scale
{
foreach ($polygons as $polygon) {
if (!is_array$polygon)) {
throw new Exception'Each polygon must be a list.');
} else if ((count$polygon)%3)!=0) {
throw new Exception'Each polygon must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
}
}
if (is_array$vertex_color)) {
if (count$vertex_color) != count$polygons)) {
throw new Exception'If vertex colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
if (is_array$face_color)) {
if (count$face_color) != count$polygons)) {
throw new Exception'If face colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
// if scale=0 then we auto-scale
if ($scale==0) {
$max = 0;
for ($i=0; $icount$polygons); $i++) {
for ($j=0; $jcount$polygons$i]); $j$j+3) {
if (abs$polygons$i][$j])>$max
$max = abs$polygons$i][$j]);
if (abs$polygons$i][$j+1])>$max
$max = abs$polygons$i][$j+1]);
}
}
if ($max>0)
$scale = ($image_size-2)/($max*2);
}
// the polygon arrays (x,y,z) must be converted into shapes (x,y)
$shapes = array();
$z_max = array();
for ($i=0; $icount$polygons); $i++) {
$max = $polygons$i][2];
for ($j=0; $jcount$polygons$i]); $j$j+3) {
$x = $polygons$i][$j];
$y = $polygons$i][$j+1];
// map each x,y coord to a screen position
$x = round$image_size/2 + $x$scale);
$y = round$image_size/2 - $y$scale);
$shapes$i][$j] = $x
$shapes$i][$j+1] = $y
// keep track of the maximum z-value for each shape
if ($polygons$i][$j+2]>$max
$max = $polygons$i][$j+2];
}
$shapes$i] = array_values$shapes$i]);
$z_max$i] = $max
}
// create a blank image
$image = image_create_alpha$image_size, $image_size);
// create the colors
if (!is_array$vertex_color))
$vertex_color = array_fill(0, count$polygons), $vertex_color);
if (!is_array$face_color))
$face_color = array_fill(0, count$polygons), $face_color);
// painter's algorithm - draw farther polygons first
array_multisort$z_max, SORT_DESC, $shapes, $face_color, $vertex_color);
// draw the polygons
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$f_color = allocate_color$image, $face_color$i]);
if (!$wireframe) {
imagefilledpolygon$image, $shapes$i], count$shapes$i])/2, $f_color);
}
imagepolygon$image, $shapes$i], count$shapes$i])/2, $v_color);
}
// draw dashes - BUGGY
if ($dashes) {
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$style = array$v_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle$image, $style);
imagepolygon$image, $shapes$i], count$shapes$i])/2, IMG_COLOR_STYLED);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm prime_factors
function prime_factors ($number
{
$factors = array();
while$number > 1)
{
$prime = 2;
while$number % $prime != 0)
{
$prime = next_prime$prime);
}
if$prime > floor$number/2))
{
$factors[] = $number
break
}
$factors[] = $prime
$number = $number$prime
}
return $factors
}
eXorithm – Execute Algorithm: View / Run Algorithm pie_chart
function pie_chart ($data
{
$size=200;
$centerx = 100;
$centery = 100;
$circle_diameter = 180;
$image = image_create_alpha$size, $size);
$blackimagecolorallocatealpha$image, 0, 0, 0, 0);
$total = array_sum$data);
$n = 0;
$start = 0;
foreach ($data as $key => $value) {
$length = 360 * $value$total
$color = allocate_color$image, generate_graph_color$n++));
imagefilledarc$image, $centerx, $centery, $circle_diameter, $circle_diameter, round$start), round$start$length), $color, IMG_ARC_PIE);
$start += $length
}
imageellipse$image, $centerx, $centery, $circle_diameter, $circle_diameter, $black);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm overlay_image
function overlay_image ($base, $image
{
$h = imagesy$image);
$w = imagesx$image);
imagealphablending$base, true);
imagesavealpha$base, true);
imagecopy$base, $image, 0, 0, 0, 0, $w, $h);
return $base
}
eXorithm – Execute Algorithm: View / Run Algorithm photobucket
function photobucket ($url
{
$str = file_get_contents$url);
preg_match_all'/<img[^>]+>/i'$str, $result);
$strPics = ""
foreach( $result as $img_tag) {
foreach( $img_tag as $img) {
if( !strpos$img, 'class="under off"') ) continue
preg_match'/< *img[^>]*src *= *["']?([^"']*)/i', $img, $imgURLs);
$imgURL = str_replace"/th_", "/", $imgURLs[1]);
$strPics .= $imgURL . "n"
}
}
return $strPics
}
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"
}