1--TEST--
2Test Imagick, sparseColorImage
3--SKIPIF--
4<?php
5$imageMagickRequiredVersion=0x653;
6require_once(dirname(__FILE__) . '/skipif.inc');
7?>
8--FILE--
9<?php
10
11
12
13
14function createGradientImage($width, $height, $colorPoints, $sparseMethod, $absolute = false) {
15
16    $imagick = new \Imagick();
17    $imagick->newImage($width, $height, "rgba(255, 255, 255, 1)");
18    $imagick->setImageFormat("png");
19
20    $barycentricPoints = array();
21
22    foreach ($colorPoints as $colorPoint) {
23
24        if ($absolute == true) {
25            $barycentricPoints[] = $colorPoint[0];
26            $barycentricPoints[] = $colorPoint[1];
27        }
28        else {
29            $barycentricPoints[] = $colorPoint[0] * $width;
30            $barycentricPoints[] = $colorPoint[1] * $height;
31        }
32
33        if (is_string($colorPoint[2])) {
34            $imagickPixel = new \ImagickPixel($colorPoint[2]);
35        }
36        else if ($colorPoint[2] instanceof \ImagickPixel) {
37            $imagickPixel = $colorPoint[2];
38        }
39        else{
40            $errorMessage = sprintf(
41                "Value %s is neither a string nor an ImagickPixel class. Cannot use as a color.",
42                $colorPoint[2]
43            );
44
45            throw new \InvalidArgumentException(
46                $errorMessage
47            );
48        }
49
50        $red = $imagickPixel->getColorValue(\Imagick::COLOR_RED);
51        $green = $imagickPixel->getColorValue(\Imagick::COLOR_GREEN);
52        $blue = $imagickPixel->getColorValue(\Imagick::COLOR_BLUE);
53        $alpha = $imagickPixel->getColorValue(\Imagick::COLOR_ALPHA);
54
55        $barycentricPoints[] = $red;
56        $barycentricPoints[] = $green;
57        $barycentricPoints[] = $blue;
58        $barycentricPoints[] = $alpha;
59    }
60
61    $imagick->sparseColorImage($sparseMethod, $barycentricPoints);
62
63    return $imagick;
64}
65
66function renderImageBarycentric() {
67    $points = array(
68        array(0, 0, 'skyblue'),
69        array(-1, 1, 'skyblue'),
70        array(1, 1, 'black'),
71    );
72    $imagick = createGradientImage(600, 200, $points, \Imagick::SPARSECOLORMETHOD_BARYCENTRIC);
73    $bytes = $imagick->getImageBlob();
74    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
75    echo "Ok".PHP_EOL;
76}
77function renderImageVoronoi() {
78    $points = array(
79        array(0.30, 0.10, 'red'),
80        array(0.10, 0.80, 'blue'),
81        array(0.70, 0.60, 'lime'),
82        array(0.80, 0.20, 'yellow'),
83    );
84    $imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_VORONOI);
85    $bytes = $imagick->getImageBlob();
86    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
87    echo "Ok".PHP_EOL;
88}
89function renderImageShepards() {
90    $points = array(
91            array(0.30, 0.10, 'red'),
92            array(0.10, 0.80, "RGBA(0, 255, 0, 0.5)"),
93            array(0.70, 0.60, "RGBA(0, 255, 0, 1)"),
94            array(0.80, 0.20, 'yellow'),
95        );
96    $imagick = createGradientImage(600, 600, $points, \Imagick::SPARSECOLORMETHOD_SPEPARDS);
97    $bytes = $imagick->getImageBlob();
98    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
99    echo "Ok".PHP_EOL;
100}
101
102function renderImageBilinear() {
103    $points = array(
104        array(0.30, 0.10, 'red'),
105        array(0.10, 0.80, 'blue'),
106        array(0.70, 0.60, 'lime'),
107        array(0.80, 0.20, 'yellow'),
108    );
109    $imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_BILINEAR);
110    $bytes = $imagick->getImageBlob();
111    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
112    echo "Ok".PHP_EOL;
113}
114
115try {
116	renderImageBilinear() ;
117}
118catch (\Exception $e) {
119	echo "renderImageBilinear failed ".$e->getMessage().PHP_EOL;
120}
121try {
122	renderImageShepards();
123}
124catch (\Exception $e) {
125	echo "renderImageShepards failed ".$e->getMessage().PHP_EOL;
126}
127try {
128	renderImageVoronoi();
129}
130catch (\Exception $e) {
131	echo "renderImageVoronoi failed ".$e->getMessage().PHP_EOL;
132}
133try {
134	renderImageBarycentric();
135}
136catch (\Exception $e) {
137	echo "renderImageBarycentric failed ".$e->getMessage().PHP_EOL;
138}
139
140?>
141--EXPECTF--
142Ok
143Ok
144Ok
145Ok