电脑安全

您当前的位置:主页 > 电脑安全 >

php给图片加水印六-网络编程

来源:[db:来源]标题:   水印    编辑:电脑技术全能网 时间:2019-09-19 12:33

原标题:php给图片加水印六-网络编程
   <?function _countMaskPos() { if($this->_isFull()) { switch($this->mask_position) { case 1: // 左上 $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 2: // 左下 $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; case 3: // 右上 $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 4: // 右下 $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; default: // 默许将水印放到右下,偏移指定像素 $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; } } else { switch($this->mask_position) { case 1: // 左上 $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 2: // 左下 $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; case 3: // 右上 $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 4: // 右下 $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; default: // 默许将水印放到右下,偏移指定像素 $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; } } } /** * 设置字体信息 */ function _setFontInfo() { if(is_numeric($this->font)) { $this->font_w = imagefontwidth($this->font); $this->font_h = imagefontheight($this->font); // 盘算水印字体所占宽高 $word_length = strlen($this->mask_word); $this->mask_w = $this->font_w*$word_length; $this->mask_h = $this->font_h; } else { $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word); $this->mask_w = abs($arr[0] - $arr[2]); $this->mask_h = abs($arr[7] - $arr[1]); } } /** * 设置新图尺寸 * * @param integer $img_w 目的宽度 * @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 - 10 ;//弥补round偏差 打消白边; 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; }}?>上面来看应用方式:require_once('lib/ImageHandler.phpcls');$imgHandler_Obj = new ImageHandler();/* 默许减少缩小 */$imgHandler_Obj->setSrcImg("img/xrk.jpg");$imgHandler_Obj->setDstImg("tmp/New_Image_0.jpg");$imgHandler_Obj->setCutType(0);// 指定缩放比例$imgHandler_Obj->createImg(20);//50 100 200 尝尝/* 1, 笔墨水印图片减少$imgHandler_Obj->setSrcImg("img/xrk.jpg");$imgHandler_Obj->setMaskFont("Fonts/STXINGKA.TTF");$imgHandler_Obj->setMaskFontSize(15);$imgHandler_Obj->setMaskFontColor("#0061A7");$imgHandler_Obj->setMaskTxtPct(20);$imgHandler_Obj->setDstImgBorder(0,"#dddddd");$imgHandler_Objext = "Happy闲逛者";//$str = mb_convert_encoding($imgHandler_Objext,"UTF-8","GBK");$imgHandler_Obj->setMaskWord($imgHandler_Objext);// 指定牢固宽高 输入到扫瞄器$imgHandler_Obj->createImg(400,400);*//* 2, 加水印图片减少$imgHandler_Obj->setSrcImg("img/test.jpg");$imgHandler_Obj->setDstImg("tmp/New_Image_2.jpg");$imgHandler_Obj->setMaskImg("img/t.gif");$imgHandler_Obj->setMaskPosition(0);//0为在图片右下角参加水印 $imgHandler_Obj->setMaskImgPct(60);$imgHandler_Obj->setDstImgBorder(1,"#dddddd");// 指定缩放比例$imgHandler_Obj->createImg(400,400);*//* 3, 同步减少 $imgHandler_Obj->setSrcImg("img/test.jpg"); $imgHandler_Obj->setCutType(0);//同步减少 $imgHandler_Obj->setDstImg("tmp/New_Image_3.jpg"); $imgHandler_Obj->createImg(150,160);*//* 4, 剪裁图片$imgHandler_Obj->setSrcImg("img/test.jpg"); $imgHandler_Obj->setCutType(2);//指明为手工裁切 $imgHandler_Obj->setSrcCutPosition(50, 50);// 源图终点坐标 $imgHandler_Obj->setRectangleCut(300, 200);// 裁切尺寸 $imgHandler_Obj->setDstImg("tmp/New_Image_4.jpg"); $imgHandler_Obj->createImg(300,200);*/

上一篇:PHP中通过ADO调用Access数据库-网络编程

下一篇:没有了