Php image watermark function, php image watermark function code example
/**
- * Watermarks images (for png, jpg, and gif formats)
- *
- * @ Author flynetcn
- *
- * @ Param $ srcImg original image
- * @ Param $ waterImg watermark image
- * @ Param $ savepath: save path
- * @ Param $ savename save the name
- * @ Param $ position of the positon watermark
- * 1: Top left, 2: top right, 3: center, 4: bottom left, 5: bottom right
- * @ Param $ alpha transparency -- 0: completely transparent, 100: completely opaque
- * Script School http://bbs.it-home.org
- * @ Return success -- the new image address after the watermark is added
- * Failed ---1: The original file does not exist.-2: The watermark image does not exist.-3: The original file Image object creation failed.
- *-4: failed to create the watermark File Image object-5: failed to save the new image after adding the watermark
- */
- Function img_water_mark ($ srcImg, $ waterImg, $ savepath = null, $ savename = null, $ positon = 5, $ alpha = 30)
- {
- $ Temp = pathinfo ($ srcImg );
- $ Name = $ temp ['basename'];
- $ Path = $ temp ['dirname'];
- $ Exte = $ temp ['extension'];
- $ Savename = $ savename? $ Savename: $ name;
- $ Savepath = $ savepath? $ Savepath: $ path;
- $ Savefile = $ savepath. '/'. $ savename;
- $ Srcinfo = @ getimagesize ($ srcImg );
- If (! $ Srcinfo ){
- Return-1; // The original file does not exist.
- }
- $ Waterinfo = @ getimagesize ($ waterImg );
- If (! $ Waterinfo ){
- Return-2; // The watermark image does not exist.
- }
- $ SrcImgObj = image_create_from_ext ($ srcImg );
- If (! $ SrcImgObj ){
- Return-3; // failed to create the original file Image object
- }
- $ WaterImgObj = image_create_from_ext ($ waterImg );
- If (! $ WaterImgObj ){
- Return-4; // watermark File Image object creation failed
- }
- Switch ($ positon ){
- // 1 top left
- Case 1: $ x = $ y = 0; break;
- // 2. top right
- Case 2: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = 0; break;
- // 3 center
- Case 3: $ x = ($ srcinfo [0]-$ waterinfo [0])/2; $ y = ($ srcinfo [1]-$ waterinfo [1]) /2; break;
- // 4 the bottom is left
- Case 4: $ x = 0; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
- // 5 bottom right
- Case 5: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
- Default: $ x = $ y = 0;
- }
- Imagecopymerge ($ srcImgObj, $ waterImgObj, $ x, $ y, 0, 0, $ waterinfo [0], $ waterinfo [1], $ alpha );
- Switch ($ srcinfo [2]) {
- Case 1: imagegif ($ srcImgObj, $ savefile); break;
- Case 2: imagejpeg ($ srcImgObj, $ savefile); break;
- Case 3: imagepng ($ srcImgObj, $ savefile); break;
- Default: return-5; // save failed
- }
- Imagedestroy ($ srcImgObj );
- Imagedestroy ($ waterImgObj );
- Return $ savefile;
- }
Function image_create_from_ext ($ imgfile)
- {
- $ Info = getimagesize ($ imgfile );
- $ Im = null;
- Switch ($ info [2]) {
- Case 1: $ im = imagecreatefromgif ($ imgfile); break;
- Case 2: $ im = imagecreatefromjpeg ($ imgfile); break;
- Case 3: $ im = imagecreatefrompng ($ imgfile); break;
- }
- Return $ im;
- }
|