gpt4 book ai didi

可用的 stackblur 算法的 PHP 实现?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:26:23 27 4
gpt4 key购买 nike

我正在尝试模糊图像并需要更快的解决方案。

这是我目前的尝试,对于大图像来说太慢了,我不想使用 imagick。

public function blur($filename, $extension, $factor = 20){
if (strtolower($extension) === "jpg" || strtolower($extension) === "jpeg") $image = imagecreatefromjpeg($filename);
if (strtolower($extension) === "png") $image = imagecreatefrompng($filename);

for ($x=1; $x<=$factor; $x++)
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
imagejpeg($image, "$filename.blur.$extension");
imagedestroy($image);

}

是否有可用的 stackblur 或其他快速算法的 PHP 实现?

最佳答案

简单的解决方案是在应用模糊滤镜之前缩小图像。以下是一些示例:

原图:

Photo of a cat (public domain: Longhair_Tabby_JaJa.jpg from Wikimedia Commons)

20× 高斯模糊(2.160 秒):

{
$start = microtime(true);
for ($x=0; $x<20; $x++) {
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
}
$end = microtime(true);
$howlong = $end - $start;
}

Result of applying Gaussian blur filter 20 times

缩放和高斯模糊的组合(0.237 秒):

{
$start = microtime(true);

/* Scale by 25% and apply Gaussian blur */
$s_img1 = imagecreatetruecolor(160,120);
imagecopyresampled($s_img1, $image, 0, 0, 0, 0, 160, 120, 640, 480);
imagefilter($s_img1, IMG_FILTER_GAUSSIAN_BLUR);

/* Scale result by 200% and blur again */
$s_img2 = imagecreatetruecolor(320,240);
imagecopyresampled($s_img2, $s_img1, 0, 0, 0, 0, 320, 240, 160, 120);
imagedestroy($s_img1);
imagefilter($s_img2, IMG_FILTER_GAUSSIAN_BLUR);

/* Scale result back to original size and blur one more time */
imagecopyresampled($image, $s_img2, 0, 0, 0, 0, 640, 480, 320, 240);
imagedestroy($s_img2);
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
$end = microtime(true);
$howlong = $end - $start;
}

Result of applying a combination of image scaling and Gaussian blur

关于可用的 stackblur 算法的 PHP 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261869/

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