实现代码,封装成类
首先下载一张要切割的大图放到指定的目录中
<?php
/**
* Created by PhpStorm.
* User: 奇葩天地网
* Date: 19-12-27
* Time: 下午16:22
*/
//处理图像分割8等分,每等份加链接
class dopic
{
public $path = './static/file/'; //大图路径
public $filename = './qietu/datu.jpg'; //大图名字
public $newpath = './static/result/'; //新的小图存放目录
public $exNum = array(1, 2, 3, 4, 5, 6, 7, 8); //每等份图的名字
public $templatedir = './static/'; //html模版目录
public function __construct()
{
ini_set('memory_limit', '220M');
}
//分割图片8等份
public function explodepic()
{
$file = $this->path . $this->filename; //大图文件地址
$filePre = explode('.', $this->filename); //获取大图文件名
$newfile = $this->newpath . $filePre[0]; //获取小图存放路径,以大图文件名为名称创建一个目录
self::create_dir($newfile); //创建小图存放路径
$ext = $this->getExt($this->filename);
//获取大图的尺寸
list($width, $height, $type, $attr) = getimagesize($file);
//算出每等份尺寸,
//不被整除,保证不留白
$minW = $width;
$minHx = ceil($height / count($this->exNum));
//分别截取出小图
//传入图片路径和后缀
$bigpic = $this->setImage($file, $ext);
//新建一个图像画布
$smallpic = imagecreatetruecolor($minW, $minHx); //新建一个图像
for ($i = 0; $i < count($this->exNum); $i++) {
if ($i == (count($this->exNum) - 1)) {
$minH = $height - $minHx * $i;
$smallpic = imagecreatetruecolor($minW, $minH); //新建一个图像
} else {
$minH = $minHx;
$smallpic = imagecreatetruecolor($minW, $minHx); //新建一个图像
}
//单独处理png的图片
if ($ext == 'png' || $ext == 'gif') {
//拾取白色
$white = imagecolorallocate($smallpic, 255, 255, 255);
//把画布染成白色
imagefill($smallpic, 0, 0, $white);
//把图片中白色设置为透明色
imagecolortransparent($smallpic, $white);
}
imagecopy($smallpic, $bigpic, 0, 0, 0, ($i * $minHx), $minW, $minH); //复制图像一部分
$this->setPic($smallpic, $newfile . '/' . $this->exNum[$i] . '.' . $ext, $ext);
//imagejpeg($smallpic, $newfile . '/' . $this->exNum[$i] . '.jpg', 100); //输出小图
}
return $newfile;
}
//获取文件名部分
public function getfilename($filename)
{
$res = explode('.', $filename);
return $res[0];
}
//创建目录
public function create_dir($dir)
{
return is_dir($dir) or (self::create_dir(dirname($dir)) and mkdir($dir, 0777));
}
/**
*函数:getExt()
* @param:$filename文件名称
* @return:返回上传文件的扩展名称
*/
public function getExt($filename)
{
$array = explode('.', $filename);
$ext = array_pop($array);
return strtolower($ext);
}
/**
* @desc 保存生成的图片
* @access private setPic()
* @param $imgSource 图像的资源
* @param $path 要保存的图片路径地址和名称
* @param $ext 要保存路径的扩展名称
*/
private function setPic($imgSource, $path, $ext)
{
switch (strtolower($ext)) {
case 'jpg':
case 'jpeg':
return imagejpeg($imgSource, $path, 100);
break;
case 'gif' :
return imagegif($imgSource, $path);
break;
case 'png' :
return imagepng($imgSource, $path, 9);
break;
default :
$this->getErrorInfo("暂不支持你的扩展名称,请尝试jpg,gif,png");
}
}
/**
* @desc 保存生成的图片
* @access private setPic()
* @param $imgSource 图像的资源
* @param $path 要保存的图片路径地址和名称
* @param $ext 要保存路径的扩展名称
*/
private function setImage($imgSource, $ext)
{
switch (strtolower($ext)) {
case 'jpg':
case 'jpeg':
return imagecreatefromjpeg($imgSource);
break;
case 'gif' :
return imagecreatefromgif($imgSource);
break;
case 'png' :
return imagecreatefrompng($imgSource);
break;
default :
$this->getErrorInfo("暂不支持你的扩展名称,请尝试jpg,gif,png");
}
}
/**
*函数:getErrorInfo()
* @param:$error错误信息
* @return:返回错误信息
*/
public function getErrorInfo($error)
{
echo '<script type="text/javascript">alert("错误信息:' . $error . '");</script>';
exit();
}
}
@session_start();
$filename = 'qipadatu.jpg';
//测试
$res = new dopic();
$res->filename = $filename;
$newfile = $res->explodepic();
echo "<pre>";
print_r($newfile);
//header('Location:index.php');
?>
在浏览器中执行:http://localhost/xiutu/qietu.php
会看到生成的目录中,多了8张切割好的小图
查看切好的图片
<?php
$url='http://localhost/xiutu/static/result/qipadatu/';
for($i=1;$i<9;$i++){
echo '<img src="'.$url.$i.'.jpg" alt="">';
}
?>
打开网址:http://localhost/xiutu/images.php
就能看到如下图:
查看源码