PHPCon Poland 2024


(PECL imagick 2, PECL imagick 3)

Imagick::compositeImageCompose une image avec une autre


public Imagick::compositeImage(
    Imagick $composite_object,
    int $composite,
    int $x,
    int $y,
    int $channel = Imagick::CHANNEL_DEFAULT
): bool

Compose une image dans une autre, à la position donné. Tout paramètre supplémentaire nécessaire à l'algorithme de composition doit être passé à setImageArtifact avec comme premier paramètre 'compose:args' et les données en second paramètre.

Liste de paramètres


Objet Imagick qui contient l'image composite


L'opérateur de composition. Voyez la liste des constantes d'opérateurs de composition


La colonne de position dans l'image composée


La ligne de position dans l'image composée


Fournit une constante de canal valide pour votre mode de canal. Pour utiliser plus d'un canal, combinez les constantes de type de canal en utilisant les opérateurs de bits. Reportez-vous à la liste des constantes de canal.

Valeurs de retour

Retourne true en cas de succès.


Exemple #1 Using Imagick::compositeImage():

Compose deux images avec la méthode de composition 'mathematics'.


// Equivalent à lancer la commande
// convert src1.png src2.png -compose mathematics -define compose:args="1,0,-0.5,0.5" -composite output.png

$src1 = new \Imagick("./src1.png");
$src2 = new \Imagick("./src2.png");

$src1->setImageArtifact('compose:args', "1,0,-0.5,0.5");
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);


Voir aussi

add a note

User Contributed Notes 4 notes

m dot roszka at textend dot net
16 years ago
Here is an example on how to compose two images into a single one. The Imagick class utilises the exception handling model introduced in PHP5 and thus we will do that as well. Let's presume, that we have a directory in our filesystem, which contains our program and the two images we want to operate on.

// Let's check whether we can perform the magick.
if (TRUE !== extension_loaded('imagick'))
throw new
Exception('Imagick extension is not loaded.');

// This check is an alternative to the previous one.
// Use the one that suits you better.
if (TRUE !== class_exists('Imagick'))
throw new
Exception('Imagick class does not exist.');

// Let's find out where we are.
$dir = dirname(__FILE__);

// Let's read the images.
$glasses = new Imagick();
if (
FALSE === $glasses->readImage($dir . '/glasses.png'))
throw new

$face = new Imagick();
if (
FALSE === $face->readImage($dir . '/face.jpg'))
throw new

// Let's put the glasses on (10 pixels from left, 20 pixels from top of face).
$face->compositeImage($glasses, Imagick::COMPOSITE_DEFAULT, 10, 20);

// Let's merge all layers (it is not mandatory).

// We do not want to overwrite face.jpg.
$face->setImageFileName($dir . '/face_and_glasses.jpg');

// Let's write the image.
if (FALSE == $face->writeImage())
throw new

catch (
Exception $e)
'Caught exception: ' . $e->getMessage() . "\n";

Also a couple more words on the Imagick::COMPOSITE_DEFAULT argument. The images we are composing together are separate layers. Not only can we put them in specific order, but we can also choose the way we want them to interfere with each other. And here comes the second argument of the compositeImage method. It can be given either as a constant or as the integer value of that constant. You can use the reflection API of PHP5 to get the list of them.

::export(new ReflectionClass('Imagick'));

Just look for COMPOSITE_* constants in the "Constants" section.
elizapinchley at google dot com
3 years ago
There has been changes to Imagick constants in new version. Please read new list of constants like this.


= new \Imagick();
$reflection_class = new ReflectionClass($img);


12 years ago
To copy the alpha channel from one image to another, you can do the following:


= new Imagick( "image1.png" );
$img2 = new Imagick( "image2.png" );

$img1->compositeImage( $img2, imagick::COMPOSITE_COPYOPACITY, 0, 0 );

header('Content-type: image/png');

giso at connectholland dot nl
14 years ago
You might need to set the colorspace the same when composing two images over each other

//Creating two Imagick object
$first = new Imagick('first.jpg');
$second = new Imagick('second.jpg');

// Set the colorspace to the same value
$first->setImageColorspace($second->getImageColorspace() );

//Second image is put on top of the first
$first->compositeImage($second, $second->getImageCompose(), 5, 5);

//new image is saved as final.jpg
To Top