Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you please document the extract_size_and_pixels_with_imagick function used in the example? #4

Closed
uandco opened this issue Mar 29, 2023 · 2 comments

Comments

@uandco
Copy link

uandco commented Mar 29, 2023

I guess it uses some kind of ImagickPixelIterator implementation but that makes the example very hard to use without that missing piece.

Thanks!

@SRWieZ
Copy link
Owner

SRWieZ commented Apr 4, 2023

Sort of, the goal is to simulate what getImageData javascript function returns.

Here is a commented version using ImagickPixelIterator, hope it help you understand 🙂
(not tested, but should work)

/**
 *
 * Extracts the size (width and height) and pixel data (RGBA) from an image using the Imagick library
 * and ImagickPixelIterator.
 * @param  string  $content  The binary data of the image to be processed.
 * @return array An array containing the width, height, and pixel data of the image.
 * @throws \ImagickPixelIteratorException
 */
function extract_size_and_pixels_with_imagick_pixel_iterator($content): array
{
    // Create a new Imagick object and read the image from the binary data provided.
    $image = new Imagick();
    $image->readImageBlob($content);

    // Get the width and height of the image.
    $width = $image->getImageWidth();
    $height = $image->getImageHeight();

    // Create a new ImagickPixelIterator to iterate through the pixels of the image.
    $pixelIterator = $image->getPixelIterator();

    $pixels = [];

    // Loop through the rows of the image using the iterator.
    foreach ($pixelIterator as $row => $pixelRow) {
        // Loop through the pixels in the current row.
        foreach ($pixelRow as $column => $pixel) {
            // Get the RGBA color values of the pixel.
            $colors = $pixel->getColor(2);

            // Append the RGBA values to the pixels array.
            $pixels[] = $colors['r'];
            $pixels[] = $colors['g'];
            $pixels[] = $colors['b'];
            $pixels[] = $colors['a'];
        }
    }

    // Return an array containing the width, height, and pixel data.
    return [$width, $height, $pixels];
}

@SRWieZ SRWieZ closed this as completed Apr 4, 2023
@uandco
Copy link
Author

uandco commented Apr 5, 2023

Thanks a lot, worked a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants