douhui4699
douhui4699
2013-07-19 08:26
浏览 79
已采纳

访问php中的gd图像资源作为矩阵

I am working on a simple script which calculates a fingerprint of an image file to detect resized copies of same images (and copies in different format). Everything works pretty fine, except that to access image color values i need to read every single pixel of an image using imagecolorat() like this:

function image_chksum (&$src_image, $x1, $y1, $x2, $y2) {
        $chksum = 0;
        for ($x = $x1; $x < $x2; $x++)
                for ($y = $y1; $y < $y2; $y++) {
                        $rgb = imagecolorsforindex ($src_image, imagecolorat($src_image, $x, $y));
                        $sum = ($rgb["red"] + $rgb["green"] + $rgb["blue"] + $rgb["alpha"]);
                        $chksum += $sum;
                }
        return $chksum;
}

(Don't pay attention to what it does, this is not the whole algorithm, it's just an example)

So. looking at this ugly code a question comes up - can i just access a gd image resource ($src_image in my case) and use it as an array or someth. I've seen this method working in python using PIL/PILLOW.

This problem also appears when checking for alpha png images. Unfortunately methods described here: How to check a PNG for grayscale/alpha color type? does not work for non-truecolor images, so i have to fallback to checking every single pixel (again). A first-in-mind idea is to check every N-th pixel, because transparency is usually used in areas, not single pixels, but that's very uncool.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doy57007
    doy57007 2013-07-24 22:10
    已采纳

    The QB extension might be the solution you're looking for. It lets you access a GD image as a multidimensional array. Depending on which is more convenient, you may declare an image to be either an array of uint8[4], where each pixel is an array of four 8-bit integer, or an array of float32[4], where each pixel is an array of four 32-bit floating point numbers, (with values between 0 and 1).

    Performance should be at least one order of magnitude better than regular PHP. Enabling native compilation makes your code run five times faster still.

    点赞 评论

相关推荐