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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?