Skip to content
Related Articles

Related Articles

PHP | Imagick importImagePixels() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Nov, 2019
View Discussion
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!