Open In App

PHP | Imagick importImagePixels() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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;
?>


Output:

Program 2:




<?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;
?>


Output:

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



Last Updated : 28 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads