所谓图片模糊,就是通过滤镜让图片看上去没那么清晰,类似相机对焦点对不上,达到画面模糊的效果。这种图片处理手法会让图片更加唯美,有神秘感,所以在渲染气氛上非常合适。

下面给大家分享一个PHP批量图片模糊处理的源码,需要的小伙伴可以直接使用。

使用步骤:

1、把要批量模糊的图片先全部转化成jpg格式;

2、把转化好的jpg全部放到【xfm_img_15】里面,程序会读取这个目录里的所有图片;

3、运行【process_blur.php】,让程序开始执行,模糊算法处理时间比较长,要稍微等一下。你的图片越多,等待时间越长。

4、处理完成后的图片会保存到【blur】目录里。

最后,处理完成可以看到【process_blur.php】显示,处理完毕。

<?php
set_time_limit(0);
$config_dir = 'xfm_img_15';

$list_img = get_local_images();
foreach($list_img as $key=>$file) {
	var_dump($file);
	var_dump($key);
	xfm_blur($file);
}
echo '处理完毕';

function xfm_blur($file)
{
	$image = imagecreatefromjpeg($file);
	/* Get original image size */
	list($w, $h) = getimagesize($file);

	/* Create array with width and height of down sized images */
	$size = array(
		'sm' => array('w' => intval($w / 4), 'h' => intval($h / 4)),
		'md' => array('w' => intval($w / 2), 'h' => intval($h / 2))
	);

	/* Scale by 25% and apply Gaussian blur */
	$sm = imagecreatetruecolor($size['sm']['w'], $size['sm']['h']);
	imagecopyresampled($sm, $image, 0, 0, 0, 0, $size['sm']['w'], $size['sm']['h'], $w, $h);

	for ($x = 1; $x <= 40; $x++) {
		//imagefilter($sm, IMG_FILTER_GAUSSIAN_BLUR, 1);
	}

	imagefilter($sm, IMG_FILTER_SMOOTH, 11);
	imagefilter($sm, IMG_FILTER_BRIGHTNESS, 10);

	/* Scale result by 200% and blur again */
	$md = imagecreatetruecolor($size['md']['w'], $size['md']['h']);
	imagecopyresampled($md, $sm, 0, 0, 0, 0, $size['md']['w'], $size['md']['h'], $size['sm']['w'], $size['sm']['h']);
	imagedestroy($sm);

	for ($x = 1; $x <= 25; $x++) {
		imagefilter($md, IMG_FILTER_GAUSSIAN_BLUR, 111);
	}

	imagefilter($md, IMG_FILTER_SMOOTH, 11);
	imagefilter($md, IMG_FILTER_BRIGHTNESS, 10);

	/* Scale result back to original size */
	imagecopyresampled($image, $md, 0, 0, 0, 0, $w, $h, $size['md']['w'], $size['md']['h']);
	imagedestroy($md);

	// Apply filters of upsized image if you wish, but probably not needed
	//imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); 
	//imagefilter($image, IMG_FILTER_SMOOTH,99);
	//imagefilter($image, IMG_FILTER_BRIGHTNESS, 10);       

	//imagejpeg($image);
	//imagejpeg($image, 'good.jpg', 100);
	my_save_img($image, $file);
	imagedestroy($image);
}



function my_save_img($image, $file) {
	global $config_dir;
	$new_file = str_replace($config_dir, 'blured', $file);
	imagejpeg($image, $new_file, 100);
}


// 本地图片库 config/images/ 里面的 jpg 和 png 图片
function get_local_images() {
	if (isset($GLOBALS["config_dir"])) {
		$file_dir = $GLOBALS["config_dir"].DIRECTORY_SEPARATOR;
	}
	else {
		$file_dir = 'images'.DIRECTORY_SEPARATOR;
	}

	if (file_exists($file_dir)) {
		$dh  = opendir($file_dir);
		$tmp_arr = [];
		while (false !== ($filename = readdir($dh))) {
			if($filename !=".." && $filename !=".") {
				/*if(is_dir($dir."/".$filename)){
					$tmp_arr[$filename] = myScanDir($dir."/".$filename);
				}else{
					$tmp_arr[] = $filename;
				}*/
				if (strpos($filename, '.jpg') || strpos($filename, '.png')) {
					$tmp_arr[] = $file_dir.$filename;
				}
			}
	
		}
		closedir($dh);
	
		return $tmp_arr;
	}
	else {
		return array('config'.DIRECTORY_SEPARATOR.'thumb_2.jpg');
	}
}
Last modified: 2023年1月16日

Author