1--TEST--
2Test Tutorial, imagickCompositeGen
3--SKIPIF--
4<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5--FILE--
6<?php
7
8$contrast = 10;
9$blendMidpoint = 0.5;
10
11function generateBlendImage($height, $overlap, $contrast = 10, $midpoint = 0.5) {
12    $imagick = new \Imagick();
13    $imagick->newPseudoImage($height, $overlap, 'gradient:black-white');
14    $quantum = $imagick->getQuantum();
15    $imagick->sigmoidalContrastImage(true, $contrast, $midpoint * $quantum);
16
17    return $imagick;
18}
19
20
21function mergeImages($outputSize, $overlap, $contrast = 10, $blendMidpoint = 0.5, $horizontal = true) {
22
23    $images = array();
24    $newImageWidth = 0;
25    $newImageHeight = 0;
26
27    if ($horizontal == true) {
28        $resizeWidth = 0;
29        $resizeHeight = $outputSize;
30    }
31    else {
32        $resizeWidth = $outputSize;
33        $resizeHeight = 0;
34    }
35
36    $blendWidth = 0;
37
38    $srcImages = array(1, 2, 3);
39
40    foreach ($srcImages as $srcImage) {
41        $nextImage = new \Imagick();
42        $nextImage->newPseudoImage(640, 480, "magick:logo");
43
44
45        $nextImage->resizeImage($resizeWidth, $resizeHeight, \Imagick::FILTER_LANCZOS, 0.5);
46
47        if ($horizontal == true) {
48            $newImageWidth += $nextImage->getImageWidth();
49            $blendWidth = $nextImage->getImageHeight();
50        }
51        else {
52            //$newImageWidth = $nextImage->getImageWidth();
53            $blendWidth = $nextImage->getImageWidth();
54            $newImageHeight += $nextImage->getImageHeight();
55        }
56
57        $images[] = $nextImage;
58    }
59
60    if ($horizontal == true) {
61        $newImageWidth -= $overlap * (count($srcImages) - 1);
62        $newImageHeight = $outputSize;
63    }
64    else {
65        $newImageWidth = $outputSize;
66        $newImageHeight -= $overlap * (count($srcImages) - 1);
67    }
68
69    if ($blendWidth == 0) {
70        throw new \Exception("Failed to read source images");
71    }
72
73    $fadeLeftSide = generateBlendImage($blendWidth, $overlap, $contrast, $blendMidpoint);
74
75    if ($horizontal == true) {
76        //We are placing the images horizontally.
77        $fadeLeftSide->rotateImage('black', -90);
78    }
79
80    //Fade out the left part - need to negate the mask to
81    //make math correct
82    $fadeRightSide = clone $fadeLeftSide;
83    $fadeRightSide->negateimage(false);
84
85    //Create a new canvas to render everything in to.
86    $canvas = new \Imagick();
87    $canvas->newImage($newImageWidth, $newImageHeight, new \ImagickPixel('black'));
88
89    $count = 0;
90
91    $imagePositionX = 0;
92    $imagePositionY = 0;
93
94    /** @var $image \Imagick */
95    foreach ($images as $image) {
96        $finalBlending = new \Imagick();
97        $finalBlending->newImage($image->getImageWidth(), $image->getImageHeight(), 'white');
98
99        if ($count != 0) {
100            $finalBlending->compositeImage($fadeLeftSide, \Imagick::COMPOSITE_ATOP, 0, 0);
101        }
102
103        $offsetX = 0;
104        $offsetY = 0;
105
106        if ($horizontal == true) {
107            $offsetX = $image->getImageWidth() - $overlap;
108        }
109        else {
110            $offsetY = $image->getImageHeight() - $overlap;
111        }
112
113        if ($count != count($images) - 1) {
114            $finalBlending->compositeImage($fadeRightSide, \Imagick::COMPOSITE_ATOP, $offsetX, $offsetY);
115        }
116
117        $image->compositeImage($finalBlending, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
118        $canvas->compositeimage($image, \Imagick::COMPOSITE_BLEND, $imagePositionX, $imagePositionY);
119
120        if ($horizontal == true) {
121            $imagePositionX = $imagePositionX + $image->getImageWidth() - $overlap;
122        }
123        else {
124            $imagePositionY = $imagePositionY + $image->getImageHeight() - $overlap;
125        }
126        $count++;
127    }
128
129    return $canvas;
130}
131
132function imagickCompositeGen($contrast = 10, $blendMidpoint = 0.5) {
133
134    $size = 160;
135
136    //Load the images
137    $output = mergeImages(
138        $size,
139        0.2 * $size, //overlap
140        $contrast,
141        $blendMidpoint,
142        true);
143
144    //$output = generateBlendImage(200, 200, 5, 0.5);
145    $output->setImageFormat('png');
146
147    $bytes = $output->getImageBlob();
148    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
149}
150
151imagickCompositeGen($contrast = 10, $blendMidpoint = 0.5) ;
152echo "Ok";
153?>
154--EXPECTF--
155Ok