ImagickKernel::fromMatrix

(PECL imagick >= 3.3.0)

ImagickKernel::fromMatrix从 2D 值矩阵创建内核

描述

public static ImagickKernel::fromMatrix(array $matrix, array $origin = ?): ImagickKernel

从 2D 值矩阵创建内核。每个值应该是一个浮点数(如果元素应该被使用)或者 'false'(如果元素应该被跳过)。对于两个维度上大小为奇数的矩阵,原点像素将默认为内核的中心。对于所有其他内核大小,必须指定原点像素。

参数

array

定义内核的值矩阵(即二维数组)。每个元素应该是一个浮点值,或者 FALSE,如果该元素不应该被内核使用。

array

内核的哪个元素应该用作原点像素。例如,对于一个 3x3 矩阵,将原点指定为 [2, 2] 将指定右下角元素应该作为原点像素。

返回值

生成的 ImagickKernel。

示例

示例 #1 ImagickKernel::fromMatrix()

<?php

function renderKernel(ImagickKernel $imagickKernel) {
$matrix = $imagickKernel->getMatrix();

$imageMargin = 20;

$tileSize = 20;
$tileSpace = 4;
$shadowSigma = 4;
$shadowDropX = 20;
$shadowDropY = 0;

$radius = ($tileSize / 2) * 0.9;

$rows = count($matrix);
$columns = count($matrix[0]);

$imagickDraw = new \ImagickDraw();

$imagickDraw->setFillColor('#afafaf');
$imagickDraw->setStrokeColor('none');

$imagickDraw->translate($imageMargin, $imageMargin);
$imagickDraw->push();

ksort($matrix);

foreach (
$matrix as $row) {
ksort($row);
$imagickDraw->push();
foreach (
$row as $cell) {
if (
$cell !== false) {
$color = intval(255 * $cell);
$colorString = sprintf("rgb(%f, %f, %f)", $color, $color, $color);
$imagickDraw->setFillColor($colorString);
$imagickDraw->rectangle(0, 0, $tileSize, $tileSize);
}
$imagickDraw->translate(($tileSize + $tileSpace), 0);
}
$imagickDraw->pop();
$imagickDraw->translate(0, ($tileSize + $tileSpace));
}

$imagickDraw->pop();

$width = ($columns * $tileSize) + (($columns - 1) * $tileSpace);
$height = ($rows * $tileSize) + (($rows - 1) * $tileSpace);

$imagickDraw->push();
$imagickDraw->translate($width/2 , $height/2);
$imagickDraw->setFillColor('rgba(0, 0, 0, 0)');
$imagickDraw->setStrokeColor('white');
$imagickDraw->circle(0, 0, $radius - 1, 0);
$imagickDraw->setStrokeColor('black');
$imagickDraw->circle(0, 0, $radius, 0);
$imagickDraw->pop();

$canvasWidth = $width + (2 * $imageMargin);
$canvasHeight = $height + (2 * $imageMargin);

$kernel = new \Imagick();
$kernel->newPseudoImage(
$canvasWidth,
$canvasHeight,
'canvas:none'
);

$kernel->setImageFormat('png');
$kernel->drawImage($imagickDraw);

/* create drop shadow on it's own layer */
$canvas = $kernel->clone();
$canvas->setImageBackgroundColor(new \ImagickPixel('rgb(0, 0, 0)'));
$canvas->shadowImage(100, $shadowSigma, $shadowDropX, $shadowDropY);

$canvas->setImagePage($canvasWidth, $canvasHeight, -5, -5);
$canvas->cropImage($canvasWidth, $canvasHeight, 0, 0);

/* composite original text_layer onto shadow_layer */
$canvas->compositeImage($kernel, \Imagick::COMPOSITE_OVER, 0, 0);
$canvas->setImageFormat('png');

return
$canvas;
}

function
createFromMatrix() {
$matrix = [
[
0.5, 0, 0.2],
[
0, 1, 0],
[
0.9, 0, false],
];

$kernel = \ImagickKernel::fromMatrix($matrix);

return
$kernel;
}

function
fromMatrix() {
$kernel = createFromMatrix();
$imagick = renderKernel($kernel);

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

?>

添加备注

用户贡献的备注

此页面没有用户贡献的备注。
To Top