#!/usr/bin/php5 -q 
<?php

//// IMG_SRC  ////////////////////////////// ///// //// /// // /
/// the image source image source maker /// ///// //// /// // /
// bot ID #5 ///////////////////////////// ///// //// /// // /

ini_set('memory_limit''32M');

require_once(
'global_vars_backend.php');
ini_set('user_agent'$img_src_user_agent);
$mode $_SERVER['argv'][1];


echo 
"
 _____________  ___________   _____________________________
_____  _/__   |/  /_  ____/  ___  ___/__  __ \_  ____/__  /
  __  / __  /|_/ /_  / __      ____ \__  /_/ /  /    __  / 
 __/ /  _  /  / / / /_/ /     ____/ /_  _, _// /___   /_/  
 /___/  /_/  /_/  \____/______/____/ /_/ |_| \____/  (_)   
                       _/_____/                            

  IMG_SRC is the main program of sourcerer 1.0 the IMG SRC ORG
  indexing program. it analyses images and makes them visually 
  available for the image-based image-search and is thus essential
  part of the image-source.org search engine
  bot ID #5, the standard timer is set to " 
$timer ." seconds
  IN from table ImagesConfirmed, OUT to ImagesSaved,
  ImagesRValuesBig/Small, ImagesGValuesBig/Small, ImagesBValuesBig/Small,
  ImagesBrightnessValuesBig/Small, (ImagesTraceValuesBig/Small),
  ImagesSymmetryValues, ImagesAmountValues
  IMG_SRC also creates thumbnails
  __________________________________________________
"
;

// Special Function Definitions

// --

// Main function to loop


function Cycle ($timer$img_src_timer$img_src_timer_pause$img_src_select_limit,
$img_src_thumb_size,
$mode)
{

    
$time time();
    
    
$connect mysql_connect ('localhost''imgsrc1''******');
    
$database_select mysql_select_db ('imagesource'$connect);

    
// read from IN table
    /////////////////////////////////////////////////////////////////////////////
     
$query 'SELECT id FROM ImagesConfirmed;';
    
$query mysql_query($query);
    
$rows_number mysql_affected_rows();
     
    
$query 'SELECT id, url, timestamp, updated, width, height, aspect, size, alt, longdesc, leftside, rightside
         FROM ImagesConfirmed ORDER BY timestamp ASC LIMIT 0, ' 
$img_src_select_limit ';';
    
$query mysql_query($query);
    
$query_result mysql_fetch_assoc($query);

    
$site_id $query_result['id'];
    
//$image_id = $query_result['id'];
    
$url $query_result['url'];
    
$timestamp $query_result['timestamp'];
    
$updated $query_result['updated'];

    
$width $query_result['width'];
    
$height $query_result['height'];
    
$aspect $query_result['aspect'];
    
$size $query_result['size'];
    
$alt $query_result['alt'];
    
$longdesc $query_result['longdesc'];

    
$leftside $query_result['leftside'];
    
$rightside $query_result['rightside'];

    
// get potential image id
    /////////////////////////////////////////////////////////////////////////////
    
$query 'SELECT id FROM ImagesSaved;';
    
$query mysql_query($query);

    
$image_id mysql_insert_id() +1;


    
/////////////////////////////////////////////////////////////////////////////
    // processing data
    /////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////

    
$write_out TRUE;
    
$delete_in TRUE;

    if ( 
$rows_number == ) { $write_out FALSE; }

    if ( isset(
$url) && isset($image_id) && $write_out == TRUE ) {

        
// create thumbnails
        ////////////////////////////////////////////////////////////////////////
        // now created in line 1094

        // create small 16x16 and 8x8 pics
        ////////////////////////////////////////////////////////////////////////
        
$resize_big shell_exec"convert " $url " -resize 16x16! /var/www/is/public/temp/16_by_16/" $image_id ".jpg" );
        
$resize_small shell_exec"convert " $url " -resize 8x8! /var/www/is/public/temp/8_by_8/" $image_id ".jpg" );

        
$resize_big_txt shell_exec"convert " $url " -resize 16x16! /var/www/is/public/temp/16_by_16/" $image_id ".txt" );
        
$resize_small_txt shell_exec"convert " $url " -resize 8x8! /var/www/is/public/temp/8_by_8/" $image_id ".txt" );


        
//$resize_monochrome = shell_exec( "convert /var/www/is/public/temp/16_by_16/" . $image_id .
        //    ".jpg -monochrome /var/www/is/public/temp/bright/test.jpg");
        //echo ">>>" . $output = shell_exec( "identify -verbose %format " . $url );


        
$matches preg_split("/\,/i"$output0PREG_SPLIT_NO_EMPTY );




        
// read RGB, brightness + hex values into array
        ////////////////////////////////////////////////////////////////////////

        // big values

        
$value_file_big "/var/www/is/public/temp/16_by_16/" $image_id ".txt";
        
$value_file_big_open fopen($value_file_big,"r");

        if (
$value_file_big_open) {
            while ( !
feof($value_file_big_open) ) {
                
$line fgets($value_file_big_open);
                
$match preg_match("/^(\d{1,2}),(\d{1,2})\: \(([\d\s]{1,3}),([\d\s]{1,3}),([\d\s]{1,3})\) (.*)/i"$line$line_matches);

                if ( 
$match == ) {
                    
$values_big[$line_matches[1]+1][$line_matches[2]+1][r] = $line_matches[3];
                    
$values_big[$line_matches[1]+1][$line_matches[2]+1][g] = $line_matches[4];
                    
$values_big[$line_matches[1]+1][$line_matches[2]+1][b] = $line_matches[5];

                    
$values_big[$line_matches[1]+1][$line_matches[2]+1][hex] = $line_matches[6];

                    
// calculate brightness
                    
$values_big[$line_matches[1]+1][$line_matches[2]+1][brightness]
                         = 
round ( ($line_matches[3]*0.3 $line_matches[4]*0.59 $line_matches[5]*0.11) );
                }
            }
        }
        
fclose($value_file_big_open);

        
// small values (used for most calculations)

        
$value_file_small "/var/www/is/public/temp/8_by_8/" $image_id ".txt";
        
$value_file_small_open fopen($value_file_small,"r");

        if (
$value_file_small_open) {
            while ( !
feof($value_file_small_open) ) {
                
$line fgets($value_file_small_open);
                
$match preg_match("/^(\d),(\d)\: \(([\d\s]{1,3}),([\d\s]{1,3}),([\d\s]{1,3})\) (.*)/i"$line$line_matches);

                if ( 
$match == ) {
                    
$values_small[$line_matches[1]+1][$line_matches[2]+1][r] = $line_matches[3];
                    
$values_small[$line_matches[1]+1][$line_matches[2]+1][g] = $line_matches[4];
                    
$values_small[$line_matches[1]+1][$line_matches[2]+1][b] = $line_matches[5];

                    
$values_small[$line_matches[1]+1][$line_matches[2]+1][hex] = '#' $line_matches[6];

                    
// calculate brightness
                    
$values_small[$line_matches[1]+1][$line_matches[2]+1][brightness]
                         = 
round ( ($line_matches[3]*0.3 $line_matches[4]*0.59 $line_matches[5]*0.11) );
                }
            }
        }
        
fclose($value_file_small_open);


        
// algorithms, analysis
        ////////////////////////////////////////////////////////////////////////
        // remember non-columns mode - reading from left to right from top to bottom instead!

        // color areas, areas and groups by grid 16
        // BIG

        
$tolerance_r 23;
        
$tolerance_g 23;
        
$tolerance_b 23;

        
$area 1;
        
$group 1;

        
//echo "\n";

        
for ($x 1$x <= 16$x++)
        {
            for (
$y 1$y <= 16$y++)
            {
                
$neighbor[$x][$y][self_r] = $values_big[$x][$y][r];
                
$neighbor[$x][$y][self_g] = $values_big[$x][$y][g];
                
$neighbor[$x][$y][self_b] = $values_big[$x][$y][b];

                
$neighbor[$x][$y][top_r] = $values_big[$x][$y-1][r];
                
$neighbor[$x][$y][top_g] = $values_big[$x][$y-1][g];
                
$neighbor[$x][$y][top_b] = $values_big[$x][$y-1][b];

                
$neighbor[$x][$y][left_r] = $values_big[$x-1][$y][r];
                
$neighbor[$x][$y][left_g] = $values_big[$x-1][$y][g];
                
$neighbor[$x][$y][left_b] = $values_big[$x-1][$y][b];

                
$neighbor[$x][$y][right_r] = $values_big[$x+1][$y][r];
                
$neighbor[$x][$y][right_g] = $values_big[$x+1][$y][g];
                
$neighbor[$x][$y][right_b] = $values_big[$x+1][$y][b];

                
$neighbor[$x][$y][bottom_r] = $values_big[$x][$y+1][r];
                
$neighbor[$x][$y][bottom_g] = $values_big[$x][$y+1][g];
                
$neighbor[$x][$y][bottom_b] = $values_big[$x][$y+1][b];


                if ( 
$neighbor[$x][$y][left_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][left_r] > $neighbor[$x][$y][self_r] - $tolerance_r
                
                
&& $neighbor[$x][$y][left_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][left_g] > $neighbor[$x][$y][self_g] - $tolerance_g
 
                
&& $neighbor[$x][$y][left_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][left_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes[$area][b][min] ) {
                        
$group_extremes[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][min] = $group_extremes[$area][r][min];
                        
$group_extremes[$area][g][min] = $group_extremes[$area][g][min];
                        
$group_extremes[$area][b][min] = $group_extremes[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes[$area][b][max] ) {
                        
$group_extremes[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                    for ( 
$a count($group_extremes); $a >= 1$a-- ) {

                        if ( 
$values_big[$x-1][$y][r] < $group_extremes[$a][r][max]
                        && 
$values_big[$x-1][$y][r] > $group_extremes[$a][r][min]
                        && 
$values_big[$x-1][$y][g] < $group_extremes[$a][g][max]
                        && 
$values_big[$x-1][$y][g] > $group_extremes[$a][g][min]
                        && 
$values_big[$x-1][$y][b] < $group_extremes[$a][b][max]
                        && 
$values_big[$x-1][$y][b] > $group_extremes[$a][b][min] ) {
                            
$area_exists $a;

                        }
                    }

                    if (
$neighbor[$x-1][$y][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x-1][$y][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x-1][$y][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x-1][$y][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][top_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][top_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][top_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][top_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][top_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][top_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes[$area][b][min] ) {
                        
$group_extremes[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][min] = $group_extremes[$area][r][min];
                        
$group_extremes[$area][g][min] = $group_extremes[$area][g][min];
                        
$group_extremes[$area][b][min] = $group_extremes[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes[$area][b][max] ) {
                        
$group_extremes[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                    for ( 
$a count($group_extremes); $a >= 1$a-- ) {

                        if ( 
$values_big[$x][$y-1][r] < $group_extremes[$a][r][max]
                        && 
$values_big[$x][$y-1][r] > $group_extremes[$a][r][min]
                        && 
$values_big[$x][$y-1][g] < $group_extremes[$a][g][max]
                        && 
$values_big[$x][$y-1][g] > $group_extremes[$a][g][min]
                        && 
$values_big[$x][$y-1][b] < $group_extremes[$a][b][max]
                        && 
$values_big[$x][$y-1][b] > $group_extremes[$a][b][min] ) {
                            
$area_exists $a;
                        }
                    }

                    if (
$neighbor[$x][$y-1][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x][$y-1][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x][$y-1][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x][$y-1][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][right_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][right_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][right_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][right_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][right_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][right_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes[$area][b][min] ) {
                        
$group_extremes[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][min] = $group_extremes[$area][r][min];
                        
$group_extremes[$area][g][min] = $group_extremes[$area][g][min];
                        
$group_extremes[$area][b][min] = $group_extremes[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes[$area][b][max] ) {
                        
$group_extremes[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                    for ( 
$a count($group_extremes); $a >= 1$a-- ) {

                        if ( 
$values_big[$x+1][$y][r] < $group_extremes[$a][r][max]
                        && 
$values_big[$x+1][$y][r] > $group_extremes[$a][r][min]
                        && 
$values_big[$x+1][$y][g] < $group_extremes[$a][g][max]
                        && 
$values_big[$x+1][$y][g] > $group_extremes[$a][g][min]
                        && 
$values_big[$x+1][$y][b] < $group_extremes[$a][b][max]
                        && 
$values_big[$x+1][$y][b] > $group_extremes[$a][b][min] ) {
                            
$area_exists $a;

                        }
                    }

                    if (
$neighbor[$x+1][$y][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x+1][$y][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x+1][$y][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x+1][$y][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][bottom_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][bottom_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][bottom_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][bottom_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][bottom_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][bottom_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes[$area][b][min] ) {
                        
$group_extremes[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][min] = $group_extremes[$area][r][min];
                        
$group_extremes[$area][g][min] = $group_extremes[$area][g][min];
                        
$group_extremes[$area][b][min] = $group_extremes[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes[$area][b][max] ) {
                        
$group_extremes[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                    for ( 
$a count($group_extremes); $a >= 1$a-- ) {

                        if ( 
$values_big[$x-1][$y][r] < $group_extremes[$a][r][max]
                        && 
$values_big[$x][$y+1][r] > $group_extremes[$a][r][min]
                        && 
$values_big[$x][$y+1][g] < $group_extremes[$a][g][max]
                        && 
$values_big[$x][$y+1][g] > $group_extremes[$a][g][min]
                        && 
$values_big[$x][$y+1][b] < $group_extremes[$a][b][max]
                        && 
$values_big[$x][$y+1][b] > $group_extremes[$a][b][min] ) {
                            
$area_exists $a;

                        }
                    }

                    if (
$neighbor[$x][$y+1][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x][$y+1][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x][$y+1][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x][$y+1][area]]++;
                    }

                } else {
                    
                    
$neighbor[$x][$y][area] = $area;
                    
$neighbor[$x][$y][group] = $area;
                    
$area++;
                    
$areas[$area]++;

                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes[$area][b][min] ) {
                        
$group_extremes[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][min] = $group_extremes[$area][r][min];
                        
$group_extremes[$area][g][min] = $group_extremes[$area][g][min];
                        
$group_extremes[$area][b][min] = $group_extremes[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes[$area][r][max]
                    && 
$neighbor[$x][$y][self_g] > $group_extremes[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes[$area][b][max]) {
                        
$group_extremes[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                }
            }
        }

        for (
$y 1$y <= 16$y++)
        {
            for (
$x 1$x <= 16$x++)
            {
                
$values_big[$x][$y][area] = $neighbor[$x][$y][area];
                
                 
// test output
                  
if ( $neighbor[$x][$y][area] < 10 ) { $neighbor[$x][$y][area] = " " $neighbor[$x][$y][area]; }
                  if ( 
$x == ) {
                  echo 
"\n " $neighbor[$x][$y][area];
                  } else {
                  echo 
" " $neighbor[$x][$y][area];
                  }
            }
        }
echo 
"\n  ";
        for (
$y 1$y <= 16$y++)
        {
            for (
$x 1$x <= 16$x++)
            {
                
$values_big[$x][$y][group] = $neighbor[$x][$y][group];
                
                 
// test output
                  
if ( $neighbor[$x][$y][group] < 10 ) { $neighbor[$x][$y][group] = " " $neighbor[$x][$y][group]; }
                  if ( 
$x == ) {
                  echo 
"\n " $neighbor[$x][$y][group];
                  } else {
                  echo 
" " $neighbor[$x][$y][group];
                  }
            }
        }

        unset(
$group_extremes);
        unset(
$neighbor);
        unset(
$area_exists);
        unset(
$areas);

        
// SMALL

        
$tolerance_r 23;
        
$tolerance_g 23;
        
$tolerance_b 23;

        
$area 1;
        
$group 1;

        echo 
"";
        for (
$x 1$x <= 8$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$neighbor[$x][$y][self_r] = $values_small[$x][$y][r];
                
$neighbor[$x][$y][self_g] = $values_small[$x][$y][g];
                
$neighbor[$x][$y][self_b] = $values_small[$x][$y][b];

                
$neighbor[$x][$y][top_r] = $values_small[$x][$y-1][r];
                
$neighbor[$x][$y][top_g] = $values_small[$x][$y-1][g];
                
$neighbor[$x][$y][top_b] = $values_small[$x][$y-1][b];

                
$neighbor[$x][$y][left_r] = $values_small[$x-1][$y][r];
                
$neighbor[$x][$y][left_g] = $values_small[$x-1][$y][g];
                
$neighbor[$x][$y][left_b] = $values_small[$x-1][$y][b];

                
$neighbor[$x][$y][right_r] = $values_small[$x+1][$y][r];
                
$neighbor[$x][$y][right_g] = $values_small[$x+1][$y][g];
                
$neighbor[$x][$y][right_b] = $values_small[$x+1][$y][b];

                
$neighbor[$x][$y][bottom_r] = $values_small[$x][$y+1][r];
                
$neighbor[$x][$y][bottom_g] = $values_small[$x][$y+1][g];
                
$neighbor[$x][$y][bottom_b] = $values_small[$x][$y+1][b];


                if ( 
$neighbor[$x][$y][left_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][left_r] > $neighbor[$x][$y][self_r] - $tolerance_r
                
                
&& $neighbor[$x][$y][left_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][left_g] > $neighbor[$x][$y][self_g] - $tolerance_g
 
                
&& $neighbor[$x][$y][left_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][left_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes_small[$area][r][min]) ) { $group_extremes_small[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][r][max]) ) { $group_extremes_small[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][g][min]) ) { $group_extremes_small[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][g][max]) ) { $group_extremes_small[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][b][min]) ) { $group_extremes_small[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][b][max]) ) { $group_extremes_small[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes_small[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes_small[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes_small[$area][b][min] ) {
                        
$group_extremes_small[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][min] = $group_extremes_small[$area][r][min];
                        
$group_extremes_small[$area][g][min] = $group_extremes_small[$area][g][min];
                        
$group_extremes_small[$area][b][min] = $group_extremes_small[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes_small[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes_small[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes_small[$area][b][max] ) {
                        
$group_extremes_small[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][max] = $group_extremes_small[$area][r][max];
                        
$group_extremes_small[$area][g][max] = $group_extremes_small[$area][g][max];
                        
$group_extremes_small[$area][b][max] = $group_extremes_small[$area][b][max];
                    }

                    for ( 
$a count($group_extremes_small); $a >= 1$a-- ) {
                        if ( 
$values_small[$x-1][$y][r] < $group_extremes_small[$a][r][max]
                        && 
$values_small[$x-1][$y][r] > $group_extremes_small[$a][r][min]
                        && 
$values_small[$x-1][$y][g] < $group_extremes_small[$a][g][max]
                        && 
$values_small[$x-1][$y][g] > $group_extremes_small[$a][g][min]
                        && 
$values_small[$x-1][$y][b] < $group_extremes_small[$a][b][max]
                        && 
$values_small[$x-1][$y][b] > $group_extremes_small[$a][b][min] ) {
                            
$area_exists $a;

                        }
                    }

                    if (
$neighbor[$x-1][$y][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x-1][$y][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x-1][$y][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x-1][$y][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][top_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][top_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][top_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][top_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][top_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][top_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes_small[$area][r][min]) ) { $group_extremes_small[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][r][max]) ) { $group_extremes_small[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][g][min]) ) { $group_extremes_small[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][g][max]) ) { $group_extremes_small[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][b][min]) ) { $group_extremes_small[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][b][max]) ) { $group_extremes_small[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes_small[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes_small[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes_small[$area][b][min] ) {
                        
$group_extremes_small[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][min] = $group_extremes_small[$area][r][min];
                        
$group_extremes_small[$area][g][min] = $group_extremes_small[$area][g][min];
                        
$group_extremes_small[$area][b][min] = $group_extremes_small[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes_small[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes_small[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes_small[$area][b][max] ) {
                        
$group_extremes_small[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][max] = $group_extremes_small[$area][r][max];
                        
$group_extremes_small[$area][g][max] = $group_extremes_small[$area][g][max];
                        
$group_extremes_small[$area][b][max] = $group_extremes_small[$area][b][max];
                    }

                    for ( 
$a count($group_extremes_small); $a >= 1$a-- ) {

                        if ( 
$values_small[$x][$y-1][r] < $group_extremes_small[$a][r][max]
                        && 
$values_small[$x][$y-1][r] > $group_extremes_small[$a][r][min]
                        && 
$values_small[$x][$y-1][g] < $group_extremes_small[$a][g][max]
                        && 
$values_small[$x][$y-1][g] > $group_extremes_small[$a][g][min]
                        && 
$values_small[$x][$y-1][b] < $group_extremes_small[$a][b][max]
                        && 
$values_small[$x][$y-1][b] > $group_extremes_small[$a][b][min] ) {
                            
$area_exists $a;
                        }
                    }

                    if (
$neighbor[$x][$y-1][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x][$y-1][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x][$y-1][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x][$y-1][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][right_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][right_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][right_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][right_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][right_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][right_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes_small[$area][r][min]) ) { $group_extremes_small[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][r][max]) ) { $group_extremes_small[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][g][min]) ) { $group_extremes_small[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][g][max]) ) { $group_extremes_small[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][b][min]) ) { $group_extremes_small[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][b][max]) ) { $group_extremes_small[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes_small[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes_small[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes_small[$area][b][min] ) {
                        
$group_extremes_small[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][min] = $group_extremes_small[$area][r][min];
                        
$group_extremes_small[$area][g][min] = $group_extremes_small[$area][g][min];
                        
$group_extremes_small[$area][b][min] = $group_extremes_small[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes_small[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes_small[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes_small[$area][b][max] ) {
                        
$group_extremes_small[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes[$area][r][max] = $group_extremes[$area][r][max];
                        
$group_extremes[$area][g][max] = $group_extremes[$area][g][max];
                        
$group_extremes[$area][b][max] = $group_extremes[$area][b][max];
                    }

                    for ( 
$a count($group_extremes); $a >= 1$a-- ) {

                        if ( 
$values_small[$x+1][$y][r] < $group_extremes_small[$a][r][max]
                        && 
$values_small[$x+1][$y][r] > $group_extremes_small[$a][r][min]
                        && 
$values_small[$x+1][$y][g] < $group_extremes_small[$a][g][max]
                        && 
$values_small[$x+1][$y][g] > $group_extremes_small[$a][g][min]
                        && 
$values_small[$x+1][$y][b] < $group_extremes_small[$a][b][max]
                        && 
$values_small[$x+1][$y][b] > $group_extremes_small[$a][b][min] ) {
                            
$area_exists $a;
                        }
                    }

                    if (
$neighbor[$x+1][$y][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x+1][$y][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x+1][$y][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x+1][$y][area]]++;
                    }

                } elseif ( 
$neighbor[$x][$y][bottom_r] < $neighbor[$x][$y][self_r] + $tolerance_r
                
&& $neighbor[$x][$y][bottom_r] > $neighbor[$x][$y][self_r] - $tolerance_r

                
&& $neighbor[$x][$y][bottom_g] < $neighbor[$x][$y][self_g] + $tolerance_g
                
&& $neighbor[$x][$y][bottom_g] > $neighbor[$x][$y][self_g] - $tolerance_g

                
&& $neighbor[$x][$y][bottom_b] < $neighbor[$x][$y][self_b] + $tolerance_b
                
&& $neighbor[$x][$y][bottom_b] > $neighbor[$x][$y][self_b] - $tolerance_b ) {

                    if ( !isset(
$group_extremes_small[$area][r][min]) ) { $group_extremes_small[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][r][max]) ) { $group_extremes_small[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][g][min]) ) { $group_extremes_small[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][g][max]) ) { $group_extremes_small[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes_small[$area][b][min]) ) { $group_extremes_small[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes_small[$area][b][max]) ) { $group_extremes_small[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes_small[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes_small[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes_small[$area][b][min] ) {
                        
$group_extremes_small[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][min] = $group_extremes_small[$area][r][min];
                        
$group_extremes_small[$area][g][min] = $group_extremes_small[$area][g][min];
                        
$group_extremes_small[$area][b][min] = $group_extremes_small[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes_small[$area][r][max
                    && 
$neighbor[$x][$y][self_g] > $group_extremes_small[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes_small[$area][b][max] ) {
                        
$group_extremes_small[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][max] = $group_extremes_small[$area][r][max];
                        
$group_extremes_small[$area][g][max] = $group_extremes_small[$area][g][max];
                        
$group_extremes_small[$area][b][max] = $group_extremes_small[$area][b][max];
                    }

                    for ( 
$a count($group_extremes_small); $a >= 1$a-- ) {

                        if ( 
$values_small[$x-1][$y][r] < $group_extremes_small[$a][r][max]
                        && 
$values_small[$x][$y+1][r] > $group_extremes_small[$a][r][min]
                        && 
$values_small[$x][$y+1][g] < $group_extremes_small[$a][g][max]
                        && 
$values_small[$x][$y+1][g] > $group_extremes_small[$a][g][min]
                        && 
$values_small[$x][$y+1][b] < $group_extremes_small[$a][b][max]
                        && 
$values_small[$x][$y+1][b] > $group_extremes_small[$a][b][min] ) {
                            
$area_exists $a;
                        }
                    }

                    if (
$neighbor[$x][$y+1][area] == '') {
                        
$neighbor[$x][$y][area] = $area;
                        
$neighbor[$x][$y][group] = $area;
                        
$areas[$area]++;
                    } else {
                        
$neighbor[$x][$y][area] = $neighbor[$x][$y+1][area];

                        if ( 
$area_exists == FALSE ) {
                            
$neighbor[$x][$y][group] = $neighbor[$x][$y+1][area];
                        } else {
                            
$neighbor[$x][$y][group] = $area_exists;
                        }

                        
$areas[$neighbor[$x][$y+1][area]]++;
                    }

                } else {


                    
$neighbor[$x][$y][area] = $area;
                    
$neighbor[$x][$y][group] = $area;
                    
$area++;
                    
$areas[$area]++;


                    if ( !isset(
$group_extremes[$area][r][min]) ) { $group_extremes_small[$area][r][min] = 255; }
                    if ( !isset(
$group_extremes[$area][r][max]) ) { $group_extremes_small[$area][r][max] = 0; }
                    if ( !isset(
$group_extremes[$area][g][min]) ) { $group_extremes_small[$area][g][min] = 255; }
                    if ( !isset(
$group_extremes[$area][g][max]) ) { $group_extremes_small[$area][g][max] = 0; }
                    if ( !isset(
$group_extremes[$area][b][min]) ) { $group_extremes_small[$area][b][min] = 255; }
                    if ( !isset(
$group_extremes[$area][b][max]) ) { $group_extremes_small[$area][b][max] = 0; }

                    if ( 
$neighbor[$x][$y][self_r] < $group_extremes_small[$area][r][min]
                    && 
$neighbor[$x][$y][self_g] < $group_extremes_small[$area][g][min]
                    && 
$neighbor[$x][$y][self_b] < $group_extremes_small[$area][b][min] ) {
                        
$group_extremes_small[$area][r][min] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][min] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][min] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][min] = $group_extremes_small[$area][r][min];
                        
$group_extremes_small[$area][g][min] = $group_extremes_small[$area][g][min];
                        
$group_extremes_small[$area][b][min] = $group_extremes_small[$area][b][min];
                    }

                    if ( 
$neighbor[$x][$y][self_r] > $group_extremes_small[$area][r][max]
                    && 
$neighbor[$x][$y][self_g] > $group_extremes_small[$area][g][max]
                    && 
$neighbor[$x][$y][self_b] > $group_extremes_small[$area][b][max]) {
                        
$group_extremes_small[$area][r][max] = $neighbor[$x][$y][self_r];
                        
$group_extremes_small[$area][g][max] = $neighbor[$x][$y][self_g];
                        
$group_extremes_small[$area][b][max] = $neighbor[$x][$y][self_b];
                    } else {
                        
$group_extremes_small[$area][r][max] = $group_extremes_small[$area][r][max];
                        
$group_extremes_small[$area][g][max] = $group_extremes_small[$area][g][max];
                        
$group_extremes_small[$area][b][max] = $group_extremes_small[$area][b][max];
                    }

                }
            }
        }


echo 
"\n  ";
        for (
$y 1$y <= 8$y++)
        {
            for (
$x 1$x <= 8$x++)
            {
                
$values_small[$x][$y][area] = $neighbor[$x][$y][area];
                
                 
// test output
                  
if ( $neighbor[$x][$y][area] < 10 ) { $neighbor[$x][$y][area] = " " $neighbor[$x][$y][area]; }
                  if ( 
$x == ) {
                      echo 
"\n " $neighbor[$x][$y][area];
                  } else {
                      echo 
" " $neighbor[$x][$y][area];
                  }
            }
        }
echo 
"\n  ";
        for (
$y 1$y <= 8$y++)
        {
            for (
$x 1$x <= 8$x++)
            {
                
$values_small[$x][$y][group] = $neighbor[$x][$y][group];
                
                 
// test output
                  
if ( $neighbor[$x][$y][group] < 10 ) { $neighbor[$x][$y][group] = " " $neighbor[$x][$y][group]; }
                  if ( 
$x == ) {
                      echo 
"\n " $neighbor[$x][$y][group];
                  } else {
                      echo 
" " $neighbor[$x][$y][group];
                  }
            }
        }
echo 
"\n  ";

        unset(
$group_extremes_small);
        unset(
$neighbor);
        unset(
$area_exists);
        
// unset($areas);
    
    
        // counting all elements of an image (gives you contrast)
        
$element_amount count($areas);

        for (
$i 1$i <= $element_amount$i++)
        {
            if (
$areas[$i] < 4) { unset($areas[$i]); }
        }

        
// erasing elements that accur less than 4 times gives you groups
        
$group_amount =count($areas);



        
// color amounts (small values used)

        
for ($x 1$x <= 8$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$r_amount $r_amount $values_small[$x][$y][r];
                
$g_amount $g_amount $values_small[$x][$y][g];
                
$b_amount $b_amount $values_small[$x][$y][b];
            }
        }
        
$r_average $r_amount 64 255;
        
$g_average $g_amount 64 255;
        
$b_average $b_amount 64 255;


        
// brightness

        
for ($x 1$x <= 8$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$brightness_amount $brightness_amount $values_small[$x][$y][brightness];
            }
        }
        
$brightness_average $brightness_amount 64 255;


        
// Symmetry -- radial

        
for ($x 1$x <= 8/2$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$index1 $x;
                
$index2 $y;
                
$index3 $x 1;
                
$index4 $y 1;

                
$index strval("$index1 $index2 $index3 $index4");

                
$pair_radial_r[$index] = abs($values_small[$index1][$index2][r] - $values_small[$index3][$index4][r]);
                
$pair_radial_g[$index] = abs($values_small[$index1][$index2][g] - $values_small[$index3][$index4][g]);
                
$pair_radial_b[$index] = abs($values_small[$index1][$index2][b] - $values_small[$index3][$index4][b]);
            }
        }

        
$average_r array_sum($pair_radial_r) / (count($pair_radial_r) * 255);
        
$average_g array_sum($pair_radial_g) / (count($pair_radial_r) * 255);
        
$average_b array_sum($pair_radial_b) / (count($pair_radial_r) * 255);

        
$radial_symmetry = ($average_r $average_g $average_b) / 3;


        
// Symmetry -- horizontal

        
for ($x 1$x <= 8/2$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$index1 $x;
                
$index2 $y;
                
$index3 $x 1;
                
$index4 $y;

                
$index strval("$index1 $index2 $index3 $index4");

                
$pair_horizontal_r[$index] = abs($values_small[$index1][$index2][r] - $values_small[$index3][$index4][r]);
                
$pair_horizontal_g[$index] = abs($values_small[$index1][$index2][g] - $values_small[$index3][$index4][g]);
                
$pair_horizontal_b[$index] = abs($values_small[$index1][$index2][b] - $values_small[$index3][$index4][b]);
            }
        }

        
$average_r array_sum($pair_horizontal_r) / (count($pair_horizontal_r) * 255);
        
$average_g array_sum($pair_horizontal_g) / (count($pair_horizontal_r) * 255);
        
$average_b array_sum($pair_horizontal_b) / (count($pair_horizontal_r) * 255);

        
$horizontal_symmetry = ($average_r $average_g $average_b) / 3;


        
// Symmetry -- vertical

        
for ($x 1$x <= 8/2$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                
$index1 $x;
                
$index2 $y;
                
$index3 $x;
                
$index4 $y 1;

                
$index strval("$index1 $index2 $index3 $index4");

                
$pair_vertical_r[$index] = abs($values_small[$index1][$index2][r] - $values_small[$index3][$index4][r]);
                
$pair_vertical_g[$index] = abs($values_small[$index1][$index2][g] - $values_small[$index3][$index4][g]);
                
$pair_vertical_b[$index] = abs($values_small[$index1][$index2][b] - $values_small[$index3][$index4][b]);
            }
        }

        
$average_r array_sum($pair_vertical_r) / (count($pair_horizontal_r) * 255);
        
$average_g array_sum($pair_vertical_g) / (count($pair_horizontal_r) * 255);
        
$average_b array_sum($pair_vertical_b) / (count($pair_horizontal_r) * 255);

        
$vertical_symmetry = ($average_r $average_g $average_b) / 3;

    }


    
// OUT
    /////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////


    // compare with Text (TextsConfirmed and TextsSaved)!
    /////////////////////////////////////////////////////////////////////////////

    
$query1 "SELECT id FROM TextsConfirmed WHERE id = '$site_id'";
    
$query1 mysql_query($query1);
    if (
$query1) { 
        
$query_result1 mysql_fetch_assoc($query1);
        
$site_id_tc $query_result1['id'];
    }

    
$query2 "SELECT id FROM TextsSaved WHERE id = '$site_id'";
    
$query2 mysql_query($query2);
    if (
$query2) {
        
$query_result2 mysql_fetch_assoc($query2);
        
$site_id_ts $query_result2['id'];
    }


    
// id found in tc, not in ts
    
if ( $site_id_tc && !$site_id_ts ) {

        
// image
        
$write_out == TRUE;

        
// text: lesen, einfügen, löschen
        
$query "SELECT * FROM TextsConfirmed WHERE id = '$site_id'";
        
$query mysql_query($query);
        
$query_result mysql_fetch_assoc($query);

        
$text_id $query_result['id'];
        
$text_url $query_result['url'];
        
$text_timestamp $query_result['timestamp'];
        
$text_title $query_result['title'];
        
$text_author $query_result['author'];
        
$text_keywords $query_result['keywords'];
        
$text_description $query_result['description'];
        
$text_linked_from_id $query_result['linked_from_id'];


        
$query "INSERT INTO TextsSaved (id, url, timestamp, title, author, keywords, description, linked_from_id)
            VALUES ( '
$text_id', '$text_url', '$text_timestamp', '$text_title', '$text_author', '$text_keywords', '$text_description', '$linked_from_id' );";
        
$query_insert mysql_query($query);

        
$query "DELETE FROM TextsConfirmed WHERE id = '$site_id';";
        
$query mysql_query($query);

        
$text_msg "image and text moved";
        
$create_thumb TRUE;


    
// id not found in tc, but in ts
    
} elseif ( !$site_id_tc && $site_id_ts ) {

        
// image
        
$write_out == TRUE;

        
// nothing done with text

        
$text_msg "image assigned to existing page";
        
$create_thumb TRUE;


    
// id not found in tc, neither in ts
    
} elseif ( !$site_id_tc && !$site_id_ts ) {

        
// image
        
$write_out == FALSE;
        
$delete_in == TRUE;

        
// nothing done with text (that does not exist)

        
$text_msg "no texts found, image deleted";
        
$create_thumb FALSE;


    
// id found in tc and in ts (rare)
    
} elseif ( $site_id_tc && $site_id_ts ) {

        
// image
        
$write_out == TRUE;

        
// text: lesen, UPDATEN, löschen
        
$query "SELECT * FROM TextsConfirmed WHERE id = '$site_id'";
        
$query mysql_query($query);
        
$query_result mysql_fetch_assoc($query);

        
$text_id $query_result['id'];
        
$text_url $query_result['url'];
        
$text_timestamp $query_result['timestamp'];
        
$text_title $query_result['title'];
        
$text_author $query_result['author'];
        
$text_keywords $query_result['keywords'];
        
$text_description $query_result['description'];
        
$text_linked_from_id $query_result['linked_from_id'];

        
// update
        
$query "UPDATE TextsSaved SET url = '$text_url', timestamp = '$text_timestamp',
            title = '
$text_title', author = '$text_author', keywords = $text_keywords', description = '$text_description',
            linked_from_id = '
$text_linked_from_id'
            WHERE id = '
$site_id'";
        
$query_update mysql_query($query);

        
$query "DELETE FROM TextsConfirmed WHERE id = '$site_id';";
        
$query mysql_query($query);

        
$text_msg "image moved and text updated";
        
$create_thumb TRUE;


    }

    
// OUT to several tables
    /////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////

    
if ($write_out == TRUE ) {
        
$query "INSERT INTO ImagesSaved ( site_id, image_id, url, timestamp, updated, width, height, aspect, size, alt, longdesc, leftside, rightside )
            VALUES ( '
$site_id', '', '$url', '$timestamp', '$updated', '$width', '$height', '$aspect', '$size', '$alt', '$longdesc', '$leftside', '$rightside' );";
        
$query_insert_saved mysql_query($query);
        
$id mysql_insert_id();
    }


    
// create thumbnails and wait a bit
    ////////////////////////////////////////////////////////////////////////
    
if ($create_thumb == TRUE) {

        if (
$aspect 1.1) {
            
// echo "landscape";
            
$resize_big1 shell_exec"convert " $url " -resize " $img_src_thumb_size "x /var/www/is/public/thumbs/" $id ".jpg" );
            
// debug coder was needed to make the shell answer
        
} elseif ($aspect 1.1) {
            
// echo "potrait";
            
$resize_big1 shell_exec"convert " $url " -resize x" $img_src_thumb_size " /var/www/is/public/thumbs/" $id ".jpg" );
        } else {
            
// echo "almost square";
            
$resize_big1 shell_exec"convert " $url " -resize " round($img_src_thumb_size*0.9) . "x /var/www/is/public/thumbs/" $id ".jpg" );
        }
        
sleep(2);

        
// REAL check if the craetion of thumbs was successful
        
if (file_exists("/var/www/is/public/thumbs/" $id ".jpg")) {
            
$resize_big TRUE;
            
// $real_thumb_msg = " Ok.";
        
} else {
            echo 
"\n  Error creating a thumbnail!\n";
            
// $real_thumb_msg = " Error!";
        
}

    }




    
// resize big checks if there was a thumbnail created
    ////////////////////////////////////////////////////////////////////////
    // if not -- NOTHING goes out! (not any longer)


    
if ($write_out == TRUE && $resize_big) {
        
$query_big_part1_red "INSERT INTO ImagesRValuesBig ( id, ";
        
$query_big_part1_green "INSERT INTO ImagesGValuesBig ( id, ";
        
$query_big_part1_blue "INSERT INTO ImagesBValuesBig ( id, ";
        
$query_big_part1_brightness "INSERT INTO ImagesBrightnessValuesBig ( id, ";
        
$query_big_part1_area "INSERT INTO ImagesAreaValuesBig ( id, ";
        
$query_big_part1_group "INSERT INTO ImagesGroupValuesBig ( id, ";

        
$query_small_part1_red "INSERT INTO ImagesRValuesSmall ( id, ";
        
$query_small_part1_green "INSERT INTO ImagesGValuesSmall ( id, ";
        
$query_small_part1_blue "INSERT INTO ImagesBValuesSmall ( id, ";
        
$query_small_part1_brightness "INSERT INTO ImagesBrightnessValuesSmall ( id, ";
        
$query_small_part1_area "INSERT INTO ImagesAreaValuesSmall ( id, ";
        
$query_small_part1_group "INSERT INTO ImagesGroupValuesSmall( id, ";

        for (
$x 1$x <= 16$x++)
        {
            for (
$y 1$y <= 16$y++)
            {
                if ( 
$x == 16 && $y == 16 ) {
                    
$query_big_part2 $query_big_part2 $x "_" $y " ) ";
                } else {
                    
$query_big_part2 $query_big_part2 $x "_" $y ", ";
                }
            }
        }

        for (
$x 1$x <= 8$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                if ( 
$x == && $y == ) {
                    
$query_small_part2 $query_small_part2 $x "_" $y " ) ";
                } else {
                    
$query_small_part2 $query_small_part2 $x "_" $y ", ";
                }
            }
        }

        
$query_part3 "VALUES ( '$id', ";

        for (
$x 1$x <= 16$x++)
        {
            for (
$y 1$y <= 16$y++)
            {
                if ( 
$x == 16 && $y == 16 ) {
                    
$query_big_part4_red $query_big_part4_red "'" $values_big[$x][$y][r] . "'" " );";
                    
$query_big_part4_green $query_big_part4_green "'" $values_big[$x][$y][g] . "'" " );";
                    
$query_big_part4_blue $query_big_part4_blue "'" $values_big[$x][$y][b] . "'" " );";
                    
$query_big_part4_brightness $query_big_part4_brightness "'" $values_big[$x][$y][brightness] . "'" " );";
                    
$query_big_part4_area $query_big_part4_area "'" $values_big[$x][$y][area] . "'" " );";
                    
$query_big_part4_group $query_big_part4_group "'" $values_big[$x][$y][group] . "'" " );";
                } else {
                    
$query_big_part4_red $query_big_part4_red "'" $values_big[$x][$y][r] . "'" ", ";
                    
$query_big_part4_green $query_big_part4_green "'" $values_big[$x][$y][g] . "'" ", ";
                    
$query_big_part4_blue $query_big_part4_blue "'" $values_big[$x][$y][b] . "'" ", ";
                    
$query_big_part4_brightness $query_big_part4_brightness "'" $values_big[$x][$y][brightness] . "'" ", ";
                    
$query_big_part4_area $query_big_part4_area "'" $values_big[$x][$y][area] . "'" ", ";
                    
$query_big_part4_group $query_big_part4_group "'" $values_big[$x][$y][group] . "'" ", ";
                }
            }
        }

        for (
$x 1$x <= 8$x++)
        {
            for (
$y 1$y <= 8$y++)
            {
                if ( 
$x == && $y == ) {
                    
$query_small_part4_red $query_small_part4_red "'" $values_small[$x][$y][r] . "'" " );";
                    
$query_small_part4_green $query_small_part4_green "'" $values_small[$x][$y][g] . "'" " );";
                    
$query_small_part4_blue $query_small_part4_blue "'" $values_small[$x][$y][b] . "'" " );";
                    
$query_small_part4_brightness $query_small_part4_brightness "'" $values_small[$x][$y][brightness] . "'" " );";
                    
$query_small_part4_area $query_small_part4_area "'" $values_small[$x][$y][area] . "'" " );";
                    
$query_small_part4_group $query_small_part4_group "'" $values_small[$x][$y][group] . "'" " );";
                } else {
                    
$query_small_part4_red $query_small_part4_red "'" $values_small[$x][$y][r] . "'" ", ";
                    
$query_small_part4_green $query_small_part4_green "'" $values_small[$x][$y][g] . "'" ", ";
                    
$query_small_part4_blue $query_small_part4_blue "'" $values_small[$x][$y][b] . "'" ", ";
                    
$query_small_part4_brightness $query_small_part4_brightness "'" $values_small[$x][$y][brightness] . "'" ", ";
                    
$query_small_part4_area $query_small_part4_area "'" $values_small[$x][$y][area] . "'" ", ";
                    
$query_small_part4_group $query_small_part4_group "'" $values_small[$x][$y][group] . "'" ", ";
                }
            }
        }

    }

    if (
$write_out == TRUE) {
        
$query_big_red $query_big_part1_red $query_big_part2 $query_part3 $query_big_part4_red;
        
$query_big_green $query_big_part1_green $query_big_part2 $query_part3 $query_big_part4_green;
        
$query_big_blue $query_big_part1_blue $query_big_part2 $query_part3 $query_big_part4_blue;
        
$query_big_brightness $query_big_part1_brightness $query_big_part2 $query_part3 $query_big_part4_brightness;
        
$query_big_area $query_big_part1_area $query_big_part2 $query_part3 $query_big_part4_area;
        
$query_big_group $query_big_part1_group $query_big_part2 $query_part3 $query_big_part4_group;

        
$query_insert_numbers mysql_query($query_big_red);
        
$query_insert_numbers mysql_query($query_big_green);
        
$query_insert_numbers mysql_query($query_big_blue);
        
$query_insert_numbers mysql_query($query_big_brightness);
        
$query_insert_numbers mysql_query($query_big_area);
        
$query_insert_numbers mysql_query($query_big_group);

        
$query_small_red $query_small_part1_red $query_small_part2 $query_part3 $query_small_part4_red;
        
$query_small_green $query_small_part1_green $query_small_part2 $query_part3 $query_small_part4_green;
        
$query_small_blue $query_small_part1_blue $query_small_part2 $query_part3 $query_small_part4_blue;
        
$query_small_brightness $query_small_part1_brightness $query_small_part2 $query_part3 $query_small_part4_brightness;
        
$query_small_area $query_small_part1_area $query_small_part2 $query_part3 $query_small_part4_area;
        
$query_small_group $query_small_part1_group $query_small_part2 $query_part3 $query_small_part4_group;

        
$query_insert_numbers mysql_query($query_small_red);
        
$query_insert_numbers mysql_query($query_small_green);
        
$query_insert_numbers mysql_query($query_small_blue);
        
$query_insert_numbers mysql_query($query_small_brightness);
        
$query_insert_numbers mysql_query($query_small_area);
        
$query_insert_numbers mysql_query($query_small_group);
    }

    if (
$write_out == TRUE) {
        
$query "INSERT INTO ImagesAmountValues ( id, red, green, blue, brightness, groups, elements )
            VALUES ( '
$id', '$r_average', '$g_average', '$b_average', '$brightness_average', '$group_amount', '$element_amount' );";
        
$query_insert_amounts mysql_query($query);
    }

    if (
$write_out == TRUE) {
        
$query "INSERT INTO ImagesSymmetryValues ( id, radial, horizontal, vertical )
            VALUES ( '
$id', '$radial_symmetry', '$horizontal_symmetry', '$vertical_symmetry' );";
        
$query_insert_symmetry mysql_query($query);
    }


    
// update program control (PRG_CTR!)
    /////////////////////////////////////////////////////////////////////////////
    
if ($write_out == TRUE) {
        
$query "UPDATE ProgramControl SET running = 1, lastrun = CURRENT_TIMESTAMP() WHERE program = 'IMG_SRC';";
        
$query_update_control mysql_query($query);
    } elseif ( 
$rows_number == ) {
        
$query "UPDATE ProgramControl SET running = 2, lastrun = CURRENT_TIMESTAMP() WHERE program = 'IMG_SRC';";
        
$query_update_control mysql_query($query);
    }


    
// delete from IN table
    /////////////////////////////////////////////////////////////////////////////
    
if ($delete_in == TRUE) {
        
$query "DELETE FROM ImagesConfirmed WHERE url = '$url';";
        
$query_delete mysql_query($query);
    }




    
// take the time
    /////////////////////////////////////////////////////////////////////////////    
    
$time time() - $time;


    
// shell messages
    /////////////////////////////////////////////////////////////////////////////


    
if ($query_insert_saved == 1) { $query_insert_saved_msg "Ok"; } else { $query_insert_saved_msg "Error"; }
    if (
$query_insert_numbers == 1) { $query_insert_numbers_msg "Ok"; } else { $query_insert_numbers_msg "Error"; }
    if (
$query_insert_symmetry == 1) { $query_insert_symmetry_msg "Ok"; } else { $query_insert_symmetry_msg "Error"; }
    if (
$query_insert_amounts == 1) { $query_insert_amounts_msg "Ok"; } else { $query_insert_amounts_msg "Error"; }
    if (
$query_insert_delete == 1) { $query_insert_delete_msg "Ok"; } else { $query_insert_delete_msg "Error"; }

    if (isset(
$resize_big)) {
        
$thumb_msg "  Thumbnail created: " $id ".jpg for site ID# " $site_id "\n";
    } else {
        
$thumb_msg "  Warning! No thumbnail created!\n";
    }



    if ( 
$query_insert_saved && $query_insert_numbers && $query_insert_symmetry && $query_insert_amounts && $query_delete ) {
        
$confirm " added.\n" .
        
"  Image Save: " $query_insert_saved_msg ".\n" .
        
"  Mathematical Values: " $query_insert_numbers_msg ".\n" .
        
"  Symmetry Values: " $query_insert_saved_msg ".\n" .
        
"  Delete from IN-Tables: " $query_insert_saved_msg ".\n";

        
$delete_out FALSE;
    } else {
        
$confirm " failure!\n" .
        
"  Image Save: " $query_insert_saved_msg ".\n" .
        
"  Mathematical Values: " $query_insert_numbers_msg ".\n" .
        
"  Symmetry Values: " $query_insert_saved_msg ".\n" .
        
"  Delete from IN-Tables: " $query_insert_saved_msg ".\n";

        
$delete_out TRUE;
    }

    if (
$rows_number == 0) {
        if (
$mode == 'stop') {
            echo 
"\n  table ImagesConfirmed empty\n IMG_SRC stopped\n";
            return;
        } else {
            
$timer $img_src_timer_pause;
            echo 
"\n  table ImagesConfirmed empty\n  waiting "
            
$timer " seconds\n  _____________________________________________\n";
        }
    } else {
        if (
$mode == "stop") {
            echo 
"\n  " $complete_url " (-" $rows_number-")";
            echo 
"\n  IMG_SRC stopped\n  use 'loop' as argument to cycle the ImagesConfirmed table\n";
            return;
        } else {
            
$timer $img_src_timer;
            
$number $rows_number 1;
            echo 
"\n  " $url $confirm $thumb_msg 
            
"  Text Results: " $text_msg "."
            
."\n  (" $number ." to go, " $time " s needed, memory status: " .
            
round(memory_get_usage()/1024/10243) . ")\n";
        }
    }

    
sleep($timer);



    
// in the sad case sth went wrong: delete from OUT tables
    /////////////////////////////////////////////////////////////////////////////
    
    
$delete_counter 0;

    if (
$delete_out == TRUE) {

        
$all_final_tables = array(TextsSavedImagesSaved,
        
ImagesRValuesBigImagesRValuesSmall,
        
ImagesGValuesBigImagesGValuesSmall
        
ImagesBValuesBigImagesBValuesSmall,
        
ImagesBrightnessValuesBigImagesBrightnessValuesSmall
        
ImagesAreaValuesBigImagesAreaValuesSmall,
        
ImagesGroupValuesBigImagesGroupValuesSmall,
        
ImagesSymmetryValuesImagesAmountValues);


        foreach (
$all_final_tables as $k => $v) {
            
$query_delete FALSE;

            if ( 
$all_final_tables[$k] == 'ImagesSaved' ) {
                
$query "DELETE FROM " $all_final_tables[$k] . " WHERE image_id = $id;";
            } else {
                
$query "DELETE FROM " $all_final_tables[$k] . " WHERE id = $id;";
            }

            
$query_delete mysql_query($query);
            if (
$query_delete ) { $delete_counter++; }
        }
    echo 
"  Unused entries from " $delete_counter " of 16 tables removed!\n";

    }

    
// close db
    /////////////////////////////////////////////////////////////////////////////    
    
mysql_close($connect);    


}




// getting table entries for loop
/////////////////////////////////////////////////////////////////////////////

$connect mysql_connect ('localhost''imgsrc1''builder');
$database_select mysql_select_db ('imagesource'$connect);

$query 'SELECT id FROM ImagesConfirmed;';
$query mysql_query($query);
$loop mysql_affected_rows();

// set orange color
/////////////////////////////////////////////////////////////////////////////

$query "UPDATE ProgramControl SET running = 2, lastrun = CURRENT_TIMESTAMP() WHERE program = 'IMG_SRC';";
$query_update_control mysql_query($query);

mysql_close($connect);

// loop
/////////////////////////////////////////////////////////////////////////////

for ( $i 1$i <= $loop$i++ ) {
    
    
Cycle ($timer$img_src_timer$img_src_timer_pause$img_src_select_limit,
    
$img_src_thumb_size,
    
$mode);


}

?>