PHP | Imagick importImagePixels() Function

The Imagick::importImagePixels() function is an inbuilt function in PHP which is used to import pixels from an array into an image.

Syntax:

bool Imagick::importImagePixels( int $x, int $y, int $width, int $height, string $map, 
                        int $storage, array $pixels )

Parameters: This function accepts seven parameters as mentioned above and described below:

  • $x: It specifies the image x position.
  • $y: It specifies the image y position.
  • $width: It specifies the image width.
  • $height: It specifies the image height.
  • $map: It specifies the map of pixel ordering as a string.
  • $storage: It specifies the pixel storage method which is an integer value corresponding to one PIXEL constants.
  • $pixels: It specifies the array of pixels.

List of PIXEL constants is given below:

  • imagick::PIXEL_CHAR (0)
  • imagick::PIXEL_DOUBLE (1)
  • imagick::PIXEL_FLOAT (2)
  • imagick::PIXEL_INTEGER (3)
  • imagick::PIXEL_LONG (4)
  • imagick::PIXEL_QUANTUM (5)
  • imagick::PIXEL_SHORT (6)

Return Value: This function returns TRUE on success.



Exceptions: This function throws ImagickException on error.

Below given programs illustrate the Imagick::importImagePixels() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick();
  
// Generate array of pixels
$pixels =
   array_merge(array_pad(array(), 15000, 0),
               array_pad(array(), 15000, 255));
  
$imagick->newImage(100, 100, 'white');
  
// Import the pixels into image.
$imagick->importImagePixels(0, 0, 100, 100, "RGB", imagick::PIXEL_FLOAT, $pixels);
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick;
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick();
  
// Generate array of pixels
$pixels =
   array_merge(array_pad(array(), 5000, 0),
               array_pad(array(), 5000, 255),
               array_pad(array(), 5000, 0),
               array_pad(array(), 5000, 255),
               array_pad(array(), 5000, 0),
               array_pad(array(), 5000, 255));
  
$imagick->newImage(100, 100, 'white');
  
// Import the pixels into image.
$imagick->importImagePixels(0, 0, 100, 100, "RGB", imagick::PIXEL_FLOAT, $pixels);
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick;
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.importimagepixels.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.