<?php /** * Add Watermark Class * Jones Taiwan Blog */
Class water{
Private $imgPath; Picture path
Public function __construct ($imgPath = "./") { $this->imgpath = RTrim ($imgPath, "/"). " /"; }
Write Watermark Action Public Function Waterinfo ($ground, $water, $pos =0, $prefix = "Lee_", $tm =50) { $allPathGround = $this->imgpath. $ground; $allPathWater = $this->imgpath. $water; $groundInfo = $this->imginfo ($allPathGround); $waterInfo = $this->imginfo ($allPathWater);
Determine if the watermark image is larger than the original if (! $newPos = $this->imgpos ($groundInfo, $waterInfo, $pos)) { echo "Your watermark picture is bigger than the original"; return false; }
Open Resource $groundRes = $this->imgres ($allPathGround, $groundInfo [' mime ']); $waterRes = $this->imgres ($allPathWater, $waterInfo [' mime ']);
Consolidating resources $newGround = $this->imgcopy ($groundRes, $waterRes, $newPos, $waterInfo, $TM);
Save Resources $this->saveimg ($newGround, $ground, $groundInfo [' MIME '], $prefix);
}
Private Function Saveimg ($img, $ground, $info, $prefix) { $path = $this->imgpath. $prefix. $ground; Switch ($info) { Case "Image/jpg": Case "Image/jpeg": Case "Image/pjpeg": Imagejpeg ($img, $path); Break Case "Image/gif": Imagegif ($img, $path); Break Case "Image/png": Imagepng ($img, $path); Break Default IMAGEGD2 ($img, $path); } }
Private Function Imgcopy ($ground, $water, $pos, $waterInfo, $tm) { Imagecopymerge ($ground, $water, $pos [0], $pos [1],0,0, $waterInfo [0], $waterInfo [1], $TM); return $ground; }
Private Function Imgres ($img, $imgType) { Switch ($imgType) { Case "Image/jpg": Case "Image/jpeg": Case "Image/pjpeg": $res =imagecreatefromjpeg ($IMG); Break Case "Image/gif": $res =imagecreatefromgif ($IMG); Break Case "Image/png": $res =imagecreatefrompng ($IMG); Break Case "Image/wbmp": $res =imagecreatefromwbmp ($IMG); Break Default $res =imagecreatefromgd2 ($IMG); } return $res; }
Location is 1 Upper-Left 2 Upper 3 right 4 left Middle 5 Middle 6 right middle 7 lower left 8 lower 9 right lower 0 Random Positions Private Function Imgpos ($ground, $water, $pos) { if ($ground [0]< $water [0] | | $ground [1]< $water [1])//judging watermark and original image compare if watermark is higher or wider than the original The original youngster returns a fake return false; Switch ($pos) { Case 1: $x = 0; $y = 0; Break Case 2: $x =ceil (($ground [0]-$water [0])/2); $y = 0; Break Case 3: $x = $ground [0]-$water [0]; $y = 0; Break Case 4: $x = 0; $y =ceil (($ground [1]-$water [1])/2); Break Case 5: $x =ceil (($ground [0]-$water [0])/2); $y =ceil (($ground [1]-$water [1])/2); Break Case 6: $x = $ground [0]-$water [0]; $y =ceil (($ground [1]-$water [1])/2); Break Case 7: $x = 0; $y = $ground [1]-$water [1]; Break Case 8: $x =ceil ($ground [0]-$water [0]/2); $y = $ground [1]-$water [1]; Break Case 9: $x = $ground [0]-$water [0]; $y = $ground [1]-$water [1]; Break Case 0: Default $x =rand (0, $ground [0]-$water [0]); $y =rand (0, $ground [1]-$water [1]); } $xy []= $x; $xy []= $y; return $xy; }
function to get picture information Private Function Imginfo ($img) { Return getimagesize ($IMG); } } ?> |