0

I have a site that has to read up to 50,000 individual pixels from an image. I need a way to do this fast. Right now, reading each pixel individually, at 22,000 pixels, takes almost 10 seconds. I know GD is not the best way to do this. What PHP image library would be the fastest way to read tons of pixels in a short time?

dda
  • 6,030
  • 2
  • 25
  • 34
PitaJ
  • 12,969
  • 6
  • 36
  • 55
  • 5
    "PHP" and "fast" don't cohabit in the same sentence... The first question is: and what are you going to do with these pixels? I can't imagine PHP being up to loading 50K x 3 bytes at any reasonable speed and then manipulating them... You might want to defer the whole process to a C process. – dda Nov 29 '12 at 16:51
  • https://stackoverflow.com/questions/13791207/better-way-to-get-map-of-all-pixels-of-an-image-with-gd is sort of a dup of this Q. I provided a pure-PHP answer in which a 6.5MPixel image is read on my machine in about 3.6 sec, which, at around 800 times faster than your test, seems to be getting into the realms of reasonable to me, though it's admittedly not comparing like with anything very close to like. – Dewi Morgan Feb 02 '16 at 03:57

3 Answers3

1

You could try Imagick. Although I'm not sure if it'll actually be faster. Here's an example:

<?php
$imagick = new Imagick("my_image.jpeg");
$iterator = $imagick->getPixelIterator();

foreach($iterator as $row => $pixels) {
    foreach ($pixels as $column => $pixel) {
        // Do something with $pixel
    }
    $iterator->syncIterator();
}
?>
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
1

You can use the GraphicsMagick.

From their documentation :

GraphicsMagick is the swiss army knife of image processing. Comprised of 282K physical lines (according to David A. Wheeler's SLOCCount) of source code in the base package (or 964K including 3rd party libraries) it provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.

Image processing is multi-threaded (see the multi-thread benchmark results) using OpenMP so that CPU-bound tasks scale linearly as processor cores are added. OpenMP support requires compilation with GCC 4.2 (or later), or use of any C compiler supporting at least the OpenMP 2.0 specification.

It's forked from ImageMagick's source code on 2002. It has a pecl php extension named GMagick and considerably faster at executing image processing operations from the command line than ImageMagick.

Update: GraphicsMagick is faster than ImageMagick doesn't means GMagick is better than Imagick. Library choice is depends by your requirements. Here a nice SO answer about this subject : https://stackoverflow.com/a/5282269/199593

Community
  • 1
  • 1
edigu
  • 9,878
  • 5
  • 57
  • 80
0

Gmagick is a an extension to make it easy to use GraphicsMagick, which itself is a fork of ImageMagick.

They claim that it's faster, but I haven't used it myself.

daveyfaherty
  • 4,585
  • 2
  • 27
  • 42