📄 class.thumbhandler.php
字号:
* @param integer $img_h 目标高度 */ function _setNewImgSize($img_w, $img_h=null) { $num = func_num_args(); if(1 == $num) { $this->img_scale = $img_w;// 宽度作为比例 $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2; $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2; // 源文件起始坐标 $this->src_x = 0; $this->src_y = 0; $this->copy_w = $this->src_w; $this->copy_h = $this->src_h; // 目标尺寸 $this->dst_w = $this->fill_w + $this->img_border_size*2; $this->dst_h = $this->fill_h + $this->img_border_size*2; } if(2 == $num) { $fill_w = (int)$img_w - $this->img_border_size*2; $fill_h = (int)$img_h - $this->img_border_size*2; if($fill_w < 0 || $fill_h < 0) { die("图片边框过大,已超过了图片的宽度"); } $rate_w = $this->src_w/$fill_w; $rate_h = $this->src_h/$fill_h; switch($this->cut_type) { case 0: // 如果原图大于缩略图,产生缩小,否则不缩小 if($rate_w < 1 && $rate_h < 1) { $this->fill_w = (int)$this->src_w; $this->fill_h = (int)$this->src_h; } else { if($rate_w >= $rate_h) { $this->fill_w = (int)$fill_w; $this->fill_h = round($this->src_h/$rate_w); } else { $this->fill_w = round($this->src_w/$rate_h); $this->fill_h = (int)$fill_h; } } $this->src_x = 0; $this->src_y = 0; $this->copy_w = $this->src_w; $this->copy_h = $this->src_h; // 目标尺寸 $this->dst_w = $this->fill_w + $this->img_border_size*2; $this->dst_h = $this->fill_h + $this->img_border_size*2; break; // 自动裁切 case 1: // 如果图片是缩小剪切才进行操作 if($rate_w >= 1 && $rate_h >=1) { if($this->src_w > $this->src_h) { $src_x = round($this->src_w-$this->src_h)/2; $this->setSrcCutPosition($src_x, 0); $this->setRectangleCut($fill_h, $fill_h); $this->copy_w = $this->src_h; $this->copy_h = $this->src_h; } elseif($this->src_w < $this->src_h) { $src_y = round($this->src_h-$this->src_w)/2; $this->setSrcCutPosition(0, $src_y); $this->setRectangleCut($fill_w, $fill_h); $this->copy_w = $this->src_w; $this->copy_h = $this->src_w; } else { $this->setSrcCutPosition(0, 0); $this->copy_w = $this->src_w; $this->copy_h = $this->src_w; $this->setRectangleCut($fill_w, $fill_h); } } else { $this->setSrcCutPosition(0, 0); $this->setRectangleCut($this->src_w, $this->src_h); $this->copy_w = $this->src_w; $this->copy_h = $this->src_h; } // 目标尺寸 $this->dst_w = $this->fill_w + $this->img_border_size*2; $this->dst_h = $this->fill_h + $this->img_border_size*2; break; // 手工裁切 case 2: $this->copy_w = $this->fill_w; $this->copy_h = $this->fill_h; // 目标尺寸 $this->dst_w = $this->fill_w + $this->img_border_size*2; $this->dst_h = $this->fill_h + $this->img_border_size*2; break; default: break; } } // 目标文件起始坐标 $this->start_x = $this->img_border_size; $this->start_y = $this->img_border_size; } /** * 检查水印图是否大于生成后的图片宽高 */ function _isFull() { Return ( $this->mask_w + $this->mask_offset_x > $this->fill_w || $this->mask_h + $this->mask_offset_y > $this->fill_h) ?true:false; } /** * 检查水印图是否超过原图 */ function _checkMaskValid() { if( $this->mask_w + $this->mask_offset_x > $this->src_w || $this->mask_h + $this->mask_offset_y > $this->src_h) { die("水印图片尺寸大于原图,请缩小水印图"); } } /** * 取得图片类型 * * @param string $file_path 文件路径 */ function _getImgType($file_path) { $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp"); if(file_exists($file_path)) { $img_info = @getimagesize ($file_path); if(isset($type_list[$img_info[2]])) { Return $type_list[$img_info[2]]; } } else { die("文件不存在,不能取得文件类型!"); } } /** * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求 * php版本大于4.1.0 * * @param string $img_type 文件类型 */ function _checkValid($img_type) { if(!array_key_exists($img_type, $this->all_type)) { Return false; } } /** * 按指定路径生成目录 * * @param string $path 路径 */ function _mkdirs($path) { $adir = explode('/',$path); $dirlist = ''; $rootdir = array_shift($adir); if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir)) { @mkdir($rootdir); } foreach($adir as $key=>$val) { if($val!='.'&&$val!='..') { $dirlist .= "/".$val; $dirpath = $rootdir.$dirlist; if(!file_exists($dirpath)) { @mkdir($dirpath); @chmod($dirpath,0777); } } } } /** * 垂直翻转 * * @param string $src 图片源 */ function _flipV($src) { $src_x = $this->getImgWidth($src); $src_y = $this->getImgHeight($src); $new_im = imagecreatetruecolor($src_x, $src_y); for ($y = 0; $y < $src_y; $y++) { imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1); } $this->h_src = $new_im; } /** * 水平翻转 * * @param string $src 图片源 */ function _flipH($src) { $src_x = $this->getImgWidth($src); $src_y = $this->getImgHeight($src); $new_im = imagecreatetruecolor($src_x, $src_y); for ($x = 0; $x < $src_x; $x++) { imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y); } $this->h_src = $new_im; }}?>使用实例: <?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); $t->setSrcImg("img/test.jpg");$t->setDstImg("tmp/new_test.jpg");$t->setMaskImg("img/test.gif");$t->setMaskPosition(1);$t->setMaskImgPct(80);$t->setDstImgBorder(4,"#dddddd"); // 指定缩放比例$t->createImg(300,200);?><?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); // 基本使用$t->setSrcImg("img/test.jpg");$t->setMaskWord("test");$t->setDstImgBorder(10,"#dddddd"); // 指定缩放比例$t->createImg(50);?><?phpequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); // 基本使用$t->setSrcImg("img/test.jpg");$t->setMaskWord("test"); // 指定固定宽高$t->createImg(200,200);?><?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); $t->setSrcImg("img/test.jpg");$t->setDstImg("tmp/new_test.jpg");$t->setMaskWord("test"); // 指定固定宽高$t->createImg(200,200);?><?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); $t->setSrcImg("img/test.jpg"); // 指定字体文件地址$t->setMaskFont("c:/winnt/fonts/arial.ttf");$t->setMaskFontSize(20);$t->setMaskFontColor("#ffff00");$t->setMaskWord("test3333333");$t->setDstImgBorder(99,"#dddddd");$t->createImg(50); ?><?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); $t->setSrcImg("img/test.jpg");$t->setMaskOffsetX(55);$t->setMaskOffsetY(55);$t->setMaskPosition(1);//$t->setMaskPosition(2);//$t->setMaskPosition(3);//$t->setMaskPosition(4);$t->setMaskFontColor("#ffff00");$t->setMaskWord("test"); // 指定固定宽高$t->createImg(50);?><?phprequire_once('lib/thumb.class.php'); $t = new ThumbHandler(); $t->setSrcImg("img/test.jpg");$t->setMaskFont("c:/winnt/fonts/simyou.ttf");$t->setMaskFontSize(20);$t->setMaskFontColor("#ffffff");$t->setMaskTxtPct(20);$t->setDstImgBorder(10,"#dddddd");$text = "中文";$str = mb_convert_encoding($text, "UTF-8", "gb2312");$t->setMaskWord($str);$t->setMaskWord("test"); // 指定固定宽高$t->createImg(50);?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -