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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染