PHPb实现的stackblur算法可用吗?

Ste*_*hlf 2 php algorithm image-processing

我试图模糊图像,需要更快的解决方案.

这是我目前的尝试,对于大型图像而言太慢了,我不想使用想象力.

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);

}
Run Code Online (Sandbox Code Playgroud)

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

squ*_*age 13

简单的解决方案是在应用模糊滤镜之前缩小图像.这里有些例子:

原始图片:

一只猫的照片(公共领域:来自Wikimedia Commons的Longhair_Tabby_JaJa.jpg)

20×高斯模糊(2.160秒):

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

应用高斯模糊滤波器20次的结果

缩放和高斯模糊的组合(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;
}
Run Code Online (Sandbox Code Playgroud)

应用图像缩放和高斯模糊组合的结果