记录,一个用PHP切图九宫格的函数,可自定义3*3,4*4等多种切割

function segmentation($w, $h, $dir,$filename)
{
	//获取原图wdith和height
	list($width, $height) = getimagesize($dir.$filename);
	//分割后大小
	$newwidth = floor($width / $w);
	$newheight = floor($height / $h);
	//复制
	$source = imagecreatefrompng($dir.$filename);
	for ($i = 0; $i < $h; $i++) {
		for ($j = 0; $j < $w; $j++) {
			$startY = $i * $newheight;
			$startX = $j * $newwidth;
			$thumb = ImageCreateTrueColor($newwidth, $newheight);
			//结果对象,来源对象,结果对象x起点,结果对象y起点,来源对象x起点,来源对象y起点,需要的width,需要的height
			imagecopy($thumb, $source, 0, 0, $startX, $startY, $newwidth,  $newheight);
			$n++;
			imagepng($thumb, "$dir$n.png", 9);
		}
	}
}
segmentation(横格数, 纵格数, 图片目录,图片名称)
本文链接:https://www.dnwfb.com/5887.html,转载请注明出处。
0

评论0

没有账号? 注册  忘记密码?