<?php /** * Created by PhpStorm. * User: Administrator * Date: 14-3-26 * Time: 下午6:31 */ //处理图像分割8等分,每等份加链接 class dopic { public $path = './static/file/'; //大图路径 public $filename = ''; //大图名字 public $newpath = './static/result/'; //新的小图存放目录 public $exNum = array(1, 2, 3, 4, 5, 6, 7, 8); //每等份图的名字 public $htmlpath = './static/html/'; //生成html的目录 public $templatedir = './static/'; //html模版目录 public $templatefile = 'edm.htm'; //html模版目录 public $links = ''; //链接地址 public $htmlfilename = ''; //生成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 = imagecreatefromjpeg($file); $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; } imagecopy($smallpic, $bigpic, 0, 0, 0, ($i * $minHx), $minW, $minH); //复制图像一部分 imagejpeg($smallpic, $newfile . '/' . $this->exNum[$i] . '.jpg', 100); //输出小图 } return $newfile; } //创建目录 public function create_dir($dir) { return is_dir($dir) or (self::create_dir(dirname($dir)) and mkdir($dir, 0777)); } //生成html public function mkHtml() { $str = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . ltrim($this->templatedir, '.') . $this->templatefile); $newstr = str_replace("#areplace", $this->links, $str); $fname = $this->getfilename($this->filename); $newstr = str_replace("#filename", $fname, $newstr); self::create_dir($this->htmlpath . $fname); file_put_contents($this->htmlpath . $fname . '/index.html', $newstr); return $this->htmlpath . $fname . '/index.html'; } //获取文件名部分 public function getfilename($filename) { $res = explode('.', $filename); return $res[0]; } /** *函数: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); break; case 'gif' : return imagegif($imgSource, $path); break; case 'png' : return imagepng($imgSource, $path); 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(); //if (isset($_SESSION['images']) && !empty($_SESSION['images']) && isset($_POST['links'])) { // //获取提交的表单数据 // $links = $_POST['links']; // if ($links == '') { // echo '<script>alert("链接不能为空");</script>'; // exit; // } // $filename = $_SESSION['images']; // //测试 // $res = new dopic(); // $res->filename = $filename; // $res->links = $links; // $newfile = $res->explodepic(); // $html = $res->mkHtml(); // $_SESSION['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $html; // header('Location:index.php'); //} ?>
//调用
public function actionImg() { // include_once dirname(__FILE__).'/ImgController.php'; // $old = dirname(__FILE__) . '/20230322111620.jpg'; // $new = dirname(__FILE__) . '/ccc.jpg'; // $ic = new ImgController($old, $new); // $ic->Crop(1200, 1200, 5); // $ic->SaveImage(); // //$ic->SaveAlpha();将补白变成透明像素保存 // $ic->destory(); // echo 'ok';exit; //测试 include_once dirname(__FILE__).'/dopic.php'; $res = new dopic(); $res->newpath = dirname(__FILE__).'/';; $res->path = dirname(__FILE__).'/';; // $res->filename = 'ccc.jpg,aaa.jpg,bbb.jpg,ccc.jpg,ddd.jpg,eee.jpg,fff.jpg,www.jpg,ppp.jpg';; // $res->filename = 'aaa.jpg';; $res->filename = '20230322111620.jpg'; $newfile = $res->explodepic(); echo 'ok';exit; // $html = $res->mkHtml(); // $_SESSION['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $html; // header('Location:index.php'); }