<? PHP $ Image = new new Image (); $ Image -> Water ( './ Upload / up_5cf0caca0565b.png', '. / Upload / up_5cf0cb3a61fae.jpg',. 9 ); class Image { // path protected $ path ; // whether to enable random name protected $ isRandName ; // to save the image type protected $ of the type ; // initialize member properties via the constructor function __construct ( $ path = './', $ isRandName = to true , $ of the type = 'PNG' ) { $ the this-> path = $ path ; $ the this -> isRandName = $ isRandName ; $ the this -> = of the type of the type $ ; } / * * * publicly watermarking method * * @param [of the type] $ Image original images * @param [ type] $ water watermark image * @param [type] $ postion watermark image position * @param integer $ tmd transparency watermark image * @param string $ prefix picture of the prefix * @return void * / function Water ( $ Image , $ Water , $ postion , $ TMD = 100, $ prefix = 'water_' ) { // 1, these two images is determined whether there IF ((! The file_exists ( $ Image )) || (! The file_exists ( $ Water ))) { Die ( "Image resource does not exist ' ); } // 2, to give original pictures of width and height, and the watermark image of broadband and high $ imageinfo = Self :: getImageInfo ( $ Image ); $ waterInfo = Self :: getImageInfo ( $ Water ); // 3, to determine whether the watermark image Tieshanglai IF ( ! $ the this -> checkImage ( $ imageinfo , $ waterInfo )) { Exit ( 'watermark image is too large' ); } // 4, open images $ imageRes = Self :: openAnyImage ( $ Image ); $ waterRes = Self :: openAnyImage ( $ Water ); // . 5, the watermark image is calculated based on the coordinate position of the watermark image $ POS = $ the this -> getPostion ( $ postion , $ imageinfo , $ waterInfo ); // . 6, the paste over the watermark image imagecopymerge ( $ imageRes , $ waterRes , $ POS [ 'X'], $ POS [ 'Y'], 0, 0 , $ waterInfo [ 'width'], $ waterInfo [ 'height'], $ TMD ); // 7, to get the picture you want to save the file name $ newName = $ the this -> createNewName ( $ Image , $ prefix ); // 8, preserved picture of the path, which is the full path to the file $ newPath = RTRIM ( the this $ .. -> path, '/') '/' $ newName ; // 9, save the picture $ the this -> saveimage ( $ imageRes , $ newPath ); // 10, the destruction of resources imagedestroy ( $ imageRes ); imagedestroy ( $ waterRes ); return $ newPath ; } / * * @param [type] $ the imagePath * * Save image resource function * * @param [type] $ imageRes * @param [type] $ newPath * @return void * / protected function saveimage ( $ imageRes , $ newPath ) { // imagepng imagegif imagewbmp $ FUNC = 'Image' . $ the this -> type; // save function by the variable $ FUNC ( $ imageRes , $ newPath ); } / * * * function obtained file name * * @param [type] $ prefix * @return void * / protected function createNewName ( $ the imagePath , $ prefix ) { IF ( $ the this -> isRandName) { $ name = $ prefix . of uniqid (). '.'. $ the this -> type; } the else { $ name = $ prefix . The pathinfo ( the imagePath $ ) [ 'filename']. '.'. $ the this -> type; } return $ name ; } / * * * calculated according to the position coordinates of the watermark image * * @param [type] $postion * @param [type] $imageInfo * @param [type] $waterInfo * @return void */ protected function getPostion($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]; } / * * * Is determined whether the watermark image larger than the original image * * @param [type] $ imageinfo * @param [type] $ waterInfo * @return void * / protected function 's checkImage ( $ imageinfo , $ waterInfo ) { IF (( $ waterInfo [ 'width']> $ imageinfo [ 'width']) || ( $ waterInfo [ 'height']> $ imageinfo [ 'height' ])) { return to false ; } return to true ; } / * * * static method, in accordance with the picture image obtained path information, width, height, MIME type * * @Param [type] $ the imagePath * @return void * / static function getImageInfo ( $ the imagePath ) { // get the picture information $ info = the getimagesize ( $ the imagePath ); // save the image width $ Data [ 'width'] = info $ [0 ]; // save the image height $ Data [ 'height'] = $ info [. 1 ]; // save the picture mime type $ Data [ 'mime'] = $ info [ 'mime' ]; // will Image return return $ Data ; } / * * * open any type of image based on the image * * @param [type] $ the imagePath * @return void * / static function openAnyImage ( $ the imagePath ) { // get the picture of the mime type $ mime = Self :: getImageInfo ( $ the imagePath ) [ 'mime' ]; // use different functions depending on the mime type open image 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; } }