This article mainly for you to introduce the PHP image Watermark class package, with a certain reference value, interested in small partners can refer to
Encapsulated PHP Image Watermark class, for your reference, the specific content as follows
<?phpheader (' Content-type:text/html;charset=utf8 '); $img = new Image ();//$img->water (' 2a.jpg ', ' logo.gif ', 0); Class image{//path protected $path; Whether to enable the random name protected $isRandName; The type of image to save protected $type; Initialize the function __construct ($path = './', $isRandName =true, $type = ' png ') {$this->path = $path by constructing the method team member property; $this->israndname = $isRandName; $this->type = $type; }//Public watermark Method/** * @param char $image original * @param char $water watermark picture * @param char $postion position * @param int $ TMP transparency * @param char $prefix prefix */function water ($image, $water, $postion, $tmp =100, $prefix = ' water_ ') {//Determine if the two images are No presence if (!file_exists ($image) | |! File_exists ($water)) {die (' Picture resource not present '); }//Get the width of the original image and watermark picture $imageInfo = Self::getimageinfo ($image); $waterInfo = Self::getimageinfo ($water); Determine if the watermark image can be affixed to if (! $this->checkimage ($imageInfo, $waterInfo)) {die (' watermark image too large '); }//Open picture $imageRes = Self::openanyimage ($image); $waterRes = Self::oPenanyimage ($water); Calculates the coordinates of the watermark image according to the location of the watermark picture $pos = $this->getposition ($postion, $imageInfo, $waterInfo); Paste the watermark picture over Imagecopymerge ($imageRes, $waterRes, $pos [' X '], $pos [' y '], 0, 0, $waterInfo ["width"], $waterInfo ["Height"], $TMP); Get the file name of the picture you want to save $newName = $this->createnewname ($image, $prefix); Get the path to save the picture, that is, the full path of the file $newPath = RTrim ($this->path, '/'). ' /'. $newName; Save Picture $this->saveimage ($imageRes, $newPath); Destruction of resources Imagedestroy ($imageRes); Imagedestroy ($waterRes); Return the path return $newPath; }//Save Image Resource protected function saveimage ($imageRes, $newPath) {$func = ' image '. $this->type; Save $func by variable function ($imageRes, $newPath); }//Get the filename function protected functions createnewname ($imagePath, $prefix) {if ($this->israndname) {$name = $prefix. Uni QID (). '. '. $this->type; }else {$name = $prefix. PathInfo ($imagePath) [' filename ']. $this->type; } return $name; }//Calculate the coordinates of the watermark picture according to the location protected function getPosition ($postion, $imageInfo, $waterInfo) {switch ($postion) {Case 1: $x = 0; $y = 0; Break Case 2: $x = ($imageInfo [' width ']-$waterInfo ["width"])/2; $y = 0; Break Case 3: $x = $imageInfo ["width"]-$waterInfo ["width"]; $y = 0; Break Case 4: $x = 0; $y = ($imageInfo ["height"]-$waterInfo ["height"])/2; Break Case 5: $x = ($imageInfo [' width ']-$waterInfo ["width"])/2; $y = ($imageInfo ["height"]-$waterInfo ["height"])/2; Break Case 6: $x = $imageInfo ["width"]-$waterInfo ["width"]; $y = ($imageInfo ["height"]-$waterInfo ["height"])/2; Break Case 7: $x = 0; $y = $imageInfo [' height ']-$waterInfo ["height"]; Break Case 8: $x = ($imageInfo [' width ']-$waterInfo ["width"])/2; $y = $imageInfo [' height ']-$waterInfo ["height"]; Break Case 9: $x = $imageInfo ["width"]-$waterInfo ["width"]; $y = $imageInfo [' height ']-$waterInfo ["height"]; Break Case 0: $x = Mt_rand (0, $imageInfo ["width"]-$waterInfo ["width"]); $y = Mt_rand (0, $imageInfo [' height ']-$waterInfo ["height"]); Break } return [' X ' = $x, ' y ' = ' $y]; } protected function Checkimage ($imageInfo, $waterInfo) {if ($waterInfo [' width '] > $imageInfo [' width ']) | | ($waterInfo [' height '] > $imageInfo [' Height ']) {return false; } return true; }//static method. Get information about the picture, width, height, MIME type static function Getimageinfo ($imagePath) {$info = getimagesize ($imagePath) According to the path of the picture; $data [' width ']= $info [0]; $data [' Height ']= $info [1]; $data [' mime '] = $info [' MIME ']; return $data; } static function Openanyimage ($imagePath) {//Gets the MIME type of the image $mime = Self::getimageinfo ($imagePath) [' MIME ']; Open different images according to different MIME types switch ($mime) {case ' image/png ': $image = Imagecreatefrompng ($imagePath); Break Case ' image/gif ': $image = ImagecreaTefromgif ($imagePath); Break Case ' image/jpeg ': $image = Imagecreatefromjpeg ($imagePath); Break Case ' image/wbmp ': $image = imagecreatefromwbmp ($imagePath); Break } return $image; }