<?php Header ("Content-type:image/jpeg"); Original image $DST = "Images/flower_1.jpg"; Get the original picture information $DST _im = Imagecreatefromjpeg ($DST); $DST _info = getimagesize ($DST); Watermark Image $SRC = "Images/logo.gif"; $src _im = imagecreatefromgif ($SRC); $src _info = getimagesize ($SRC); Watermark Transparency $alpha = 30; Merging Watermark Pictures Imagecopymerge ($dst _im, $src _im, $DST _info[0]-$src _info[0], $dst _info[1]-$src _info[1],0,0, $src _info[0], $SRC _info[1], $alpha); Output merged watermark Picture Imagejpeg ($dst _im); Imagedestroy ($dst _im); Imagedestroy ($src _im); ?> After the new version Imagecopymerge function is almost not used, we can directly use Imagecopy to generate watermark Two function is exactly the same. Add watermark $watermark = 1; $watertype = 2; $waterstring = '; $waterimg = "Z.png"; Watermark Picture $sFilePath = ' aa.jpg '; if ($watermark ==1) { $image _size = getimagesize ($sFilePath); Uploaded pictures $water _size = getimagesize ($waterimg); Watermark Picture $iinfo =getimagesize ($sFilePath, $iinfo); $nimage =imagecreatetruecolor ($image _size[0], $image _size[1]); $white =imagecolorallocate ($nimage, 255,255,255); $black =imagecolorallocate ($nimage, 0,0,0); $red =imagecolorallocate ($nimage, 255,0,0); Imagefill ($nimage, 0,0, $white); Switch ($iinfo [2]) { Case 1: $simage =imagecreatefromgif ($sFilePath); Break Case 2: $simage =imagecreatefromjpeg ($sFilePath); Break Case 3: $simage =imagecreatefrompng ($sFilePath); Break Case 6: $simage =imagecreatefromwbmp ($sFilePath); Break Default Die ("Unsupported file type"); Exit }
Imagecopy ($nimage, $simage, 0,0,0,0, $image _size[0], $image _size[1]);
Switch ($watertype) { Case 1://Add watermark String Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black); Break Case 2://Add watermark Picture $simage 1 =imagecreatefrompng ($waterimg); $x = $image _size[0]-$water _size[0]; $y = $image _size[1]-$water _size[1]; Imagecopy ($nimage, $simage 1, $x, $y, 0,0,240,65); Imagedestroy ($simage 1); Break }
Switch ($iinfo [2]) { Case 1: Imagegif ($nimage, $sFilePath); Imagejpeg ($nimage, $sFilePath); Break Case 2: Imagejpeg ($nimage, $sFilePath); Break Case 3: Imagepng ($nimage, $sFilePath); Break Case 6: Imagewbmp ($nimage, $sFilePath); Imagejpeg ($nimage, $sFilePath); Break }
Overwrite original upload file Imagedestroy ($nimage); Imagedestroy ($simage); } |