📄 gd.class.php
字号:
<?phpclass GD{ #[根目录路径] var $path; #[水印文字] var $word; #[水印字体] var $font; #[水印图片] var $picture; #[水印图片所在的位置,只有在支持水印图片的情况下有效] var $position = 9; #[水印融合度] var $light = 65; function GD($path="./",$words="",$font="",$picture="",$position="9",$light=65) { $this->path = $path; $this->word = $words ? $words : "www.phpok.com"; $this->font = $font ? $font : "04b_08__.ttf"; $this->picture = $picture; $this->position = $position ? $position : 9; $this->light = $light ? $light : 65; } function info($photo) { #需要包含整个文件路径 $infos = @getimagesize($photo); $info["width"] = $infos[0]; $info["height"] = $infos[1]; $info["type"] = $infos[2]; $info["name"] = substr(basename($photo),0,strrpos(basename($photo),".")); unset($infos); return $info; } function thumb($photo,$width=120,$height=90) { $info = $this->info($photo); #[计算是否支持水印,缩略图功能] if(!function_exists("imageline")) { return false; } else { #[设置缩略图文件的新名称] $new_name = "thumb_".basename($photo); $img = $this->___get_img($photo,$info); if(empty($img)) { return false; } else { $rate_width = $width / $info["width"]; $rate_height = $height / $info["height"]; if($info["width"] >= $width && $info["height"] > $height) { if($info["width"] > $info["height"]) { $tempx = $width / $rate_height; $tempy = $info["height"]; $srcx = ($info["width"] - $tempx) / 2; $srcy = 0; } else { $tempx = $info["width"]; $tempy = $height / $rate_width; $srcx = 0; $srcy = ($info["height"] - $tempy) / 2; } } else { if($info["width"] > $info["height"]) { $tempx = $width; $tempy = $info["height"]; $srcx = ($info["width"] - $tempx) / 2; $srcy = 0; } else { $tempx = $info["width"]; $tempy = $height; $srcx = 0; $srcy = ($info["height"] - $tempy) / 2; } } #[继续操作噢] $new_width = ($rate_width > 1) ? $info["width"] : $width; $new_height = ($rate_height > 1) ? $info["height"] : $height; if(function_exists("imagecopyresampled")) { $temp_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($temp_image, $img, 0, 0, $srcx, $srcy, $new_width, $new_height, $tempx, $tempy); } else { $temp_image = imagecreate($new_width, $new_height); imagecopyresized($temp_image, $img, 0, 0, $srcx, $srcy, $new_width, $new_height, $tempx, $tempy); } #[写入新图片地址中] ##[获取路径] $get_path = str_replace(basename($photo),"",$photo); if(file_exists($get_path.$new_name)) { @unlink($get_path.$new_name); } $return = $this->___create_image($temp_image,$get_path.$new_name,$info["type"]); imagedestroy($img); imagedestroy($temp_image); if($return) { return basename($return); } else { return false; } } } } function ___create_image($temp_image,$newfile,$info_type) { if($info_type == 1) { imagegif($temp_image,$newfile); } elseif($info_type == 2) { imagejpeg($temp_image,$newfile); } elseif($info_type == 3) { imagepng($temp_image,$newfile); } else { #[如果不存在这些条件,那将文件改为jpg的缩略图] $newfile = $newfile.".jpg"; if(file_exists($newfile)) { @unlink($newfile); } imagejpeg($temp_image,$newfile); } return $newfile; } function ___get_img($pic,$info) { if($info["type"] == 1) { $img = imagecreatefromgif($pic); } elseif($info["type"] == 2) { $img = imagecreatefromjpeg($pic); } elseif($info["type"] == 3) { $img = imagecreatefrompng($pic); } else { $img = ""; } return $img; } function mark($photo,$width=650,$height=650) { $info = $this->info($photo); #[计算是否支持水印,缩略图功能] if(!function_exists("imageline")) { return false; } else { #[设置水印图片的新名称] $new_name = "mark_".basename($photo); $img = $this->___get_img($photo,$info); if(empty($img)) { return false; } else { $width = $width > $info["width"] ? $info["width"] : $width; $height = $height > $info["height"] ? $info["height"] : $height; if(($info["width"] * $width) > ($info["height"] * $height)) { $height = round(($info["height"] * $width) / $info["width"]); } else { $width = round(($info["width"] * $height) / $info["height"]); } #[根据新图像的宽高来获取新文件要放置的地方 if (function_exists("imagecreatetruecolor")) { $new_img = imagecreatetruecolor($width, $height); imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $height,$info["width"], $info["height"]); } else { $new_img = imagecreate($width, $height); imagecopyresized($new_img, $img, 0, 0, 0, 0, $width, $height, $info["width"], $info["height"]); } if($this->picture && file_exists($this->path."images/".$this->picture)) { $xy = $this->___get_position($width,$height);#[含有图片时使用] $water_info = $this->info($this->path."images/".$this->picture); #[获取水印图片的其他信息] $my_water = $this->___get_img($this->path."images/".$this->picture,$info); imagecopymerge($new_img,$my_water,$xy["x"],$xy["y"],0,0,$water_info["width"],$water_info["height"],$this->light); } else { $word = $this->gb2utf(); $black = imageColorAllocate($new_img, 0, 0, 0); #[写入文字] if(function_exists("imagettftext")) { $xy = $this->___get_position($width,$height); $font_size = imagettfbbox(9,0,$this->path."include/".$this->font,$word); $char_height = $font_size[1] - $font_size[7]; $char_width = $font_size[2] - $font_size[0]; #[填充背景色] $light = ($this->light < 100) ? (100 - $this->light) : 80; $alpha = imageColorAllocateAlpha($new_img, 230, 230, 230, $light); if($this->position == 1 || $this->position == 2 || $this->position == 3) { imagefilledrectangle($new_img,($xy["x"]),0,($xy["x"]+$char_width),$char_height+4,$alpha); imagettftext($new_img,9,0, ($xy["x"]),$char_height, $black,$this->path."include/".$this->font,$word); } else { imagefilledrectangle($new_img,($xy["x"]-4),($xy["y"]-$char_height-4),($xy["x"]+$char_width),($xy["y"]),$alpha); imagettftext($new_img,9,0, ($xy["x"]-2), ($xy["y"]-4), $black,$this->path."include/".$this->font,$word); } } else { $xy = $this->___get_position($width,$height,false); $char_height = imagefontheight(2); $char_width = strlen(trim($this->word)) * imagefontwidth(2); $light = ($this->light < 100) ? (100 - $this->light) : 80; $alpha = imageColorAllocateAlpha($new_img, 230, 230, 230, $light); imagefilledrectangle($new_img,($xy["x"]-4),($xy["y"]-$char_height+10),($xy["x"]+$char_width+14),($xy["y"]+14),$alpha); imageString($new_img,2,$xy["x"]+7,$xy["y"]-4,$word,$black); } } #[写入新图片地址中] ##[获取路径] $get_path = str_replace(basename($photo),"",$photo); if(file_exists($get_path.$new_name)) { @unlink($get_path.$new_name); } $return = $this->___create_image($new_img,$get_path.$new_name,$info["type"]); imagedestroy($img); imagedestroy($new_img); if($return) { return basename($return); } else { return false; } } } } function ___get_position($width,$height,$tfont=true) { if($this->picture && file_exists($this->path."images/".$this->picture)) { $info = $this->info($this->path."images/".$this->picture); $img = $this->___get_img($this->path."images/".$this->picture,$info); $water_width = imagesx($img); $water_height = imagesy($img); } else { if($tfont) { $word = $this->gb2utf(); $font_size = imagettfbbox(9,0,$this->path."include/".$this->font,$word); $water_width = $font_size[2] - $font_size[0]; $water_height = $font_size[1] - $font_size[3]; } else { $water_height = imagefontheight(2); $water_width = strlen(trim($this->word)) * imagefontwidth(2); } } #[确定位置] switch ($this->position) { case 1: $x = 0; if($this->picture && file_exists($this->path."images/".$this->picture)) { $y = 0; } else { $y = $tfont ? $water_height : 4; } break; case 2: $x = ($width-$water_width)/2; if($this->picture && file_exists($this->path."images/".$this->picture)) { $y = 0; } else { $y = $tfont ? $water_height : 4; } break; case 3: $x = $width - $water_width - 14; if($this->picture && file_exists($this->path."images/".$this->picture)) { $y = 0; } else { $y = $tfont ? $water_height : 4; } break; case 4: $x = 0; $y = ($height / 2) - ($water_height / 2); break; case 5: $x = ($width/2) - ($water_width/2); $y = ($height / 2) - ($water_height / 2); break; case 6: $x = $width - $water_width - 14; $y = ($height / 2) - ($water_height / 2); break; case 7: $x = 0; $y = $height - $water_height; break; case 8: $x = ($width - $water_width) / 2; $y = $height - $water_height; break; case 9: $x = $width - $water_width - 14; $y = $height - $water_height; break; default: $x = 0; $y = $height - $water_height; break; } return array("x"=>$x,"y"=>$y); } function gb2utf() { $file = $this->path."include/gb2312_utf8.table"; $fp = fopen($file,"rb"); while ($l = fgets($fp,15)) { $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); } fclose($fp); $ret = ""; $utf8 = ""; $msg = $this->word; while ($msg) { if (ord(substr($msg, 0, 1)) > 0x80) { $thisW = substr($msg, 0, 2); $msg = substr($msg, 2, strlen($msg)); $utf8 = ""; @$utf8 = $this->u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080])); if($utf8!="") { for ($i = 0;$i < strlen($utf8);$i += 3) { $ret .= chr(substr($utf8, $i, 3)); } } } else { $ret .= substr($msg, 0, 1); $msg = substr($msg, 1, strlen($msg)); } } return $ret; } function u2utf8($c) { for ($i = 0;$i < count($c);$i++) $str = ""; if ($c < 0x80) { $str .= $c; } else if ($c < 0x800) { $str .= (0xC0 | $c >> 6); $str .= (0x80 | $c & 0x3F); } else if ($c < 0x10000) { $str .= (0xE0 | $c >> 12); $str .= (0x80 | $c >> 6 & 0x3F); $str .= (0x80 | $c & 0x3F); } else if ($c < 0x200000) { $str .= (0xF0 | $c >> 18); $str .= (0x80 | $c >> 12 & 0x3F); $str .= (0x80 | $c >> 6 & 0x3F); $str .= (0x80 | $c & 0x3F); } return $str; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -