English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Implementation of Method Example to Generate Blurred Images

This example demonstrates how to generate blurred images in PHP. Shared for everyone's reference, as follows:

<63;php
class image_blur{
/**
  * Image Gaussian blur (suitable for png/jpg/gif format)
  * @param $srcImg Original image
  * @param $savepath Save path
  * @param $savename Save name
  * @param $position Blur level
  *
  *Based on the extension of Martijn Frazer's code, thank you Martijn Frazer
  */
 public function gaussian_blur($srcImg,$savepath=null,$savename=null,$blurFactor=3{
  $gdImageResource=$this->image_create_from_ext($srcImg);
  $srcImgObj=$this->blur($gdImageResource,$blurFactor);
  $temp = pathinfo($srcImg);
  $name = $temp['basename'];
  $path = $temp['dirname'];
  $exte = $temp['extension'];
  $savename = $savename &63; $savename : $name;
  $savepath = $savepath &63; $savepath : $path;
  $savefile = $savepath .'/'. $savename;
  $srcinfo = @getimagesize($srcImg);
  switch ($srcinfo[2}) {
   case 1: imagegif($srcImgObj, $savefile); break;
   case 2: imagejpeg($srcImgObj, $savefile); break;
   case 3: imagepng($srcImgObj, $savefile); break;
   default: return 'Save failed'; //Save failed
  }
  return $savefile;
  imagedestroy($srcImgObj);
 }
 /**
 * Strong Blur
 *
 * @param $gdImageResource Image resource
 * @param $blurFactor   Optional blur level
 * Optional blur level 0 uses 3Default exceeds5At that time, it is extremely blurred
 * @return GD image image resource type
 * @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482
 */
 private function blur($gdImageResource, $blurFactor = 3)
 {}}
  // blurFactor has to be an integer
  $blurFactor = round($blurFactor);
  $originalWidth = imagesx($gdImageResource);
  $originalHeight = imagesy($gdImageResource);
  $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
  $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));
  // For the first run, the previous image is the original input
  $prevImage = $gdImageResource;
  $prevWidth = $originalWidth;
  $prevHeight = $originalHeight;
  // scale way down and gradually scale back up, blurring all the way
  for($i = 0; $i < $blurFactor; $i += 1)
  {}}
   // determine dimensions of next image
   $nextWidth = $smallestWidth * pow(2, $i);
   $nextHeight = $smallestHeight * pow(2, $i);
   // resize previous image to next size
   $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
   imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,
    $nextWidth, $nextHeight, $prevWidth, $prevHeight);
   // apply blur filter
   imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);
   // now the new image becomes the previous image for the next step
   $prevImage = $nextImage;
   $prevWidth = $nextWidth;
   $prevHeight = $nextHeight;
  }
  // scale back to original size and blur one more time
  imagecopyresized($gdImageResource, $nextImage,
  0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
  imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);
  // clean up
  imagedestroy($prevImage);
  // return result
  return $gdImageResource;
 }
 private function image_create_from_ext($imgfile)
 {}}
  $info = getimagesize($imgfile);
  $im = null;
  switch ($info[2}) {
  case 1: $im=imagecreatefromgif($imgfile); break;
  case 2: $im=imagecreatefromjpeg($imgfile); break;
  case 3: $im=imagecreatefrompng($imgfile); break;
  }
  return $im;
 }
}
$image_blur = new image_blur();
$image_blur->gaussian_blur("./1.jpg",null,null,3);
?>

Original image effect:

Effect after generating blurred image:

Readers who are interested in more about PHP-related content can check the special topics on this site: 《Summary of PHP Graphics and Image Operation Skills》、《Summary of PHP File Operation》、《Comprehensive Skills of PHP Array (Array) Operation》、《PHP Basic Syntax Tutorial》、《Summary of PHP Operation and Operator Usage》、《PHP Object-Oriented Program Design Tutorial》、《Summary of PHP Network Programming Skills》、《Summary of PHP String (string) Usage》、《php+Introduction to MySQL Database Operation Tutorial》and《Summary of Common PHP Database Operation Skills》

I hope the content described in this article will be helpful to everyone's PHP program design.

Declaration: The content of this article is from the network, owned by the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like