Rumah > pembangunan bahagian belakang > tutorial php > PHP如何实现生成模糊图片

PHP如何实现生成模糊图片

小云云
Lepaskan: 2023-03-18 08:18:02
asal
2341 orang telah melayarinya

本文主要介绍了PHP实现生成模糊图片的方法,涉及php高斯算法实现图片模糊功能相关实现技巧,需要的朋友可以参考下,希望能帮助到大家。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

<?php

class image_blur{

/**

  * 图片高斯模糊(适用于png/jpg/gif格式)

  * @param $srcImg 原图片

  * @param $savepath 保存路径

  * @param $savename 保存名字

  * @param $positon 模糊程度

  *

  *基于Martijn Frazer代码的扩充, 感谢 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[&#39;basename&#39;];

  $path = $temp[&#39;dirname&#39;];

  $exte = $temp[&#39;extension&#39;];

  $savename = $savename ? $savename : $name;

  $savepath = $savepath ? $savepath : $path;

  $savefile = $savepath .&#39;/&#39;. $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 &#39;保存失败&#39;; //保存失败

  }

  return $savefile;

  imagedestroy($srcImgObj);

 }

 /**

 * Strong Blur

 *

 * @param $gdImageResource 图片资源

 * @param $blurFactor   可选择的模糊程度

 * 可选择的模糊程度 0使用 3默认 超过5时 极其模糊

 * @return GD image 图片资源类型

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

?>

Salin selepas log masuk

原图效果:

生成模糊图片后的效果:

大家学会了吗?赶紧动手尝试一下吧。

相关推荐:

css3模糊图片_html/css_WEB-ITnose

js图片模糊切换显示特效的方法_javascript技巧

css实现的图片模糊效果_javascript技巧

Atas ialah kandungan terperinci PHP如何实现生成模糊图片. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan