Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function auto_level_image($image, $level = 95, $dynamic = 254) {
- /* Adjusts levels on image by cutting out insignificant colors.
- *
- * $level is 1-100, defining what % of the range of colors you
- * want to keep in the final image.
- * This is a modified function. Check out the original at,
- * http://www.giacobbe85.altervista.org/down_en/info/Software_and_scripts/Automatic_color_correction_in_images.php?lang=eng
- */
- $colors = array('red', 'green', 'blue');
- $pixels = 0;
- // Create arrays to keep track of color frequency
- $empty_array = array();
- $red_freq = array_pad($empty_array, 256, 0);
- $green_freq = array_pad($empty_array, 256, 0);
- $blue_freq = array_pad($empty_array, 256, 0);
- // Scans each image pixel
- for ($x=0; $x < imagesx($image); $x++) {
- for($y=0; $y < imagesy($image); $y++) {
- // Computes RGB for the current pixel
- $pixel_color = imagecolorat($image, $x, $y);
- $red = ($pixel_color >> 16) & 255;
- $green = ($pixel_color >> 8) & 255;
- $blue = $pixel_color & 255;
- $pixels += 1;
- // Adds colors to frequency arrays
- $red_freq[$red] += 1;
- $green_freq[$green] += 1;
- $blue_freq[$blue] += 1;
- // Searches for the minimum
- if ($x == 0 && $y == 0) {
- $red_min = $red;
- $green_min = $green;
- $blue_min = $blue;
- } else {
- if($red < $red_min) $red_min = $red;
- if($green < $green_min) $green_min = $green;
- if($blue < $blue_min) $blue_min = $blue;
- }
- // Searches for the maximum
- if($red > $red_max) $red_max = $red;
- if($green > $green_max) $green_max = $green;
- if($blue > $blue_max) $blue_max = $blue;
- }
- }
- // Find range of acceptable colors from $level
- $range = $pixels * ($level / 100);
- $outliers = ($pixels - $range) / 2;
- foreach ($colors as $color) {
- ${$color . "_outliers_min"} = $outliers;
- ${$color . "_outliers_max"} = $outliers + $range;
- $stop_min = false;
- foreach (${$color . "_freq"} as $key => ${$color . "_lvl"}) {
- ${$color . "_outliers_min"} -= ${$color . "_lvl"};
- ${$color . "_outliers_max"} -= ${$color . "_lvl"};
- if (${$color . "_outliers_min"} <= 0 && $stop_min != true) {
- ${$color . "_min"} = $key;
- $stop_min = true;
- }
- if (${$color . "_outliers_max"} <= 0) {
- ${$color . "_max"} = $key;
- break;
- }
- }
- }
- # Normalization variables
- foreach ($colors as $color) {
- ${"shift_" . $color} = ${$color . "_min"};
- if (${$color . "_max"} != ${"shift_" . $color}) {
- ${"scale_" . $color} = $dynamic / (${$color . "_max"} - ${"shift_" . $color});
- } else {
- ${"scale_" . $color} = 1;
- }
- }
- // Now to edit the image with all our new numbers!
- // -----------------------------------------------
- // Scans each pixel to normalize it
- for ($x=0; $x < imagesx($image); $x++) {
- for ($y=0; $y < imagesy($image); $y++) {
- // Computes the RGB values for the current pixel
- $pixel_color = imagecolorat($image, $x, $y);
- $red = ($pixel_color >> 16) & 255;
- $green = ($pixel_color >> 8) & 255;
- $blue = $pixel_color & 255;
- foreach ($colors as $color) {
- // get new color numbers
- ${"new_" . $color} = ($$color - ${"shift_" . $color}) * ${"scale_" . $color};
- // keep colors in range of min and max
- if (${"new_" . $color} > 255) {
- ${"new_" . $color} = 255;
- } elseif (${"new_" . $color} < 0) {
- ${"new_" . $color} = 0;
- }
- }
- // Computes the normalized pixel and saves it in the image
- $color_pixel = imagecolorallocate($image, $new_red, $new_green, $new_blue);
- imagesetpixel($image, $x, $y, $color_pixel);
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement