gpt4 book ai didi

php - 如何使用 PHP 将漂亮的径向透明渐变应用于图像?

转载 作者:可可西里 更新时间:2023-10-31 22:42:11 25 4
gpt4 key购买 nike

如何指定一幅图像并在其呈放射状淡出的位置应用放射状透明渐变。我没有安装 Imagemagick。

边际示例:

Gradient Transparent image fade PHP

最佳答案

简介

我认为你应该安装 Imagemagick,因为你想要的是一个简单的 vignette 效果,你可以很容易地使用 ImageMagic (convert input.jpg -background black -vignette 70x80 output.png)无需循环处理大图像时可能非常慢的每个像素

原始图片

$file = __DIR__ . "/golf.jpg";

enter image description here

效果 1

$image = new imagick($file);
$image->vignetteImage(20, 20, 40, - 20);
header("Content-Type: image/png");
echo $image;

enter image description here

效果 2

$image = new imagick($file);
$image->vignetteImage(100, 100, 200, 200);
header("Content-Type: image/png");
echo $image;

enter image description here

GD 小插图

好吧,如果你被迫使用 GB ... 使用可以使用这个 cool vignette script

function vignette($im) {
$width = imagesx($im);
$height = imagesy($im);

$effect = function ($x, $y, &$rgb) use($width, $height) {
$sharp = 0.4; // 0 - 10 small is sharpnes,
$level = 0.7; // 0 - 1 small is brighter
$l = sin(M_PI / $width * $x) * sin(M_PI / $height * $y);
$l = pow($l, $sharp);
$l = 1 - $level * (1 - $l);
$rgb['red'] *= $l;
$rgb['green'] *= $l;
$rgb['blue'] *= $l;
};

for($x = 0; $x < imagesx($im); ++ $x) {
for($y = 0; $y < imagesy($im); ++ $y) {
$index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($im, $index);
$effect($x, $y, $rgb);
$color = imagecolorallocate($im, $rgb['red'], $rgb['green'], $rgb['blue']);

imagesetpixel($im, $x, $y, $color);
}
}
return (true);
}

更快的 GD 小插图方法

GD Filter testing 中使用了更好的方法将是......创建一个面具和覆盖

    $overlay = 'vignette_white.png';
$png = imagecreatefrompng($overlay);
imagecopyresampled($filter, $png, 0, 0, 0, 0, $width, $height, $width, $height);

唯一的缺点是图片必须和mask一样大小才能让效果看起来很酷

结论

如果这就是您所说的 radial transparent gradient 的意思,那么我建议您使用 ImageMagic 至少图片很可爱的女士。

关于php - 如何使用 PHP 将漂亮的径向透明渐变应用于图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com