Imagick::compositeImage

(PECL imagick 2, PECL imagick 3)

Imagick::compositeImage β€” НакладываСт ΠΎΠ΄Π½ΠΎ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π΄Ρ€ΡƒΠ³ΠΎΠ΅

ОписаниС

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

НакладываСт ΠΎΠ΄Π½ΠΎ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π΄Ρ€ΡƒΠ³ΠΎΠ΅ с ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹ΠΌ смСщСниСм. Π›ΡŽΠ±Ρ‹Π΅ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρ‹, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Π΅ для Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌΠ° налоТСния, слСдуСт ΠΏΠ΅Ρ€Π΅Π΄Π°Π²Π°Ρ‚ΡŒ Π² setImageArtifact с 'compose:args' Π² качСствС ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° ΠΈ Π΄Π°Π½Π½Ρ‹Ρ… Π² качСствС Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π°.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

composite_object

ΠžΠ±ΡŠΠ΅ΠΊΡ‚ Imagick, содСрТащий Π½Π°ΠΊΠ»Π°Π΄Ρ‹Π²Π°Π΅ΠΌΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅.

compose

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ налоТСния. Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ ΠšΠΎΠ½ΡΡ‚Π°Π½Ρ‚Ρ‹ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° налоТСния.

x

Π‘ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ столбца Π½Π°ΠΊΠ»Π°Π΄Ρ‹Π²Π°Π΅ΠΌΠΎΠ³ΠΎ изобраТСния.

y

Π‘ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ строки Π½Π°ΠΊΠ»Π°Π΄Ρ‹Π²Π°Π΅ΠΌΠΎΠ³ΠΎ изобраТСния.

channel

Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ Π»ΡŽΠ±ΡƒΡŽ константу CHANNEL, которая ΠΏΠΎΠ΄Ρ…ΠΎΠ΄ΠΈΡ‚ для вашСго Ρ€Π΅ΠΆΠΈΠΌΠ° ΠΊΠ°Π½Π°Π»Π°. Для примСнСния Π±ΠΎΠ»Π΅Π΅ Ρ‡Π΅ΠΌ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΊΠ°Π½Π°Π»Ρƒ ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΠΈΡ‚Π΅ константы Ρ‚ΠΈΠΏΠ° CHANNEL с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΏΠΎΠ±ΠΈΡ‚ΠΎΠ²Ρ‹Ρ… ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠ². ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ΡΡŒ ΠΊ этому списку констант CHANNEL.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π² случаС ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Ρ‹ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Imagick::compositeImage():

НалоТСниС Π΄Π²ΡƒΡ… ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ 'матСматичСского' ΠΌΠ΅Ρ‚ΠΎΠ΄Π° налоТСния

<?php

// Π­ΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½Ρ‚Π½ΠΎ запуску ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹
// 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->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$src1->setImageArtifact('compose:args', "1,0,-0.5,0.5");
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
$src1->writeImage("./output.png");

?>

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • Imagick::setImageArtifact() - УстанавливаСт Π°Ρ€Ρ‚Π΅Ρ„Π°ΠΊΡ‚ изобраТСния
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
6
m dot roszka at textend dot net ΒΆ
18 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.

<?php
try
{
    // 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 Exception();
    }

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

    // 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).
    $face->flattenImages();

    // 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 Exception();
    }
}

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

exit(0);
?>
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.

<?php
Reflection::export(new ReflectionClass('Imagick'));
?>

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

<?php

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

die(var_dump($reflection_class->getConstants()));

?>
up
0
Iddles ΒΆ
14 years ago
To copy the alpha channel from one image to another, you can do the following:

<?php

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

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

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

?>
up
0
giso at connectholland dot nl ΒΆ
16 years ago
You might need to set the colorspace the same when composing two images over each other

<?php
//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
$first->writeImage('final.jpg');
?>