PHP Conference Japan 2024

Imagick::sparseColorImage

(PECL imagick 2 >= 2.3.0, PECL imagick 3)

Imagick::sparseColorImage插入顏色

說明

public Imagick::sparseColorImage(int $SPARSE_METHOD, array $arguments, int $channel = Imagick::CHANNEL_DEFAULT): bool

給定包含數值的 arguments 陣列,此方法使用 sparse_method 在整個影像上插入在這些座標找到的顏色。如果 Imagick 是針對 ImageMagick 6.4.5 或更新版本編譯的,則此方法可用。

參數

SPARSE_METHOD

參考此稀疏方法常數列表

arguments

包含座標的陣列。陣列格式為 array(1,1, 2,45)

channel

提供任何適用於您通道模式的通道常數。要應用於多個通道,請使用位元運算子組合通道常數。預設為 Imagick::CHANNEL_DEFAULT。參考此通道常數列表

回傳值

成功時回傳 true

錯誤/例外

發生錯誤時拋出 ImagickException。

範例

範例 #1 SPARSECOLORMETHOD_BARYCENTRIC Imagick::sparseColorImage()

<?php
function renderImageBarycentric2() {
$points = [
[
0.30, 0.10, 'red'],
[
0.10, 0.80, 'blue'],
[
0.70, 0.60, 'lime'],
[
0.80, 0.20, 'yellow'],
];
$imagick = createGradientImage(
400, 400,
$points,
\Imagick::SPARSECOLORMETHOD_BARYCENTRIC
);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();
}

?>

範例 #2 SPARSECOLORMETHOD_BILINEAR Imagick::sparseColorImage()

<?php
function renderImageBilinear() {
$points = [[0.30, 0.10, 'red'], [0.10, 0.80, 'blue'], [0.70, 0.60, 'lime'], [0.80, 0.20, 'yellow'],];
$imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_BILINEAR);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();
}

?>

範例 #3 SPARSECOLORMETHOD_SPEPARDS Imagick::sparseColorImage() 函式

<?php
function renderImageShepards() {
$points = [
[
0.30, 0.10, 'red'],
[
0.10, 0.80, 'blue'],
[
0.70, 0.60, 'lime'],
[
0.80, 0.20, 'yellow'],
];
$imagick = createGradientImage(600, 600, $points, \Imagick::SPARSECOLORMETHOD_SPEPARDS);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();
}

?>

範例 #4 SPARSECOLORMETHOD_VORONOI Imagick::sparseColorImage() 函式

<?php
function renderImageVoronoi() {
$points = [
[
0.30, 0.10, 'red'],
[
0.10, 0.80, 'blue'],
[
0.70, 0.60, 'lime'],
[
0.80, 0.20, 'yellow'],
];
$imagick = createGradientImage(500, 500, $points, \Imagick::SPARSECOLORMETHOD_VORONOI);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();
}

?>

範例 #5 SPARSECOLORMETHOD_BARYCENTRIC Imagick::sparseColorImage()

<?php
function renderImageBarycentric() {
$points = [
[
0, 0, 'skyblue'],
[-
1, 1, 'skyblue'],
[
1, 1, 'black'],
];
$imagick = createGradientImage(600, 200, $points, \Imagick::SPARSECOLORMETHOD_BARYCENTRIC);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();
}

?>

範例 #6 createGradientImage 被其他範例使用。 Imagick::sparseColorImage()

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

$imagick = new \Imagick();
$imagick->newImage($width, $height, "white");
$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;
}

?>

新增註記

使用者貢獻的註記 1 則註記

3
aehtyb[]gmail.com
8 年前
對於那些好奇如何直接使用 sparseColorImage() 而不使用單獨的 createGradientImage() 函式的人…以下是所使用陣列的格式

陣列 (
[0] => 0 // X1(X 座標值 #1)
[1] => 0 // Y1(Y 座標值 #1)
[2] => 1 // R(紅色值介於 0 和 1 之間)
[3] => 0 // G(綠色值介於 0 和 1 之間)
[4] => 0 // B(藍色值,介於 0 和 1 之間)
[5] => 1 // A(Alpha 值,介於 0 和 1 之間)
[6] => 400 // X2
[7] => 0 // Y2
[8] => 0 // R(紅色值)
[9] => 1 // G(綠色值)
[10] => 0 // B(藍色值)
[11] => 1 // A(Alpha 值)
[12] => 0 // X3
[13] => 400 // Y3
[14] => 1 // R(紅色值)
[15] => 1 // G(綠色值)
[16] => 0 // B(藍色值)
[17] => 1 // A(Alpha 值)
[18] => 400 // X4
[19] => 400 // Y4
[20] => 0 // R(紅色值)
[21] => 0 // G(綠色值)
[22] => 1 // B(藍色值)
[23] => 1 // A(Alpha 值)
)

<?php

$imagick
= new Imagick();
$imagick->newImage(400,400,"white");
$imagick->setImageFormat("png");

$array = array(0,0,1,0,0,1,400,0,0,1,0,1,0,400,1,1,0,1,400,400,0,0,1,1);
$imagick->sparseColorImage(Imagick::SPARSECOLORMETHOD_BILINEAR,$array);

header("Content-Type: image/png");
echo
$imagick->getImageBlob();

?>
To Top