PHP Conference Japan 2024

Imagick::colorizeImage

(PECL imagick 2, PECL imagick 3)

Imagick::colorizeImage將填滿顏色與影像混合

描述

public Imagick::colorizeImage(mixed $colorize, mixed $opacity, bool $legacy = false): bool

將填滿顏色與影像中的每個像素混合。

參數

colorize

ImagickPixel 物件或包含色彩化色彩的字串

opacity

ImagickPixel 物件或包含不透明度值的浮點數。 1.0 為完全不透明,0.0 為完全透明。

回傳值

成功時回傳 true

錯誤/例外

發生錯誤時拋出 ImagickException。

變更日誌

版本 描述
PECL imagick 2.1.0 現在允許以字串表示色彩作為第一個參數,並以浮點數表示不透明度值作為第二個參數。先前的版本僅允許 ImagickPixel 物件。

範例

範例 1 Imagick::colorizeImage()

<?php
function colorizeImage($imagePath, $color, $opacity) {
$imagick = new \Imagick(realpath($imagePath));
$opacity = $opacity / 255.0;
$opacityColor = new \ImagickPixel("rgba(0, 0, 0, $opacity)");
$imagick->colorizeImage($color, $opacityColor);
header("Content-Type: image/jpg");
echo
$imagick->getImageBlob();
}

?>

新增註解

使用者貢獻的註解 6 則註解

8
Alex Lokhman [VisioN]
10 年前
如果您正在尋找用純色填滿影像並保留背景透明度的解決方案,以下是一種方法

<?php
$im
= new Imagick('image.png');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
$im->setImageBackgroundColor('color');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
$im->writeImage('output.png');
$im->destroy();
?>
3
olav at redwall dot ee
10 年前
為了改進「php at lfbittencourt dot com」的解決方案,這個解決方案會混合合成的顏色,並將不透明度納入考量。

<?php
class YourImagick extends Imagick
{
public function
colorize($color, $alpha = 1, $composite_flag = Imagick::COMPOSITE_COLORIZE)
{
$draw = new ImagickDraw();

$draw->setFillColor($color);

$geometry = $this->getImageGeometry();
$width = $geometry['width'];
$height = $geometry['height'];

$draw->rectangle(0, 0, $width, $height);

$temporary = new Imagick();
$temporary->setBackgroundColor(new ImagickPixel('transparent'));
$temporary->newImage($width, $height, new ImagickPixel('transparent'));
$temporary->setImageFormat('png32');
$temporary->drawImage($draw);

$alphaChannel = $this->clone();
$alphaChannel->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
$alphaChannel->negateImage(false, Imagick::CHANNEL_ALL);
$this->setImageClipMask($alphaChannel);

$clone = $this->clone();
$clone->compositeImage($temporary, $composite_flag, 0, 0);
$clone->setImageOpacity($alpha);

$this->compositeImage($clone, Imagick::COMPOSITE_DEFAULT, 0, 0);
}
}
?>
7
php at lfbittencourt dot com
12 年前
您是否想要具有真實不透明度控制的色彩覆蓋?試試這個

<?php

class YourImagick extends Imagick
{
public function
colorize($color, $alpha = 1)
{
$draw = new ImagickDraw();

$draw->setFillColor($color);

if (
is_float($alpha)) {
$draw->setFillAlpha($alpha);
}

$geometry = $this->getImageGeometry();
$width = $geometry['width'];
$height = $geometry['height'];

$draw->rectangle(0, 0, $width, $height);

$this->drawImage($draw);
}
}

?>

如何使用

<?php

$imagick
= new YourImagick('example.png');

$imagick->colorize('#ffcc00', 0.35);

header('Content-type: image/png');

echo
$source;

?>
1
adrien at unik dot solutions
2 年前
「olav at redwall dot ee」發布的解決方案會在每個非透明形狀的外部添加黑色邊框。

這是我的改進版本

<?php
public function colorize($color, $alpha = 1)
{
$geometry = $this->getImageGeometry();
$width = $geometry['width'];
$height = $geometry['height'];

$draw = new ImagickDraw;
$draw->setFillColor($color);
$draw->rectangle(0, 0, $width, $height);

$temporary = new Imagick;
$temporary->setBackgroundColor(new ImagickPixel('transparent'));
$temporary->newImage($width, $height, new ImagickPixel('transparent'));
$temporary->setImageFormat('png32');
$temporary->drawImage($draw);
$temporary->compositeImage($this, Imagick::COMPOSITE_COPYOPACITY, 0, 0);

$this->setImageArtifact('compose:args', ($alpha * 100) . '%,100%');
$this->compositeImage($temporary, Imagick::COMPOSITE_DISSOLVE, 0, 0);
}
?>
0
talkol at gmail dot com
12 年前
當您使用具有 alpha 通道(例如透明 png)的圖像時,值為 1.0 會傳回完全透明的圖像,但值為 1 則可以正常運作。
-1
lsmartinez at gmail dot com
15 年前
最簡單的範例

<?php
$nombre
= '001-4-0043.jpg';
$img = new Imagick($nombre);
$img->negateImage(false);
//$pixblu = new ImagickPixel('#000040');
$img->colorizeImage('#0000b0',1.0);
header('content-type: image/jpeg');
echo
$img;
?>
To Top