Graham
Graham

Reputation: 309

Remove black edges from existing image using the GD library

I'm downloading an image that already has black edges. Note: this isn't a result of me resizing an image. How can I use the GD library to detect and remove these black edges?

enter image description here

enter image description here

UPDATE

This is the cropped image using the script enter image description here

Upvotes: 3

Views: 2845

Answers (1)

jeremy
jeremy

Reputation: 10057

I was able to come up with a time-consuming fix to this. Do the images being stored need to be stored with those black borders? It'd be much better if you could run every image with the black borders through the following script (using php to loop through every image in the directory) and let php override the old, black-bordered image with the new, borderless image.

The approach I took was to create 4 loops:

  1. To look at black borders on the right (loop through x -> loop through y)
  2. To look at black borders on the left (loop through x -> loop through y)
  3. To look at black borders on the bottom (loop through y -> loop through x)
  4. To look at black borders on the top (loop through y -> loop through x)

Now, each of these loops had another loop in them which would loop through the other coordinate (ie., x->y or y->x). If the inner loop found that one of the pixels lying on the outer loop's line wasn't black, it broke the whole look. If it didn't find that, it would increase one to the counter.

At the end, we simply create a new image with the new dimensions and copy from the new to the old one.

<?php
$image_path = "jcMHt.jpg";

$jpg = imagecreatefromjpeg($image_path);
$black = array("red" => 0, "green" => 0, "blue" => 0, "alpha" => 0);

$removeLeft = 0;
for($x = 0; $x < imagesx($jpg); $x++) {
    for($y = 0; $y < imagesy($jpg); $y++) {
        if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
            break 2;
        }
    }
    $removeLeft += 1;
}

$removeRight = 0;
for($x = imagesx($jpg)-1; $x > 0; $x--) {
    for($y = 0; $y < imagesy($jpg); $y++) {
        if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
            break 2;
        }
    }
    $removeRight += 1;
}

$removeTop = 0;
for($y = 0; $y < imagesy($jpg); $y++) {
    for($x = 0; $x < imagesx($jpg); $x++) {
        if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
            break 2;
        }
    }
    $removeTop += 1;
}

$removeBottom = 0;
for($y = imagesy($jpg)-1; $y > 0; $y--) {
    for($x = 0; $x < imagesx($jpg); $x++) {
        if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
            break 2;
        }
    }
    $removeBottom += 1;
}

$cropped = imagecreatetruecolor(imagesx($jpg) - ($removeLeft + $removeRight), imagesy($jpg) - ($removeTop + $removeBottom));
imagecopy($cropped, $jpg, 0, 0, $removeLeft, $removeTop, imagesx($cropped), imagesy($cropped));

header("Content-type: image/jpeg");
imagejpeg($cropped); //change to `imagejpeg($cropped, $image_path);` to save
imagedestroy($cropped);
imagedestroy($jpg);

Upvotes: 5

Related Questions