daily pastebin goal
11%
SHARE
TWEET

Auto Level Image using PHP GD

mcmeaties Dec 30th, 2016 (edited) 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function auto_level_image($image, $level = 95, $dynamic = 254) {
  2.     /* Adjusts levels on image by cutting out insignificant colors.
  3.      *
  4.      * $level is 1-100, defining what % of the range of colors you
  5.      * want to keep in the final image.
  6.      * This is a modified function.  Check out the original at,
  7.      * http://www.giacobbe85.altervista.org/down_en/info/Software_and_scripts/Automatic_color_correction_in_images.php?lang=eng
  8.      */
  9.    
  10.     $colors = array('red', 'green', 'blue');
  11.     $pixels = 0;
  12.    
  13.     // Create arrays to keep track of color frequency
  14.     $empty_array = array();
  15.     $red_freq = array_pad($empty_array, 256, 0);
  16.     $green_freq = array_pad($empty_array, 256, 0);
  17.     $blue_freq = array_pad($empty_array, 256, 0);
  18.    
  19.     // Scans each image pixel
  20.     for ($x=0; $x < imagesx($image); $x++) {
  21.        
  22.         for($y=0; $y < imagesy($image); $y++) {
  23.            
  24.             // Computes RGB for the current pixel
  25.             $pixel_color = imagecolorat($image, $x, $y);
  26.             $red = ($pixel_color >> 16) & 255;
  27.             $green = ($pixel_color >> 8) & 255;
  28.             $blue = $pixel_color & 255;
  29.             $pixels += 1;
  30.            
  31.             // Adds colors to frequency arrays
  32.             $red_freq[$red] += 1;
  33.             $green_freq[$green] += 1;
  34.             $blue_freq[$blue] += 1;
  35.  
  36.             // Searches for the minimum
  37.             if ($x == 0 && $y == 0) {
  38.                 $red_min = $red;
  39.                 $green_min = $green;
  40.                 $blue_min = $blue;
  41.             } else {
  42.                 if($red < $red_min) $red_min = $red;
  43.                 if($green < $green_min) $green_min = $green;
  44.                 if($blue < $blue_min) $blue_min = $blue;
  45.             }
  46.            
  47.             // Searches for the maximum
  48.             if($red > $red_max) $red_max = $red;
  49.             if($green > $green_max) $green_max = $green;
  50.             if($blue > $blue_max) $blue_max = $blue;
  51.         }
  52.     }
  53.    
  54.     // Find range of acceptable colors from $level
  55.     $range = $pixels * ($level / 100);
  56.     $outliers = ($pixels - $range) / 2;
  57.    
  58.     foreach ($colors as $color) {
  59.         ${$color . "_outliers_min"} = $outliers;
  60.         ${$color . "_outliers_max"} = $outliers + $range;
  61.         $stop_min = false;
  62.        
  63.         foreach (${$color . "_freq"} as $key => ${$color . "_lvl"}) {
  64.             ${$color . "_outliers_min"} -= ${$color . "_lvl"};
  65.             ${$color . "_outliers_max"} -= ${$color . "_lvl"};
  66.            
  67.             if (${$color . "_outliers_min"} <= 0 && $stop_min != true) {
  68.                 ${$color . "_min"} = $key;
  69.                 $stop_min = true;
  70.             }
  71.            
  72.             if (${$color . "_outliers_max"} <= 0) {
  73.                 ${$color . "_max"} = $key;
  74.                 break;
  75.             }
  76.         }
  77.     }
  78.    
  79.     # Normalization variables
  80.     foreach ($colors as $color) {
  81.         ${"shift_" . $color} = ${$color . "_min"};
  82.        
  83.         if (${$color . "_max"} != ${"shift_" . $color}) {
  84.             ${"scale_" . $color} = $dynamic / (${$color . "_max"} - ${"shift_" . $color});
  85.         } else {
  86.             ${"scale_" . $color} = 1;
  87.         }
  88.     }
  89.  
  90.     // Now to edit the image with all our new numbers!
  91.     // -----------------------------------------------
  92.    
  93.     // Scans each pixel to normalize it
  94.     for ($x=0; $x < imagesx($image); $x++) {
  95.    
  96.         for ($y=0; $y < imagesy($image); $y++) {
  97.            
  98.             // Computes the RGB values for the current pixel
  99.             $pixel_color = imagecolorat($image, $x, $y);
  100.             $red = ($pixel_color >> 16) & 255;
  101.             $green = ($pixel_color >> 8) & 255;
  102.             $blue = $pixel_color & 255;
  103.            
  104.             foreach ($colors as $color) {
  105.                
  106.                 // get new color numbers
  107.                 ${"new_" . $color} = ($$color - ${"shift_" . $color}) * ${"scale_" . $color};
  108.                
  109.                 // keep colors in range of min and max
  110.                 if (${"new_" . $color} > 255) {
  111.                     ${"new_" . $color} = 255;
  112.                 } elseif (${"new_" . $color} < 0) {
  113.                     ${"new_" . $color} = 0;
  114.                 }
  115.             }
  116.      
  117.             // Computes the normalized pixel and saves it in the image
  118.             $color_pixel = imagecolorallocate($image, $new_red, $new_green, $new_blue);
  119.             imagesetpixel($image, $x, $y, $color_pixel);
  120.         }
  121.     }
  122.    
  123.     return 1;
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top