Sometimes upload images need to add a watermark to the site, watermark can be divided into text watermark and image watermark, the following implementation of these two kinds of watermark
text watermark is to add text on the picture, mainly using the Imagefttext method of GD library, and need font file. The effect figure is as follows: The implementation code is as follows: code as follows: $dst _path = ' dst.jpg '; //Create an instance of the picture $dst = Imagecreatefromstring (file_get_contents ($DST _path)); //Text $font = './SIMSUN.TTC '//font $black = Imagecolorallocate ($DST, 0x00, 0x00, 0x00);//Font Color Imagefttext ($DST, 13, 0, $black, $font, ' happy programming '); //Output Picture list ($dst _w, $dst _h, $dst _type) = getimagesize ($dst _path); Switch ($dst _type) { case 1://gif header (' content-type:image/gif '); &nbs P Imagegif ($DST); break; Case 2://jpg header (' content-type:image/jpeg '); imagejpeg ($DST); break; Case 3://png header (' content-type:image/png '); imagepng ($DST); break; Default: break; } Imagedestroy ($DST); Image Watermark &nbsP Image watermark is to add a picture on another picture, mainly using the GD library imagecopy and Imagecopymerge. The effect chart is as follows: Implementation code as follows: code as follows: $dst _path = ' dst.jpg '; $src _path = ' src.jpg '; //Create an instance of the picture $dst = Imagecreatefromstring (file_get_contents ($DST _path)); $SRC = imagecreatefromstring (file_get_contents ($src _path)); //Get watermark Picture of wide high list ($src _w, $src _h) = getimagesize ($src _path); //Copy the watermark picture to the target picture, the last parameter 50 is set the transparency, here realizes the translucent effect Imagecopymerge ($DST, $SRC, ten, 0, 0, $src _w, $src _h, 50); If the watermark picture itself is transparent, use the Imagecopy method//imagecopy ($DST, $SRC, ten, 0, 0, $src _w, $src _h); //Output Picture list ($dst _w, $dst _h, $dst _type) = getimagesize ($dst _path); Switch ($dst _type) { case 1://gif header (' content-type:image/gif '); &nbs P Imagegif ($DST); break; Case 2://jpg header (' content-type:image/jpeg '); imagejpeg ($DST); break; Case 3://png header (' Content-type:image/png ')); imagepng ($DST); break; Default: break; } Imagedestroy ($DST); Imagedestroy ($SRC);