File: 149_Imagick_sparseColorImage.phpt

package info (click to toggle)
php-imagick 3.4.4%2Bphp8.0%2B3.4.4-2%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,776 kB
  • sloc: ansic: 34,120; xml: 842; php: 188; pascal: 85; makefile: 2
file content (145 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
--TEST--
Test Imagick, sparseColorImage
--SKIPIF--
<?php
$imageMagickRequiredVersion=0x653;
require_once(dirname(__FILE__) . '/skipif.inc');
?>
--FILE--
<?php




function createGradientImage($width, $height, $colorPoints, $sparseMethod, $absolute = false) {

    $imagick = new \Imagick();
    $imagick->newImage($width, $height, "rgba(255, 255, 255, 1)");
    $imagick->setImageFormat("png");

    $barycentricPoints = array();

    foreach ($colorPoints as $colorPoint) {

        if ($absolute == true) {
            $barycentricPoints[] = $colorPoint[0];
            $barycentricPoints[] = $colorPoint[1];
        }
        else {
            $barycentricPoints[] = $colorPoint[0] * $width;
            $barycentricPoints[] = $colorPoint[1] * $height;
        }

        if (is_string($colorPoint[2])) {
            $imagickPixel = new \ImagickPixel($colorPoint[2]);
        }
        else if ($colorPoint[2] instanceof \ImagickPixel) {
            $imagickPixel = $colorPoint[2];
        }
        else{
            $errorMessage = sprintf(
                "Value %s is neither a string nor an ImagickPixel class. Cannot use as a color.",
                $colorPoint[2]
            );

            throw new \InvalidArgumentException(
                $errorMessage
            );
        }

        $red = $imagickPixel->getColorValue(\Imagick::COLOR_RED);
        $green = $imagickPixel->getColorValue(\Imagick::COLOR_GREEN);
        $blue = $imagickPixel->getColorValue(\Imagick::COLOR_BLUE);
        $alpha = $imagickPixel->getColorValue(\Imagick::COLOR_ALPHA);

        $barycentricPoints[] = $red;
        $barycentricPoints[] = $green;
        $barycentricPoints[] = $blue;
        $barycentricPoints[] = $alpha;
    }

    $imagick->sparseColorImage($sparseMethod, $barycentricPoints);

    return $imagick;
}

function renderImageBarycentric() {
    $points = array(
        array(0, 0, 'skyblue'),
        array(-1, 1, 'skyblue'),
        array(1, 1, 'black'),
    );
    $imagick = createGradientImage(600, 200, $points, \Imagick::SPARSECOLORMETHOD_BARYCENTRIC);
    $bytes = $imagick->getImageBlob();
    if (strlen($bytes) <= 0) { echo "Failed to generate image.";} 
    echo "Ok".PHP_EOL;
}
function renderImageVoronoi() {
    $points = array(
        array(0.30, 0.10, 'red'),
        array(0.10, 0.80, 'blue'),
        array(0.70, 0.60, 'lime'),
        array(0.80, 0.20, 'yellow'),
    );
    $imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_VORONOI);
    $bytes = $imagick->getImageBlob();
    if (strlen($bytes) <= 0) { echo "Failed to generate image.";} 
    echo "Ok".PHP_EOL;
}
function renderImageShepards() {
    $points = array(
            array(0.30, 0.10, 'red'),
            array(0.10, 0.80, "RGBA(0, 255, 0, 0.5)"),
            array(0.70, 0.60, "RGBA(0, 255, 0, 1)"),
            array(0.80, 0.20, 'yellow'),
        );
    $imagick = createGradientImage(600, 600, $points, \Imagick::SPARSECOLORMETHOD_SPEPARDS);
    $bytes = $imagick->getImageBlob();
    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
    echo "Ok".PHP_EOL;
}

function renderImageBilinear() {
    $points = array(
        array(0.30, 0.10, 'red'),
        array(0.10, 0.80, 'blue'),
        array(0.70, 0.60, 'lime'),
        array(0.80, 0.20, 'yellow'),
    );
    $imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_BILINEAR);
    $bytes = $imagick->getImageBlob();
    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
    echo "Ok".PHP_EOL;
}

try {
	renderImageBilinear() ;
}
catch (\Exception $e) {
	echo "renderImageBilinear failed ".$e->getMessage().PHP_EOL;
}
try {
	renderImageShepards();
}
catch (\Exception $e) {
	echo "renderImageShepards failed ".$e->getMessage().PHP_EOL;
}
try {
	renderImageVoronoi();
}
catch (\Exception $e) {
	echo "renderImageVoronoi failed ".$e->getMessage().PHP_EOL;
}
try {
	renderImageBarycentric();
}
catch (\Exception $e) {
	echo "renderImageBarycentric failed ".$e->getMessage().PHP_EOL;
}

?>
--EXPECTF--
Ok
Ok
Ok
Ok