Open In App

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:

List of PIXEL constants is given below:



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


Article Tags :