PHP实现stackblur算法可用? [英] PHP implementation of stackblur algorithm available?

查看:392
本文介绍了PHP实现stackblur算法可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想造成图像模糊,需要一个更快的解决方案。

I am trying to blur an image and need a faster solution.

这是我当前的尝试是多的大型影像太慢了,我不希望使用imagick。

This is my current attempt which is much too slow for large images and I do not want to use 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);

}

有一个PHP实现stackblur或其它快速算法可用?

Is there a PHP implementation of stackblur or another fast algorithm available?

推荐答案

最简单的解决方案是应用模糊滤镜前缩放图像了。下面是一些例子:

The simple solution is to scale the image down before you apply the blur filter. Here are some examples:

原图:

20×高斯模糊(2.160秒):

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

组合比例和高斯模糊(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;
}

这篇关于PHP实现stackblur算法可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆