是否可以使用正弦和余弦等三角函数扭曲图像,使其呈波浪状。
如果是这样,如何。
PHP是首选语言,但它可以是任何...
是否可以使用正弦和余弦等三角函数扭曲图像,使其呈波浪状。
如果是这样,如何。
PHP是首选语言,但它可以是任何...
是的,这是可能的。图像只是像素的二维数组,可以自由地重新组织它们。一种简单的方法是通过一些失真函数从原始图像中创建新图像和采样像素。
$original = read_image_pixels(); // using GD or some other way
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
// we are adding $height and taking modulo
// to insure that $distorted_y is positive and less then $height.
$distorted_y = ($y + round(10*sin($x/20)) + $height) % $height;
$distorted[$x][$y] = $original[$x][$distorted_y];
}
}
编辑:这可以进一步概括。许多熟悉的效果(如模糊和不锐化)都是卷积过滤器。GameDev 文章中对它们进行了很好的解释。我们可以将上述 sin-distortion 视为具有空间可变内核(系数矩阵)的卷积滤波器。
使用phadej的答案我得到了一个解决方案......
图片是这样的...
编码 -
<?php
header("Content-type: image/png");
$im = imagecreatefrompng('pic.png');
$newim = imagecreatetruecolor(imagesx($im),imagesy($im));
for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
$rgba = imagecolorsforindex($im, imagecolorat($im, $x, $y));
$col = imagecolorallocate($newim, $rgba["red"], $rgba["green"], $rgba["blue"]);
$distorted_y = ($y + round(100*sin($x/50)) + imagesy($im)) % imagesy($im);
imagesetpixel($newim, $x, $distorted_y, $col);
}
}
imagepng($newim);
?>
输出
这在很大程度上取决于您的图像如何工作。我不会说 PHP,所以这是一个通用的解决方案。
如果我们可以移动单个像素,那么在概念上最简单的方法就是说
y old = 像素的旧 y 位置
y new = 像素的新 y 位置
x = 像素的 x 位置
L = 图像的长度(以像素为单位)
N = 要应用的三角函数周期数(即正弦波数)
然后我们只是遍历图像。对于 x 的每个值,我们移动 y 像素:
y新= y旧* (1+sin(N π x/L)) / 2