8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: Imagick::sparseColorImage

来源:网络转载 浏览:30675次 时间:2023-09-01
Imagick::spliceImage » « Imagick::solarizeImage
  • PHP 手册
  • 函数参考
  • 图像生成和处理
  • ImageMagick
  • Imagick

Imagick::sparseColorImage

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

Imagick::sparseColorImage — Interpolates colors

说明

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

Given the arguments array containing numeric values this method interpolates the colors found at those coordinates across the whole image using sparse_method. 此方法在Imagick基于ImageMagick 6.4.5以上版本编译时可用。

参数

SPARSE_METHOD

Refer to this list of sparse method constants

arguments

An array containing the coordinates. The array is in format array(1,1, 2,45)

channel

Provide any channel constant that is valid for your channel mode. To apply to more than one channel, combine channel constants using bitwise operators. 缺省为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;
}

?>
add a note

User Contributed Notes 1 note

up down 3 aehtyb[]gmail.com6 years ago For those curious how to use sparseColorImage() directly without the seperate createGradientImage() function.. here is the format of the array used:

Array (
[0]  => 0   // X1 (X coordinate value #1)
[1]  => 0   // Y1 (Y coordinate value #1)
[2]  => 1   // R (red value between 0 and 1)
[3]  => 0   // G (green value between 0 and 1)
[4]  => 0   // B (blue value between 0 and 1)
[5]  => 1   // A (alpha value between 0 and 1)
[6]  => 400 // X2
[7]  => 0   // Y2
[8]  => 0   // R
[9]  => 1   // G
[10] => 0   // B
[11] => 1   // A
[12] => 0   // X3
[13] => 400 // Y3
[14] => 1   // R
[15] => 1   // G
[16] => 0   // B
[17] => 1   // A
[18] => 400 // X4
[19] => 400 // Y4
[20] => 0   // R
[21] => 0   // G
[22] => 1   // B
[23] => 1   // A
)

<?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();

?>
add a note

官方地址:https://www.php.net/manual/en/imagick.sparsecolorimage.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net