PHP implementation of stackblur algorithm available?

前端 未结 1 951
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 06:39

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

This is my current attempt which is much too slow for large images and I do not want to use imagick.

<
相关标签:
1条回答
  • 2020-12-17 07:11

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

    Original image:

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

    20× Gaussian Blur (2.160 seconds):

    {
      $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

    Combination of scaling and Gaussian blur (0.237 seconds):

    {
      $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

    0 讨论(0)
提交回复
热议问题