文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>生成水印的類

生成水印的類

时间:2006-10-23  来源:w3g8

[PHP]
Define("STREAM", 0);
Define("HANDLE", 1);
Define("FILE_JPEG", 2);
Define("FILE_GIF", 3);
Define("FILE_PNG", 4);
Define("FILE_WBMP", 5);
Define("FILE_XBM", 6);
Define("FILE_XPM", 7);
Define("IMG_JPEG", 8);
Define("IMG_PNG", 9);
Define("IMG_GIF", 10);
class RWatermark
{
var $original_image = -1;
var $mark_image = -1;
var $marked_image = -1;
var $original_width;
var $original_height;
var $mark_width;
var $mark_height;
var $mark_position = "RND";
var $mark_offset_x = 0;
var $mark_offset_y = 0;
var $transparent_color = -1;
var $transparency = 100;
var $gd_version;
var $version = 'RWatermark 0.92';
function RWatermark($type, $str)
{
$this->setGDInfo();
$type = StrToUpper($type);
switch ($type)
{
case STREAM:
$this->original_image = ImageCreateFromString($str);
break;
case FILE_JPEG:
$this->original_image = ImageCreateFromJPEG($str);
break;
case FILE_GIF:
$this->original_image = ImageCreateFromGIF($str);
break;
case FILE_PNG:
$this->original_image = ImageCreateFromPNG($str);
break;
case FILE_WBMP:
$this->original_image = ImageCreateFromWBMP($str);
break;
case FILE_XBM:
$this->original_image = ImageCreateFromXBM($str);
break;
case FILE_XPM:
$this->original_image = ImageCreateFromXPM($str);
break;
case HANDLE:
$this->mark_image = $str;
break;
default:
$this->dieError("Unknown input type in constructor method!");
return false;
}
if (!$this->original_image)
{
$this->dieError("GD command error in constructor method!");
return false;
}
$this->original_width = ImageSX($this->original_image);
$this->original_height = ImageSY($this->original_image);
return true;
}
function AddWatermark($type, $str)
{
$type = StrToUpper($type);
switch ($type)
{
case STREAM:
$this->mark_image = ImageCreateFromString($str);
break;
case FILE_JPEG:
$this->mark_image = ImageCreateFromJPEG($str);
break;
case FILE_GIF:
$this->mark_image = ImageCreateFromGIF($str);
break;
case FILE_PNG:
$this->mark_image = ImageCreateFromPNG($str);
break;
case FILE_WBMP:
$this->mark_image = ImageCreateFromWBMP($str);
break;
case FILE_XBM:
$this->mark_image = ImageCreateFromXBM($str);
break;
case FILE_XPM:
$this->mark_image = ImageCreateFromXPM($str);
break;
case HANDLE:
$this->mark_image = $str;
break;
default:
$this->dieError("Unknown input type in addWatermark method!");
return false;
}
if (!$this->mark_image)
{
$this->dieError("GD command error in addWatermark method!");
return false;
}
$this->mark_width = ImageSX($this->mark_image);
$this->mark_height = ImageSY($this->mark_image);
$this->getOffsets();
$this->createMarkedImage();
return true;
}
function GetMarkedImage($type = HANDLE, $param1 = "", $param2 = "", $param3 = "")
{
if ($this->marked_image != -1)
{
$type = StrToUpper($type);
switch ($type)
{
case HANDLE:
return $this->marked_image;
case IMG_PNG:
if ($param1 != "") {ImagePNG($this->marked_image, $param1);}
else {ImagePNG($this->marked_image);}
return true;
case IMG_GIF:
if ($param1 != "") {ImageGIF($this->marked_image, $param1);}
else {ImageGIF($this->marked_image);}
return true;
case IMG_JPEG:
if (StrToUpper($param3) == "YES") {ImageInterlace($this->marked_image, 1);}
else {ImageInterlace($this->marked_image, 0);}
if ($param2 == "") {$param2 = 75;}
ImageJPEG($this->marked_image, $param1, $param2);
return true;
default:
$this->dieError("Unknown output type in getMarkedImage method!");
}
}
return false;
}
function SetPosition($newposition = "RND", $x = 0, $y = 0)
{
$valid_positions = array("TL", "TM", "TR", "CL", "CM", "CR", "BL", "BM", "BR", "ABS", "RND");
$newposition = StrToUpper($newposition);
if (In_Array($newposition, $valid_positions))
{
$this->mark_position = $newposition;
if ($this->mark_position == "ABS")
{
$this->mark_offset_x = $x;
$this->mark_offset_y = $y;
}
return true;
}
return false;
}
function SetTransparency($trans)
{
if (($trans >= 0) && ($trans transparency = 100 - $trans;
return true;
}
return false;
}
function SetTransparentColor($r,$g,$b)
{
if (($r >= 0) && ($r = 0) && ($g = 0) && ($b transparent_color = ($r original_image != -1) {ImageDestroy($this->original_image);}
if ($this->marked_image != -1) {ImageDestroy($this->marked_image);}
if ($this->mark_image != -1) {ImageDestroy($this->mark_image);}
}
function createMarkedImage()
{
$this->marked_image = ImageCreateTrueColor($this->original_width, $this->original_height);
ImageCopy($this->marked_image, $this->original_image, 0, 0, 0, 0, $this->original_width, $this->original_height);
if ($this->transparent_color != -1)
{
$transparent_color_index = ImageColorExact($this->mark_image, ($this->transparent_color >> 16) & 0xFF, ($this->transparent_color >> 8) & 0xFF, $this->transparent_color & 0xFF);
ImageColorTransparent($this->mark_image,$transparent_color_index);
}
if ($this->gd_version >= 2) {
ImageAlphaBlending($this->marked_image, true);
}
ImageCopyMerge($this->marked_image, $this->mark_image, $this->mark_offset_x, $this->mark_offset_y, 0, 0, $this->mark_width, $this->mark_height, $this->transparency);
}
function getOffsets()
{
$width_left = $this->original_width - $this->mark_width;
$height_left = $this->original_height - $this->mark_height;
switch ($this->mark_position)
{
case "TL": $this->mark_offset_x = $width_left >= 5 ? 5 : $width_left;
$this->mark_offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TM": $this->mark_offset_x = intval(($this->original_width - $this->mark_width) / 2);
$this->mark_offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TR": $this->mark_offset_x = $this->original_width - $this->mark_width;
$this->mark_offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "CL": $this->mark_offset_x = $width_left >= 5 ? 5 : $width_left;
$this->mark_offset_y = intval(($this->original_height - $this->mark_height) / 2);
break;
case "CM": $this->mark_offset_x = intval(($this->original_width - $this->mark_width) / 2);
$this->mark_offset_y = intval(($this->original_height - $this->mark_height) / 2);
break;
case "CR": $this->mark_offset_x = $this->original_width - $this->mark_width;
$this->mark_offset_y = intval(($this->original_height - $this->mark_height) / 2);
break;
case "BL": $this->mark_offset_x = $width_left >= 5 ? 5 : $width_left;
$this->mark_offset_y = $this->original_height - $this->mark_height - 5;
break;
case "BM": $this->mark_offset_x = intval(($this->original_width - $this->mark_width) / 2);
$this->mark_offset_y = $this->original_height - $this->mark_height - 5;
break;
case "BR": $this->mark_offset_x = $this->original_width - $this->mark_width - 5;
$this->mark_offset_y = $this->original_height - $this->mark_height - 5;
break;
case "ABS": $this->mark_offset_x = ($this->mark_offset_x >= 0) && ($this->mark_offset_x mark_offset_x : 0;
$this->mark_offset_y = ($this->mark_offset_y >= 0) && ($this->mark_offset_y mark_offset_y : 0;
break;
case "RND":
$this->mark_offset_x = Rand(5, $width_left - 5);
$this->mark_offset_y = Rand(5, $height_left - 5);
break;
}
}
function dieError($err)
{
Die($err);
}
function setGDInfo()
{
$gdinfo = gd_info();
if (PReg_Match('/(\d)\.\d/', $gdinfo["GD Version"], $gdinfo)) {$this->gd_version = $gdinfo[1];}
else {$this->gd_version = 0;}
UnSet($gdinfo);
}
}
[/PHP]

使用方法
[PHP]
$handle = new RWatermark(FILE_JPEG, "./105975a699e.jpg");
$handle->SetPosition("RND");
$handle->SetTransparentColor(255, 0, 255);
$handle->SetTransparency(60);
$handle->AddWatermark(FILE_PNG, "./23.png");
Header("Content-type: image/png");
$handle->GetMarkedImage(IMG_PNG);
$handle->Destroy();


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载