0

Im looking to convert a Javascript function that has been written to detect the non-transparent area of an image.

The initial question and answer is here.

The iteration is easily converted to PHP, however im having trouble obtaining the same image data as written here: (getImageData and Uint32Array)

var idata = ctx.getImageData(0, 0, w, h),      // get image data for canvas
buffer = idata.data,                       // get buffer (unnes. step)
buffer32 = new Uint32Array(buffer.buffer), // get a 32-bit representation
x, y,                                      // iterators
x1 = w, y1 = h, x2 = 0, y2 = 0;

Is there a method in which I can get the same data with PHP? The Javascript method given has browser compatibility issues in which I have solved by writing a node application. I would prefer to have a PHP method if at all possible.

Community
  • 1
  • 1
Mike
  • 1,436
  • 9
  • 16
  • php (at least gd) doesn't handle pixels like in javascript. You typically would have to loop over the x/y and use [imagecolorat](http://www.php.net/imagecolorat) and the x/y coords to get a specific pixel's color data. – Jonathan Kuhn May 14 '14 at 20:30
  • Would you care to elaborate with a working example? Ive tried using imagecolorat but im very unfamiliar with the results and am unsure about sorting the RGB/Alpha bits to iterate correctly. – Mike May 14 '14 at 20:36
  • There is an example in the manual of how to use `imagecolorat()`. You would just get the height/width of the image and loop over and get the color at each pixel with a nested for loop. – Jonathan Kuhn May 14 '14 at 20:42

1 Answers1

0
$img = imagecreatefrompng($img_file);

    if (!ctype_xdigit($hex)) $hex = imagecolorat($img, 0,0);
    $b_top = $b_lft = 0;
    $b_rt = $w1 = $w2 = imagesx($img);
    $b_btm = $h1 = $h2 = imagesy($img);

    do {
        //top
        for(; $b_top < $h1; ++$b_top) {
            for($x = 0; $x < $w1; ++$x) {
                if(imagecolorat($img, $x, $b_top) != $hex) {
                    break 2;
                }
            }
        }

        // stop if all pixels are trimmed
        if ($b_top == $b_btm) {
            $b_top = 0;
            $code = 2;
            break 1;
        }

        // bottom
        for(; $b_btm >= 0; --$b_btm) {
            for($x = 0; $x < $w1; ++$x) {
                if(imagecolorat($img, $x, $b_btm-1) != $hex) {
                    break 2;
                }
            }
        }

        // left
        for(; $b_lft < $w1; ++$b_lft) {
            for($y = $b_top; $y <= $b_btm; ++$y) {
                if(imagecolorat($img, $b_lft, $y) != $hex) {
                    break 2;
                }
            }
        }

        // right
        for(; $b_rt >= 0; --$b_rt) {
            for($y = $b_top; $y <= $b_btm; ++$y) {
                if(imagecolorat($img, $b_rt-1, $y) != $hex) {
                    break 2;
                }
            }

        }

        $w2 = $b_rt - $b_lft;
        $h2 = $b_btm - $b_top;
        $code = ($w2 < $w1 || $h2 < $h1) ? 1 : 0;
    } while (0);
Mike
  • 1,436
  • 9
  • 16