Imagick
PHP Manual

Imagick::sparseColorImage

(バージョン情報なし。おそらく SVN 版にしか存在しないでしょう)

Imagick::sparseColorImage — 色を補間する

説明

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

数値を含む配列を引数として受け取り、画像全体にわたって見つかった座標の色を補間します。補間には sparse_method を用います。 このメソッドは、ImageMagick バージョン 6.4.5 以降で Imagick をコンパイルした場合に使用可能です。

パラメータ

SPARSE_METHOD

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 is used by other examples. 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;
}

?>


Imagick
PHP Manual